How to set up Go in Docker and deploy the application

  • 2020-06-07 05:50:26
  • OfStack

Hi, in this tutorial, we'll learn how to deploy golang web applications using docker. As you probably already know, docker is written entirely in golang due to its high performance and reliability. Before we go into detail, make sure you have both docker and golang installed and have a basic understanding of them.

About docker

Docker is an open source program that bundles applications and their complete dependencies into a container and shares the same Linux kernel with the host. On the other hand, hypervisor based virtualization operating system containers like VMware provide a high level of isolation and security because communication between the client and host is achieved through hypervisor and they do not share kernel space. But hardware emulation also led to performance overhead, so container virtualization was born to provide a lightweight virtual environment that groups and isolates a set of processes and resources from the host and other containers, so that processes inside the container cannot see processes or resources outside the container.

Create 1 "Hello World" web application in Go language

First we create a directory for the Go application that displays "Hello World" in the browser. Create an ES32en-ES33en directory and make it the current directory. Go to the ES34en-ES35en application directory and edit a file named main.go.


root@demohost:~# mkdir web-app
root@demohost:~# cd web-app/
root@demohost:~/web-app# vim.tiny main.go
package main
import (
  "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)
}

Use the following command to run the "Hello World" Go program above. In the browser input http: / / 127.0.0.1:8080 / World test, you will see in the browser "Hello World".


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

The next step is to containerize the above application in docker. So we'll create an dockerfile file that tells docker how to container our web application.


root@demohost:~/web-app# vim.tiny Dockerfile
#  Get up to date  golang docker  The mirror 
FROM golang:latest
#  Create inside the container 1 A directory to store our  web  Apply, and then make it a working directory. 
RUN mkdir -p /go/src/web-app
WORKDIR /go/src/web-app
#  copy  web-app  Directory to container 
COPY . /go/src/web-app
#  Download and install the code 3 The side depends on the container 
RUN go-wrapper download
RUN go-wrapper install
#  Set up the  PORT  The environment variable 
ENV PORT 8080
#  Expose the host  8080  Port, so that external networks can access your application 
EXPOSE 8080
#  tell  Docker  Start the command for the container to run 
CMD ["go-wrapper", "run"]

Build/run the container

Use the following command to build your Go ES62en-ES63en and you will be confirmed upon a successful build.


root@demohost:~/web-app# docker build --rm -t web-app .
Sending build context to Docker daemon 3.584 kB
Step 1 : FROM golang:latest
latest: Pulling from library/golang
386a066cd84a: Already exists
75ea84187083: Pull complete
88b459c9f665: Pull complete
a31e17eb9485: Pull complete
1b272d7ab8a4: Pull complete
eca636a985c1: Pull complete
08158782d330: Pull complete
Digest: sha256:02718aef869a8b00d4a36883c82782b47fc01e774d0ac1afd434934d8ccfee8c
Status: Downloaded newer image for golang:latest
---> 9752d71739d2
Step 2 : RUN mkdir -p /go/src/web-app
---> Running in 9aef92fff9e8
---> 49936ff4f50c
Removing intermediate container 9aef92fff9e8
Step 3 : WORKDIR /go/src/web-app
---> Running in 58440a93534c
---> 0703574296dd
Removing intermediate container 58440a93534c
Step 4 : COPY . /go/src/web-app
---> 82be55bc8e9f
Removing intermediate container cae309ac7757
Step 5 : RUN go-wrapper download
---> Running in 6168e4e96ab1
+ exec go get -v -d
---> 59664b190fee
Removing intermediate container 6168e4e96ab1
Step 6 : RUN go-wrapper install
---> Running in e56f093b6f03
+ exec go install -v
web-app
---> 584cd410fdcd
Removing intermediate container e56f093b6f03
Step 7 : ENV PORT 8080
---> Running in 298e2a415819
---> c87fd2b43977
Removing intermediate container 298e2a415819
Step 8 : EXPOSE 8080
---> Running in 4f639a3790a7
---> 291167229d6f
Removing intermediate container 4f639a3790a7
Step 9 : CMD go-wrapper run
---> Running in 6cb6bc28e406
---> b32ca91bdfe0
Removing intermediate container 6cb6bc28e406
Successfully built b32ca91bdfe0

You are now ready to run our ES67en-ES68en and execute the following command.


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

Enter http: / / localhost: 8080 / World browse your web application. You have successfully containable 1 repeatable/deterministic Go web application. Use the following command to start, stop, and check the state of the container.


###  List all containers 
root@demohost:~/ docker ps -a
###  use  id  Start the container 
root@demohost:~/ docker start CONTAINER_ID_OF_WEB_APP
###  use  id  Stop the container 
root@demohost:~/ docker stop CONTAINER_ID_OF_WEB_APP

Rebuild the image

Suppose you are developing the 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 this every time you change the code. To automate this process, we will use the docker volume 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 any changes to the source code of the web program? The answer is a good tool called "Gin https: / / github com/codegangsta/gin, it can detect any changes on the source, then reconstruction image / 2 hexadecimal file and in the process of container running the updated code.

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


root@demohost:~/web-app# vim.tiny Dockerfile
#  Get up to date  golang docker  The mirror 
FROM golang:latest
#  Create inside the container 1 A directory to store our  web  Apply, and then make it called the working directory. 
RUN mkdir -p /go/src/web-app
WORKDIR /go/src/web-app
#  copy  web  Program into the container 
COPY . /go/src/web-app
#  Download and install the code 3 The side depends on the container 
RUN go get github.com/codegangsta/gin
RUN go-wrapper download
RUN go-wrapper install
#  Set up the  PORT  The environment variable 
ENV PORT 8080
#  Expose the host  8080  Port, so that external networks can access your application 
EXPOSE 3030
#  Run when the container is started  Gin
CMD gin run
#  tell  Docker  Start the command for the container to run 
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 in the root directory of the current web program and link CWD (the current working directory) to the application directory in the container via exposed port 3030.


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

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

conclusion

That's it! Our Go web application is running in the Ubuntu 16.04 Docker container! You can extend current web applications by using the Go framework to rapidly develop API, web applications, and back-end services.


Related articles: