Detailed steps for deploying the golang project in docker

  • 2020-06-12 09:20:24
  • OfStack

Understand Docker

Docker helps you create a single 1 deployable "unit" for your application. Such units, also known as containers, contain the 1 cut required by the application. Examples include code (or base 2 files), runtime, system tools, and system library files. Packaging all of these requirements into a single 1 unit ensures that the same environment is provided wherever the application is deployed. This technique also helps you maintain a fully 1 level development and production environment, which is often difficult to track.

Once the setup is complete, container creation and deployment will be automated. This in and of itself will avoid series 1 problems. Most of these problems are due to out-of-sync files, or differences between development and production environments. Docker can solve these problems.

The benefits of using Docker during development

Some of the benefits of using Docker in your development efforts include:

All team members share a standard development environment, Update the dependent components centrally, using the same container anywhere, The exact same environment can be used from development to production, and It is easier to fix potential problems that can only be encountered in a production environment.

Golang supports cross-compilation to generate executables on one platform for another

1. Cross compilation

Compile Linux 64-bit executables under Windows

Execute in the root directory of the project:

GOOS: Operating system of target platform (darwin, freebsd, linux, windows)


set GOOS=linux

GOARCH: Architecture of the target platform (386, amd64, arm)


set GOARCH=amd64

compile


go build .

2. Package the image

Dockerfile(under root)


FROM hub.skyinno.com/common/alpine:latest
MAINTAINER FAE Config Server "fae@fiberhome.com"
ADD mars /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/mars"]
FROM: Set the base image for the following instructions. A valid Dockerfile file must have FROM as the first non-comment instruction MAINTAINER: Sets the Author field for the generated image ADD: Copy new files, directories, or remote files URL and add them to the container's file system path ENTRYPOINT: ENTRYPOINT allows configuration containers to run as executable files

Enter the docker environment under the following directory


set DOCKER_HOST=tcp://10.110.200.29:5555

Build the mirror


docker build -t hub.skyinno.com/fcae/mars .

//  Check to see if the image was built successfully 
docker images

Start the service


docker run -i -d --name=mars -p 8011:8011 hub.skyinno.com/fcae/mars
--name: Specify the service name -ES79en: Set the service's external exposure port

Check to see if the service started successfully


docker ps -a

conclusion


Related articles: