Running golang application on Docker
This article covers a basic example of how to deploy an applicaiton written in golang in Docker container.
Assumptions/ Prerequisites
- You have golang installed on your machine.
- You have Docker cli, runtime installed on your machine.
- Your machine is connected to internet.
Steps to follow
1. Writing a simple golang application
Below is a very simple golang application with only 1 file main.go
and no dependencies.
This program will start an http server which will listen on port 8080. Its a simple HelloWorld application.
Create a new folder for example golangapp
, and create a file inside it with name main.go
. Put the following code in it.
1package main
2
3import (
4 "fmt"
5 "net/http"
6)
7
8func helloWorld(w http.ResponseWriter, req *http.Request) {
9 fmt.Fprintf(w, "Hello World\n")
10}
11
12func main() {
13 http.HandleFunc("/helloworld", helloWorld)
14 http.ListenAndServe(":8080", nil)
15}
2. Building the application and putting it in an Docker Image using MultiStage Dockerfile
Create another file with name Dockerfile
inside the folder from above step. Put following lines and save the file.
1FROM golang:alpine AS ApplicationBuilder
2# Defines the work directory, and if the specified path is non-existing it is also created.
3WORKDIR /tmp/golangapp
4# Copies the main.go file from your local to the Work directory
5COPY main.go main.go
6# Set the env var to create a static build binary, this is important to run the binary in container with "scratch" base image
7ENV CGO_ENABLED=0
8# Executes the go build command
9RUN go build -o goapp
10
11# The base image for this image scratch
12FROM scratch
13# Copies binary from previous stage to this image
14COPY --from=ApplicationBuilder /tmp/golangapp/goapp /
15# putting the command in "[]" makes sure that shell is not used to execute the command
16# this is important for running app with "scratch" as base image
17CMD [ "/goapp" ]
I will try to explain the Dockerfile above briefly. The file above is multistage docker file.
The First stage is the build step in this case.
1st line with FROM
defines the base image to be used for the 1st stage and the AS
is an optional instruction to specify the name of the stage.
2nd FROM
defines the start of 2nd stage. In this stage we are just copying the binary built in 1st stage.
Now in your terminal go inside the folder and run following command
1docker build -t someimagename .
Here the -t
flag is used to provide image tag name. the .
specifies the path where the Dockerfile and all the relevant files are located. .
can be replaced by absolute or relative path as well.
3. Starting the application inside the container
To start the container run the following command
1docker run -p 9090:8080 someimagename
Here the -p
flag is used to define mapping of port on your localmachine to the port in container where application is listening.
Now you can access your appicaton on web browser using http://localhost:9090/helloworld
. You should see the message "Hello World" if every thing worked out.