Home >Backend Development >Golang >Hands On Docker For Beginners Golang Dev
This Article wouldn't explain how Docker works on the hood, instead this Article will explain what the purpose on each code that written on Dockerfile and docker-compose.yml file so we can write our Docker configuration for other project.
We need some project with Dockerfile and docker-compose for example, here we would use Golang Project named Ecom as example. For using Dockerfile you need to setup Local Database as mentioned on README.
Dockerfile used to create a container image.(6)
# Build application from source FROM golang:1.23.0 AS build-stage WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -o /api ./cmd/main.go
FROM golang:1.23.0 AS build-stage this is an Image for our App, it's similar like we download Go engine to our Machine, Docker need specific Image to run our Code.(1)(2)
WORKDIR /app this is the working directory we want our Code to be executed at /app.
COPY go.mod go.sum ./ this code will copying go.mod and go.sum file from local machine to ./ directory on Docker.
RUN go mod download this will executed command go mod download on Docker
COPY . . this code will copy all file and folder project from local machine to Docker.
RUN CGO_ENABLED=0 GOOS=linux go build -o /api ./cmd/main.go this code will executed command to build Golang app on linux OS to folder /api on Docker.
# Deploy application binary into a lean image FROM scratch AS build-realease-stage WORKDIR / COPY --from=build-stage /api /api EXPOSE 8080 ENTRYPOINT [ "/api" ]
FROM scratch AS build-realease-stage scratch used to create minimal images containing only just what an application needs.
WORKDIR / we will using root / as working directory.
COPY --from=build-stage /api /api this will copying directory /api from image build-stage to /api on build-realease-stage image.
EXPOSE 8080 this will expose port 8080 so we can access API with port 8080 outside Docker.
ENTRYPOINT [ "/api" ] this will set default executable at /api
Let's try our Dockerfile.
sudo docker build .
docker build to build our project into an Image. You can add tags -t project-ecom to easier identify Images you build.(3)
You can check the Image list with command sudo docker image ls
# Build application from source FROM golang:1.23.0 AS build-stage WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -o /api ./cmd/main.go
Then Run our Docker Image
--rm to remove container when stopped
--network host to connect docker app to localhost machine (4),(5)
--env-file .env to access environment value through .env file
98bc0128576e docker image ID
You now can testing to Consume API with Postman or other apps.
Docker Compose used to make multiple container services and run it inside Docker. In this project docker-compose.yml there are 4 services that we will explain.
# Build application from source FROM golang:1.23.0 AS build-stage WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -o /api ./cmd/main.go
# Deploy application binary into a lean image FROM scratch AS build-realease-stage WORKDIR / COPY --from=build-stage /api /api EXPOSE 8080 ENTRYPOINT [ "/api" ]
sudo docker build .
# Build application from source FROM golang:1.23.0 AS build-stage WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -o /api ./cmd/main.go
# Deploy application binary into a lean image FROM scratch AS build-realease-stage WORKDIR / COPY --from=build-stage /api /api EXPOSE 8080 ENTRYPOINT [ "/api" ]
Let's try our Docker Compose
sudo docker build .
This command will create images for each services and run each container.
sudo docker run --rm --network host --env-file .env 98bc0128576e
You can check the container our docker-compose.yml created.
nginx: image: nginxproxy/nginx-proxy:1.6 networks: - default ports: - "80:80" volumes: - /var/run/docker.sock:/tmp/docker.sock:ro - /etc/timezone:/etc/timezone:ro - /etc/localtime:/etc/localtime:ro environment: HTTPS_METHOD: nohttps
You can check volume our docker-compose.yml created.
db: image: mysql:8.0 networks: new: aliases: - database healthcheck: test: mysqladmin ping -h database -u ${DB_USER} --password=${DB_PASSWORD} volumes: - db_data:/var/lib/mysql - /etc/timezone:/etc/timezone:ro - /etc/localtime:/etc/localtime:ro ports: - "3308:3306" environment: MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} MYSQL_DATABASE: ${DB_NAME}
You can check images our docker-compose.yml created.
You can test Consume API from our project based on documentation on README with Postman or other apps.
if you have done you can stop the container with,
migrate-up: image: migrate/migrate networks: - new volumes: - ./cmd/migrate/migrations:/migrations - /etc/timezone:/etc/timezone:ro - /etc/localtime:/etc/localtime:ro command: ["-path", "/migrations", "-database", "${CONNECT_DB}", "-verbose", "up"] links: - db depends_on: db: condition: service_healthy
Or if you want to delete all container service inside docker compose you can run,
# Build application from source FROM golang:1.23.0 AS build-stage WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -o /api ./cmd/main.go
(1)Dockerfile Reference
(2)Docker Base Image
(3)Docker Build
(4)Docker Network Tutorials
(5)Docker Network Drivers
(6)Writing a Dockerfile
(7)Docker Hub Mysql
(8)Nginx-Proxy Docs
(9)Golang Migrate
The above is the detailed content of Hands On Docker For Beginners Golang Dev. For more information, please follow other related articles on the PHP Chinese website!