Correct Docker file
FROM golang:1.21-alpine
# Set the working directory
WORKDIR /app
# Copy go.mod and go.sum first (to cache dependencies)
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy the rest of the application code
COPY . .
# Build the application
RUN go build -o main .
# Set environment variable and expose port
ENV PORT 8080
EXPOSE 8080
# Run the application
CMD ["./main"]
Steps to Prepare Your Code
1. In your project root (where your Go code is), run:
go mod init example-app
go mod tidy
This creates go.mod and go.sum.
2. Then rebuild your Docker image:
docker build --dns=8.8.8.8 -t test .