Deploy go projects based on Docker images (detailed examples)
This article brings you relevant knowledge about deploying go projects based on docker images, including issues related to writing GoLang web programs and compiling packages under Linux. I hope it will be helpful to everyone.
Dependency knowledge
- Go cross-compilation basics
- Docker basics
- Dockerfile custom image basics
- Basics of writing docker-compose orchestration files
Of course, you can follow this step to complete the deployment even if you don’t know it at all. However, if there is a small problem in the middle, you will not know how to solve it. Of course you can also leave a message.
I developed and tested on a mac environment. If you are on windows, there may be a little difference, but there shouldn't be any big problems.
1. Dependent environment
- Docker
2. Writing a GoLang web program
I will write one here The simplest hello world program, the listening port is port 80.
Create a new main.go
file with the following content:
package mainimport ( "fmt" "log" "net/http")func sayHello(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "hello world")}func main() { http.HandleFunc("/", sayHello)//注册URI路径与相应的处理函数 log.Println("【默认项目】服务启动成功 监听端口 80") er := http.ListenAndServe("0.0.0.0:80", nil) if er != nil { log.Fatal("ListenAndServe: ", er) }}
3. Compile it into a program package under linux
I developed it on a mac and need Using go cross-compilation, if you are not familiar with cross-compilation, you can check the documentation, or directly copy my command below to compile.
We want to run it in Docker
. The basic golang
image is running, so it needs to be compiled into a program compatible with the i386
processor.
sudo env GOOS=linux GOARCH=386 go build main.go
After this compilation is completed, there will be one more main
program locally. Just leave it alone for the time being.
4. Use Dockerfile
to customize the image of our go program
Create a new folder, create a new Dockerfile
file in it, and then create a new# in it ##app,
script two files. Put the
main program in the previous step into the
app folder, and create a new
build.sh script file in
script. The content of the file Don’t worry about it for now, I’ll talk about it later.
The specific file structure is as follows.
. ├── Dockerfile ├── app │ └── main └── script └── build.sh
Dockerfile file. I will code the content first:
FROM golang MAINTAINER 青羽 WORKDIR /go/src/COPY . .EXPOSE 80CMD ["/bin/bash", "/go/src/script/build.sh"]
- FROM
is the image from which it is integrated. Our go program officially provides an image like
golang, which we can use directly.
- MAINTAINER
is to maintain this name.
- WORKDIR
Working directory.
- COPY
This is a copy command that copies all local files to the working directory.
- EXPOSE
This is the port developed by the other party. By default, I open port 80. This can be modified according to the actual situation.
- CMD
Execute a parameter with The command I wrote like this is to execute the script
script/build.shwhen the image starts. This script contains the command to start the go program.
Here I paste the content:
#!/usr/bin/env bash cd /go/src/app/ && ./main
Docker, I will paste the command.
docker build -t go-web .
- This command is executed. If the
- golang
image is not available locally, it will first go to the official image library to pull the image and then compile it. We can just wait for him quietly. .
- go-web
This parameter is the name of your last compiled image. You can modify it at will, or add a version number, such as:
go-web:v1.
go-web## in your local image. # mirror. You can use docker images
to query:
6. Write the
file
to This is our last step. If we use the
go-web we just compiled to run our go program: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>version: &#39;2&#39;networks:
basic:services:
world:
container_name: world
image: go-web
ports:
- "8099:80"
volumes:
- ./app/go/world:/go/src/app:rw
networks:
- basic</pre><div class="contentsignin">Copy after login</div></div>
At this point our orchestration file has been written, and now we only need We need to use
to start our orchestration file. The startup command is as follows: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>docker-compose -f docker-compose.yml up -d world</pre><div class="contentsignin">Copy after login</div></div>
If the following prompt is output, it means the startup is successful.
Creating world ... done
After the startup is successful, you can use
docker ps
to check whether the startup is successful.
Now visit
http://127.0.0.1:8099 to access our go program. Recommended learning: "
The above is the detailed content of Deploy go projects based on Docker images (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).
