Container deployment refers to deploying Golang applications using Dockerization, the most popular and recommended deployment method in the cloud service era.

In the examples below, we use main as the project name consistently.

1. Compile Program

Cross-platform cross-compilation is one of Golang’s features, allowing us to easily compile the version needed for the target server platform, and it’s statically compiled, making it very easy to solve runtime dependencies.

Use the following command to statically compile an executable for the Linux platform amd64 architecture:

  1. CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go

The generated main is our statically compiled executable file deployable on Linux amd64.

2. Compile Image

We need to compile this executable file main into a Docker image for distribution and deployment. The runtime environment for Golang is recommended to use the Alpine base system image, resulting in a container image of about 20MB.

A reference Dockerfile is as follows:

  1. FROM loads/alpine:3.8
  2. LABEL maintainer="john@goframe.org"
  3. ###############################################################################
  4. # INSTALLATION
  5. ###############################################################################
  6. # Set a fixed project path
  7. ENV WORKDIR /app/main
  8. # Add application executable and set permissions
  9. ADD ./main $WORKDIR/main
  10. RUN chmod +x $WORKDIR/main
  11. # Add static resource files
  12. ADD resource $WORKDIR/resource
  13. ###############################################################################
  14. # START
  15. ###############################################################################
  16. WORKDIR $WORKDIR
  17. CMD ./main

In this example, our base image is loads/alpine:3.8. For users in China, it is recommended to use this base image. The Dockerfile address for the base image is: https://github.com/gqcn/dockerfiles, and the repository address is: https://hub.docker.com/u/loads.

Then use the docker build -t main . command to compile and generate a Docker image named main.

Notes

It should be noted that in some project architectural designs, static files and configuration files may not be compiled and published with the image but managed and published separately.

For example, in projects using the MVVM pattern (such as those using the Vue framework), the front end and back end are often very independent, so the public directory is often not included in the image. In projects that use a configuration management center (such as using consul/etcd/zookeeper), the config directory is often not needed.

Therefore, the use of the Dockerfile provided above is only for reference, and necessary adjustments should be made based on actual conditions.

3. Run Image

Use the following command to run the image just compiled:

  1. docker run main

4. Image Distribution

For container distribution, you can use the official Docker platform: https://hub.docker.com/, or consider using Alibaba Cloud in China: https://www.aliyun.com/product/acr.

5. Container Orchestration

In enterprise-level production environments, Docker containers often need to be used in conjunction with container orchestration tools like Kubernetes or Docker Swarm. Container orchestration involves a lot of content, and interested individuals can refer to the following materials: