DEV Community

joy
joy

Posted on

Containerizing your Go application by using Docker

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.
2.A Github repository to save your project files
3.Golang installed in your device
4.VsCode Editor

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

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.

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

Download of Go dependencies.

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

Go build Docker command

Step 5: After building the application, you exit the container.

Exit

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

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

Docker run

_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 article.

Top comments (0)