Containerizing your Go application using Docker
Tags: devops, docker, containerapps, go
Published: 2024-01-16 09:52:11

#### Introduction
This article will explore how to containerize a simple golang web server application using Docker . You can use any Operating system to run your application,We use Ubuntu to build this application. In this article we use Docker commands as opposed to using a Dockerfile.

####Prerequisites

1.[Docker installed on your device.](https://docs.docker.com/desktop/install/ubuntu/)
2.[A Github repository to save your project files](https://github.com/)
3.[Golang installed in your device](https://itslinuxfoss.com/install-go-on-ubuntu-22-04/#google_vignette)
4.[VsCode Editor](https://code.visualstudio.com/docs/setup/linux#_installation)

#### What is Docker?
Docker is a framework used by developers to containerize software applications. Containerizing an application is when a bunch of code and all its components, like libraries and files, are executed within a hosted environment.

When a person dockerizes their application, they can access and run it on any platform supporting docker. The application run usually has many components like files and libraries; which get stored in a compressed file called a docker image.

The following steps are used to containerize an application using Docker.
**Note:** We will not be using a Dockerfile for this article.

#### Step 1:Cd into the directory that contains your code and libraries.

You will then run the following command in your terminal while in your project directory.

![Docker command for building image ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qtwa2dz6d04rnup7ajt.png)

We used alpine as the base image for docker because of its minimal size and we wanted a small image for the application. But if someone wants a larger image of better performance,We would suggest using NGINX as an image base.

####Step 2:Install the necessary build tools.

![Download of build tools.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqsn53jwskib35qyhorn.png)

#### Step 3: Copy and install go dependencies for your application.

![Download of Go dependencies.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/joggchtu5vfrp1314h9n.png)

#### Step 4 : Run and build the Go application using docker build -o name-of-your-application.

![Go build Docker command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nnrcl03dqqo3m74fk6u4.png)

#### Step 5: After building the application, you exit the container.
 
![Exit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx4jhu3rdgj7qw5taqjn.png)

#### Step 6: Commit your changes to a new image.
You can use the container ID or name of the application to commit the changes.

![Docker commit changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ucfmrcjexf7eoeropa95.png)

#### Step 7: Finally run your custom image with the following command.

![Docker run](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/buccpjrph474eaczzjre.png)


**Note**: As much as this is an interactive way of containerizing an application. It is highly recommended to use a Dockerfile to containerize an application.

 

#### Conclusion
 In conclusion, we were able to containerize the Go application using Docker. Docker is an amazing tool for containerizing applications such that you can access your application anywhere as long as you have the images stored in a container registry reachable when run with Docker. If you plan on seeking to explore how to do this using a Dockerfile, I would suggest following [this](https://kodekloud.com/blog/how-to-build-a-docker-image/) article.