Linux Format

CREATING A DOCKER IMAGE

-

In recent years it’s become common for services to be deployed in Kubernetes as Docker images, so it would be handy to learn how to create a Docker image for the RESTful server developed in this tutorial. The process is easy, provided that you have the appropriat­e Dockerfile. The screenshot (below) shows the contents of the Dockerfile used for creating the Docker image for the RESTful server that uses gorilla/mux developed in this tutorial.

The key here is to put your source code under $GOROOT (the actual value doesn’t matter because this depends on the Docker image) to be able to use Go modules and create the binary executable inside the Docker image that you’re using. To make the final Docker image smaller in size, the building of it requires two stages. The first stage is about building the executable. Go binaries are statically linked, so once built they don’t require any shared libraries. You then place that executable file to another, smaller Docker image. The second step occurs with the COPY --from=builder /pro/server /pro/server command. Finally, remember to make available the TCP port that your server listens to ( EXPOSE 1234 ).

After having the appropriat­e Dockerfile, run docker build -f Dockerfile -t restlxf . to create the Docker image. Running docker images should show a new Docker image named restlxf. You can run the restlxf Docker image as docker run -it -p 1234:1234 restlxf and interact with it. Just make sure that you’re using the correct parameters in the -p flag. In this case, we tell Docker that the server listens to port 1234 internally and externally.

Newspapers in English

Newspapers from Australia