Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy and use Go in Docker

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to deploy and use Go in Docker". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to deploy and use Go in Docker.

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. Go was developed by Robert Griesemer, Rob Pike and Ken Thompson in late 2007, and later joined Ian Lance Taylor, Russ Cox and others. Finally, it was open source in November 2009 and released a stable version of Go 1 in early 2012. Now the development of Go is completely open and has an active community.

Create a "Hello World" web application with Go language

First we create a directory for the Go application that displays "Hello World" in the browser. Create a web-app directory and make it the current directory. Enter the web-app application directory and edit a file named main.go.

Root@demohost:~# mkdir web-approot@demohost:~# cd web-app/root@demohost:~/web-app# vim.tiny main.gopackage mainimport ("fmt"net/http") func handler (w http.ResponseWriter, r * http.Request) {fmt.Fprintf (w, "Hello% s", r.URL.Path [1:])} func main () {http.HandleFunc ("/ World", handler) http.ListenAndServe (": 8080", nil)}

Run the "Hello World" Go program above using the following command. Type the http://127.0.0.1:8080/World test in the browser and you will see "Hello World" in the browser.

Root@demohost:~/web-app# PORT=8080 go run main.go

The next step is to containerize the above application in docker. So we will create a dockerfile file that tells docker how to containerize our web application.

Root@demohost:~/web-app# vim.tiny Dockerfile# gets the latest golang docker image FROM golang:latest# creates a directory inside the container to store our web application, and then makes it the working directory. RUN mkdir-p / go/src/web-appWORKDIR / go/src/web-app# copy the web-app directory to the container COPY. / go/src/web-app # download and install the third party relies on the container RUN go-wrapper download RUN go-wrapper install# to set the PORT environment variable ENV PORT 808 to expose port 8080 to the host, so that the external network can access your application EXPOSE 808 tell Docker to start the commands CMD ["go-wrapper", "run"] run by the container to build / run the container

Use the following command to build your Go web-app, and you will get confirmation after a successful build.

Root@demohost:~/web-app# docker build-- rm-t web-app .Sending build context to Docker daemon 3.584 kBStep 1: FROM golang:latestlatest: Pulling from library/golang386a066cd84a: Already exists75ea84187083: Pull complete88b459c9f665: Pull completea31e17eb9485: Pull complete1b272d7ab8a4: Pull completeeca636a985c1: Pull complete08158782d330: Pull completeDigest: sha256:02718aef869a8b00d4a36883c82782b47fc01e774d0ac1afd434934d8ccfee8cStatus: Downloaded newer image for golang:latest--- > 9752d71739d2Step 2: RUN mkdir-p / go/src/web-app--- > Running in 9aef92fff9e8After-> 49936ff4f50cRemoving intermediate container 9aef92fff9e8Step 3: WORKDIR / go/src/web- App--- > Running in 58440a93534cMurray-> 0703574296ddRemoving intermediate container 58440a93534cStep 4: COPY. / go/src/web-app--- > 82be55bc8e9fRemoving intermediate container cae309ac7757Step 5: RUN go-wrapper download--- > Running in 6168e4e96ab1 + exec go get-v-dmurmuri-> 59664b190feeRemoving intermediate container 6168e4e96ab1Step 6: RUN go-wrapper install--- > Running in e56f093b6f03 + exec go install- vweb-app--- > 584cd410fdcdRemoving intermediate container e56f093b6f03Step 7: ENV PORT 8080luk-> Running in 298e2a415819 Muffin-> c87fd2b43977Removing intermediate container 298e2a415819Step 8: EXPOSE 8080Mutual-> Running in 4f639a3790a7Fue-> 291167229d6fRemoving intermediate container 4f639a3790a7Step 9: -> Running in 6cb6bc28e406 Murray-> b32ca91bdfe0Removing intermediate container 6cb6bc28e406Successfully built b32ca91bdfe0

Now you can run our web-app and execute the following command.

Root@demohost:~/web-app# docker run-p 8080 test 8080-- name= "test"-d web-app 7644606b9af28a3ef1befd926f216f3058f500ffad44522c1d4756c576cfa85b

Go to http://localhost:8080/World and browse your web app. You have successfully containerized a repeatable / deterministic Go web application. Use the following command to start, stop, and check the status of the container.

# list all containers root@demohost:~/ docker ps-aversions # use id to start container root@demohost:~/ docker start CONTAINER_ID_OF_WEB_APP### and use id to stop container root@demohost:~/ docker stop CONTAINER_ID_OF_WEB_APP to rebuild the image

Suppose you are developing a web application and changing the code. Now to see the results after updating the code, you need to regenerate the docker image, stop the old image, and run the new image, and do so every time you change the code. To automate this process, we will use docker volumes to share a directory between the host and the container. This means that you don't have to rebuild the image to make changes in the container. How does the container detect if you have made changes to the source code of the web program? The answer is that there is a good tool called "Gin" called https://github.com/codegangsta/gin, which detects whether any changes have been made to the source code, then rebuilds the image / binaries and runs the updated code process in the container.

To automate this process, we will edit Dockerfile and install Gin to execute it as a portal command. We will open port 3030 (Gin proxy) instead of 8080. The Gin agent forwards traffic to port 8080 of the web program.

Root@demohost:~/web-app# vim.tiny Dockerfile# gets the latest golang docker image FROM golang:latest# creates a directory inside the container to store our web application, and then calls it the working directory. RUN mkdir-p / go/src/web-appWORKDIR / go/src/web-app# copy the web program to the container COPY. / go/src/web-app# download and install the third party depends on the container RUN go get github.com/codegangsta/ginRUN go-wrapper downloadRUN go-wrapper install# to set the PORT environment variable ENV PORT 808 to expose port 8080 to the host, so that the external network can access your application EXPOSE 303 when starting the container, run Gin CMD gin run# to tell Docker to start the container to run the command CMD ["go-wrapper", "run"]

Now build the image and start the container:

Root@demohost:~/web-app# docker build-rm-t web-app.

We will run docker from the root of the current web program and link CWD (the current working directory) to the application directory in the container through the exposed port 3030.

Root@demohost:~/web-app# docker run-p 3030 test 3030-v `pwd`: / go/src/web-app-- name= "test"-d web-app

Open http://localhost:3030/World and you can see your web program. Now if you change any code, it will be reflected in your browser after the browser is refreshed.

Thank you for reading, the above is the content of "how to deploy and use Go in Docker". After the study of this article, I believe you have a deeper understanding of how to deploy and use Go in Docker. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report