📜 ⬆️ ⬇️

Deploying Go Servers with Docker

Introduction


This week, the Docker team announced the official base images for Go and other major languages, giving developers a simple and reliable way to create containers for Go programs.

In this article, we will go step by step to create a Docker container for a simple web application on Go, as well as deploy this container to Google Compute Engine. If you are not familiar with Docker yet, you should read the Understanding Docker article before reading further.

Demo app


For our example, we will use the outyet program from the Go sample repository , which is a simple web server that tells you whether the next version of Go was released. This program has no dependencies other than the standard library and does not require any additional data files at run time.

Use "go get" to download and install outyet in your desktop environment:
$ go get github.com/golang/example/outyet 

Create a Dockerfile


Create a file called Dockerfile in the outyet directory with the following contents:
 # Start from a Debian image with the latest version of Go installed # and a workspace (GOPATH) configured at /go. FROM golang # Copy the local package files to the container's workspace. ADD . /go/src/github.com/golang/example/outyet # Build the outyet command inside the container. # (You may fetch or manage dependencies here, # either manually or with a tool like "godep".) RUN go install github.com/golang/example/outyet # Run the outyet command by default when the container starts. ENTRYPOINT /go/bin/outyet # Document that the service listens on port 8080. EXPOSE 8080 

This Dockerfile indicates how to build a container that will execute outyet, starting with basic dependencies (Debian systems with Go installed, the official golang image ). and ending with the outyet source codes, building it, and finally launching it.
')
The steps ADD, RUN, and ENTRYPOINT are common to any Go project. There is an easier option: onbuild from the golang image, which automatically copies the source code of the package, loads the dependencies of the application, builds the program, and sets it up for launch at startup.

If you use the onbuild option, the Dockerfile becomes much simpler:
 FROM golang:onbuild EXPOSE 8080 

Build and Run the Image


Call the docker from the application directory to collect the image from the Dockerfile:
 $ docker build -t outyet . 

In this case, the base image of golang Docker Hub will be taken, the contents of the application will be copied into it. The final image will be tagged with an outyet.

To start the container with the created image, run the command:
 $ docker run --publish 6060:8080 --name test --rm outyet 

The --publish flag tells Docker to port 8080 from a 6060 container on the host system.
The --name flag gives our container the specified name to facilitate subsequent work with it.
The --rm flag tells you to remove the container when it is finished.

Launch the container and open the localhost : 6060 / address in the browser. You should see something like this:
image
(If Docker is running on another machine (or in a virtual machine), you must replace localhost with the address of this machine. If you are using boot2docker on OS X or Windows, you can find this address using boot2docker ip .

Now that we have verified that the image is working, let's close the working container from another terminal window:
 $ docker stop test 

Creating a Docker Hub repository


Docker Hub , the container repository from which we previously took the golang image, has an Automatic Build feature that can build an image from the repositories on GitHub or BitBucket.

After Dockerfile commits to the repository and creates an automatic build for it, anyone with Docker installed can download and run our image with just one command. (We will see how useful this is in the next section.)

To set up an automatic build, you need to commit the Dockerfile to your repository on GitHub or BitBucket, create an account on the Docker Hub, and follow all instructions for creating an automated build .

When everything is ready, you can launch your container using the name of the automated assembly:
 $ docker run goexample/outyet 

(Replace goexample / outyet with the name of the automated assembly you created.)

Deploy Container to Google Compute Engine


Google provides containerized Google Compute Engine images that make it easy to pick up a virtual machine that will work with an arbitrary Docker container. On startup, the running program reads a configuration file that determines which container to launch, obtains an image of the container, and launches it.

Create a file containers.yaml , which determines which Docker image you need to run and which ports to forward:
 version: v1beta2 containers: - name: outyet image: goexample/outyet ports: - name: http hostPort: 80 containerPort: 8080 

Please note that we are forwarding the port of the 8080 container to the external 80 port, the default port for serving HTTP traffic. And, again, you should replace goexample / outyet with the name of your automatic build.

Use gcloud to create a virtual machine instance for the container:
 $ gcloud compute instances create outyet \ --image container-vm-v20140826 \ --image-project google-containers \ --metadata-from-file google-container-manifest=containers.yaml \ --tags http-server \ --zone us-central1-a \ --machine-type f1-micro 

The first argument (outyet) specifies the name of the instance that will later be conveniently used for administrative purposes.

The --image and --image-project flags define a special container-optimized system image (just copy these flags verbatim).
The flag --metadata-from-file passes the file containers.yaml to the virtual machine.
The --tags flag marks the VM instance as an HTTP server, telling the firewall to open port 80.
Flags --zone and --machine-type flags indicate the zone in which you want to run the VM and the type of machine. (To see a list of machine types and zones, run gcloud compute machine-types )

Upon completion, the gcloud command should display information about the created instance. In the output, find the networkInterfaces section, the IP address of the VM instance will be listed there. After a couple of minutes, you should be able to open this IP through a browser and see: “Has Go 1.4 been released yet?”

(To see what is happening in the virtual machine, you can connect to it via SSH using gcloud compute ssh outyet . After going there, try running sudo docker ps to see which Docker containers are working)

Additional Information


What we have done is just the tip of the iceberg and you can do a lot more with a bunch of Go, Docker, and Google Compute Engine.

Andrew Gerrand

From the translator: all comments on the accuracy of translation, spelling and style, please pass through private messages. I will be glad to fix it.

Source: https://habr.com/ru/post/238473/


All Articles