DEV Community

Cover image for Dockerise a Golang Application
Abhishek Kumar Verma
Abhishek Kumar Verma

Posted on

Dockerise a Golang Application

How can you call yourself a Golang developer without knowing how to Dockerize a Go application?

Yes, I know that feels very shameful, especially when working among your fellow teammates who are skilled developers.

Mention not, let's give them a good show!

Prerequisites:

  1. Go setup: You can download & install it from Go Official website.
  2. Docker: Download and install from Docker's Official Website.

meme image

Step 1: Create a Golang App or use my sample GoLang App by cloning it

git clone https://github.com/AbhishekCS3459/Docker_Blog_Series
cd Docker_Blog_Series/Dockerise_Go
go mod tidy
Enter fullscreen mode Exit fullscreen mode

Step 2 (optional): Run the Go application using the below command:

go run main.go
Enter fullscreen mode Exit fullscreen mode

Expected Output:

vscode output

Simple Dockerfile for a go application:

code image

Step 3: Build the Docker image using the following command:

docker build YOUR_IMAGE_NAME .
Enter fullscreen mode Exit fullscreen mode

Note: . represents that you are running the above command in the current directory where your Go application exists.

Step 4: Check whether your image has been built or not by running the below command:

docker images
Enter fullscreen mode Exit fullscreen mode

Step 5: Run the container using the following command:

docker run -it -p 8080:8080 YOUR_IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

Note: Here, -it is a flag to run the container in interactive mode, and -p is for mapping the container port with the external port.

Deploy the Image to Docker Hub

Docker Hub is a container registry built for developers and open-source contributors to find, use, and share their container images. With Hub, developers can host public repos that can be used for free, or private repos for teams and enterprises.

Dockerhub Image

Step 1: Log in to Docker Hub using the command:

docker login
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter your Docker Hub username and password.

Step 2: Tag your Docker image with your Docker Hub repository name.

Replace YOUR_DOCKERHUB_USERNAME and YOUR_IMAGE_NAME with your Docker Hub username and the name of your image:

docker tag YOUR_IMAGE_NAME YOUR_DOCKERHUB_USERNAME/YOUR_IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

Step 3: Push Your Image to Docker Hub

Push the tagged image to Docker Hub:

docker push YOUR_DOCKERHUB_USERNAME/YOUR_IMAGE_NAME
Enter fullscreen mode Exit fullscreen mode

You can now see your image on Docker Hub and share it with others!

docker file image

If you want to ask, ping me below.

Connect with me on Linkedin:
linkedin/abhishekverman.
Further Reading: Dockerize a Nodejs Application

Top comments (0)