Compile and run a Go App in a container — Part 1
In this post I want to compile and run an “hello-world” application written in GO without the need to install sdk on my computer. For that I will use docker.
I will create two images: the first one to build an hello-world application from go sources, the second one to run the app in a container.
Docker image to compile go sources
The Go application we want use is the classic “Hello World”, create a file named “hello-world.go” and write the following:
package main
import "fmt"
func main() {
fmt.Println("hello world")
}The program is very simple, it prints “hello world” on your screen where you run it.
The first step consists in creating an image to compile go application. We need to create a Dockerfile that downloads the go sdk and compile sources:
FROM ubuntu
ADD go1.6.2.linux-amd64.tar.gz /usr/local
ENV PATH /usr/local/go/bin:$PATH
VOLUME /src
WORKDIR /src
The image is based on latest Ubuntu (FROM Ubuntu).
The ADD command downloads and extracts the go sdk in the /usr/local/ directory
The ENV path command adds the go bin path to the current PATH environment variable.
The WORKDIR command sets the directory where next commands working and, finally, the VOLUME declaration will allow us to mount an host directory into the container.
To build the image run this docker command inside directory where you have saved the Dockerfile:
docker build -t compilego .
The -t parameter allows us to tag the image with a name that we will use for running.
Compiling hello-world
Ok, now we can compile go sources in order to create our hello-world application. From the directory where we have saved “hello-world.go”, run this docker command:
docker run -v $PWD:/src compilego go build hello-world.go
The -v parameter tells to Docker to mount actually directory ($PWD environment variable) in the directory /src into the container, in this way the container can see the go sources
compilego is the name of the image to run (the same name used in the -t parameter used in the build command)
Finally the “go build hello-world.go” is the command that container will run.
After ran the Docker run you should have a “hello-world” file created by container in your directory. That file is written for Linux so in order to run this application we need to build and run another container.
Running hello-world
Create a directory named running and inside it create the following Dockerfile:
FROM ubuntu COPY hello-world /app/hello-world WORKDIR /app ENTRYPOINT ["./hello-world"]
As the previous we are using an Ubuntu image as base image.
Then, with COPY command, we copy a hello-world file inside our container. The “hello-world” file must be located in the same directory of Dockerfile, then in order to build the image we need to copy the hello-world file created in the previous steps in the directory where we have created the Dockerfile.
Now we can create the image of our go application
docker build -t go-hello-world .
As in the previous build command we use -t parameter in order to named the image.
Ok, we can run our app by:
docker run go-hello-world
If all is gone well you should see:
hello-world
Good job!!
In the next part we see a more complicated example.
Originally published at www.gianlucatomasino.com on August 11, 2016.