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.
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.
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) }}
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.
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"]
is the image from which it is integrated. Our go program officially provides an image like
golang, which we can use directly.
is to maintain this name.
Working directory.
This is a copy command that copies all local files to the working directory.
This is the port developed by the other party. By default, I open port 80. This can be modified according to the actual situation.
Execute a parameter with The command I wrote like this is to execute the script
script/build.sh when 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 .
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. .
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
docker-compose.yml
to This is our last step. If we use the we just compiled to run our go program: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:sql;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:sql;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!