Dockerise a Golang Application

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:

Go setup: You can download & install it from Go Official website.

Docker: Download and install from Docker’s Official Website.

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

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

go run main.go

Expected Output:

Simple Dockerfile for a go application:

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

docker build YOUR_IMAGE_NAME .

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

Step 5: Run the container using the following command:

docker run -it -p 8080:8080 YOUR_IMAGE_NAME

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.

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

docker login

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

Step 3: Push Your Image to Docker Hub

Push the tagged image to Docker Hub:

docker push YOUR_DOCKERHUB_USERNAME/YOUR_IMAGE_NAME

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

If you want to ask, ping me below.

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

Please follow and like us:
Pin Share