I’m going to create two types of docker files. One for the local testing, and one for the production. Before creating the docker file, I want to create the docker ignore file because geometry and static folders are not used.
#.dockerignore
geometry/
static/
*.exe
Create dockerfile
FROM golang:latest
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o main .
EXPOSE 80
ENTRYPOINT [ "./main" ]
build and run
docker build -t go-app:latest .
docker run -d -p 80:80 --name web go-app:latest
The problem of this image has 974MB which is way too big for a simple application. I will use multi stage builds.
FROM golang:latest as builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o main .
# EXPOSE 80
# ENTRYPOINT [ "./main" ]
# Second stage
FROM gcr.io/distroless/base-debian11
COPY --from=builder /app/main .
EXPOSE 80
CMD ["/main"]
Much better! 👏
Docker-compose file
// docker-compose.yml
version: "3.9"
services:
web:
# build from here
build:
# the location where your dockerfile is existing
context: .
dockerfile: Dockerfile
#name of image
image: go-app-ms:latest
ports:
- "80:80"
# always restart
restart: always
networks:
- web
networks:
web:
docker-compose build
docker-compose up -d