⬆️ ⬇️

Configuring Dockerfile to create and run a Docker container with your Go program

You wrote a program (microservice) on Go, and you want to deploy it in a Docker container?

In this small note I will try to describe this process for beginners in the most compact and practical way.



In short, I will say this about Docker - it allows you to launch a container with a virtual operating system and your application hosted in it. And also, when you get bored, remove this container completely, without worrying that your application is somewhere in the dark.

Here, in fact, all the benefits of using docker containers for beginners :)



In this article, I first consider how to compile your program inside an image, at the stage of its assembly. This is the most obvious way for beginners. Then the creation of the image and the launch of the container from a pre-compiled program file is considered.


Just a few words about what docker works with. It stores images (images) of containers on the basis of which you can run as many instances of these containers as you like and configure them to route through the network (mapping of incoming ports, etc.). Images are collected on the basis of other images (it can be said that there is an inheritance of images) that are publicly available in the docker repository. For the Go program, the best ready-made choice would be the images golang: latest (based on debian) and golang: alpine (based on alpine linux). Other image names can be viewed here: hub.docker.com/_/golang



Building your program from source within a Docker image



This option is used, for example, when your main axis is windows or macos, and you are building a container for linux. In order not to rebuild the go compiler for other platforms on our machine, simply compile the sources in the target Docker image.

To build an image (image), you need to make a Dockerfile text file, without an extension, with the following contents in the source folder of your program:

')

#   FROM golang:latest # ,     RUN mkdir -p /go/src/app #   WORKDIR /go/src/app #        Docker         COPY . /go/src/app #    ,    docker RUN go-wrapper download #      RUN go-wrapper install #      ,        #-web -  ,     ,       #go-wrapper  set -x  ,    stderr         CMD ["go-wrapper", "run", "-web"] #      EXPOSE 8000 


A special feature of this file for Go is the presence of a special go-wrapper script in the basic golang image, which facilitates compiling, allowing you not to think about paths and file names.

In the folder with this file and your sources, run the command:

 docker build -t my-golang-app . 


The dot at the end is the current directory with the Docker file, don't forget to specify it.

my-golang-app - this will be the name of the image in which your program will be compiled.

After executing this command, you can see the presence of the successfully assembled image with the command

 docker images docker ps -a 


You can create and launch a container based on the collected image as follows:

 docker run -d --rm --name my-running-app my-golang-app 


my-running-app - this will be the name of the container being created based on the my-golang-app image.

Here you can also use the keys:

-d | --detach - id . ( == false)

-i | --interactive - ( STDIN , )

-t | --tty - , -i

-p | --publish=[] - . : ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort

-e | --env=[] - .

-v | --volume=[] -


For more details on the docker startup keys and the composition of the Dockerfile file, see here and here .



Running a compiled binary program file in a Docker container



It must be remembered that the file before being placed in the container must be correctly compiled for the desired image and architecture.



Create and run a container with a binary:

 #!/bin/bash #  CGO    alpine docker run --rm -v "$PWD":/go/src/myapp -w /go/src/myapp golang:alpine /bin/sh -c "apk add --update gcc musl-dev; go build -ldflags \"-s -w\" -a -o myapp_bin_name" 


Alternatively, we pack it in a container:

 # -*- Dockerfile -*- FROM alpine COPY myapp_bin_name /app/ RUN apk add --update tzdata openssl musl-dev ca-certificates && \ rm -rf /var/cache/apk/* && \ cp /usr/share/zoneinfo/Europe/Moscow /etc/localtime && \ echo "Europe/Moscow" > /etc/timezone && \ chmod +x /app/myapp_bin_name && \ mkdir -p /data WORKDIR /app VOLUME /data CMD /app/myapp_bin_name EXPOSE 8085 


Create an image:

 docker build -t mymegapp . 


Run:

 docker run -d --rm --name my-running-app my-golang-app 

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



All Articles