DEV Community

MANOJ AP
MANOJ AP

Posted on • Updated on

How to Containerize Golang app with Docker

To containerize a Golang app is similar to any other project. We need to use the base image for golandg and build the app.

Install Docker desktop , if you are on windows and create an account first. Then in the project create Dockerfile ( no extension required) with following command.

FROM  golang:1.18beta2-bullseye
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN go build -o main .
CMD ["/app/main"]
Enter fullscreen mode Exit fullscreen mode

Save the file in root of the project

Building the Docker Image

Go to the terminal and CD into the project folder and build the image. Docker image can be used to run different process.

docker build -t my-go-app . 
Enter fullscreen mode Exit fullscreen mode

try to list image using docker images command.

docker images 
Enter fullscreen mode Exit fullscreen mode

Using the image

To use the image we created with run command.

docker run -d -p 8080:8080 --name test1 my-go-app
Enter fullscreen mode Exit fullscreen mode

The above command will run the image in detached mode, leaving console reedy for another execution.

It also exposed to port internal 8080:8080. Go to the browser and try localhost:8080 and it should have working.

Stopping, Starting and removing containers

Using docker stop <container-name/id>. Once it stopped can delete using docker rm container-name/id command.

Need to start again ? use the docker start command.

Wanna know running processes/ containers ? Try docker ps.

Here is more guides on

Top comments (0)