Containerizing a Go Web Application with Docker
Containerization has become a popular packaging method, for distributing and running applications in a consistent and isolated environment. In this bl
Introduction
The GoWebApp is a simple web application built with the Go (Golang) programming language. It demonstrates basic web development concepts such as routing, serving static files, and handling HTTP requests.
Prerequisites
Before we begin, ensure that you have Docker installed on your machine. You can download and install Docker from here
Step 1: Cloning the Repository
First, clone the GoWebApp repository to your local machine using the following command:
git clone https://github.com/helloabhii/GoWebApp.git
cd GoWebApp
tree .
.
├── Dockerfile
├── go.mod
├── gopher.png
├── index.html
├── main.go
└── README.md
0 directories, 6 files
Step 2: Create a Dockerfile
To containerize our Go web application, we need to create a Dockerfile. The Dockerfile contains instructions for building a Docker image.
# Use the official Golang Alpine image as the base image for the builder stage
FROM golang:alpine AS builder
# Set the working directory inside the container
WORKDIR /app
# Install Git, which is needed to fetch dependencies
RUN apk add --no-cache git
# Clone the GoWebApp repository from GitHub into the container
RUN git clone https://github.com/helloabhii/GoWebApp .
COPY go.mod .
# Build the GoWebApp binary inside the container
RUN go build -o main .
###
# Define the base image for the final image
FROM alpine:latest
WORKDIR /app
# Copy the built binary from the builder stage to the final image
COPY --from=builder /app/main .
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the GoWebApp binary when the container starts
CMD ["./main"]
Now, let's break down each step:
1. Build Stage (golang:alpine):
Sets up a temporary build environment with Golang and Git.
Clones the Go application code (if using a remote repository).
Copies the
go.mod
file (specifies app dependencies).Builds the Go application binary (main).
2. Final Image (alpine:latest):
Uses a lightweight Alpine image for the final container.
Copies the built binary (main) from the build stage.
Exposes port 8080 (for external connections).
Runs the Go application (main) when the container starts.Step 3: Building the Docker Image
Now, let's build the Docker image for our Go web application:
docker build -t gowebapp .
This command will create a Docker image named gowebapp
based on the Dockerfile in the current directory.
Step 4: Running the Docker Container
Once we have built the Docker image, we can run a Docker container based on it:
docker run -d -p 8080:8080 gowebapp
This command will start a Docker container running our Go web application, exposing port 8080 on our localhost
Step 5: Accessing the Application
Open your web browser and navigate to http://localhost:8080 to access the GoWebApp.
Step 6: Pushing the Docker Image to Docker Hub
To make our Docker image accessible to others, we can push it to Docker Hub.
Before pushing the Docker image to Docker Hub, you need to authenticate yourself by logging in to Docker using the docker login
command. Here's how you can do it:
docker login
Tag the Docker Image: Before pushing the image, you need to tag it with your Docker Hub username and the repository name. You can do this using the docker tag
command. docker tag gowebapp helloabhii/gowebapp
docker tag gowebapp helloabhii/gowebapp
Replace helloabhii
with your Docker Hub username. Then, push the tagged Docker image to Docker Hub:
docker push helloabhii/gowebapp
This command will push the tagged Docker image to your Docker Hub repository under the specified username.
Now your GoWebApp Docker image is available on Docker Hub for others to use.
After successfully pushing the image, it will be available on Docker Hub at the following URL: https://hub.docker.com/repository/docker/helloabhii/gowebapp/general.
Now others can pull the image from Docker Hub using docker pull helloabhii/gowebapp
and run it in their environments.
Conclusion
In this blog, we've covered how to containerize a Go web app using Docker. Following these steps, you can package, run, and share your app via Docker Hub, streamlining deployment and ensuring consistency. Now, you can explore further customization and optimization options.
Additionally, feel free to reach out to me on LinkedIn, Docker Hub, Twitter, Hashnode, Reddit, or view more of my projects on GitHub.
Happy containerizing!