Table of Contents
- Introduction
- Prerequisites
- Setup
- Creating a Go Web Application
- Building a Docker Image
- Running the Docker Container
- Conclusion
Introduction
In this tutorial, you will learn how to use Docker to build and run a Go web application. Docker is a popular containerization platform that allows you to package your applications with all their dependencies into a standardized unit called a container. By using Docker, you can ensure that your Go web application runs consistently across different environments.
By the end of this tutorial, you will have a basic understanding of Docker and how to use it with a Go web application. You will learn how to create a Go web application, build a Docker image for it, and run the application inside a Docker container.
Prerequisites
Before starting this tutorial, you should have the following:
- Basic knowledge of the Go programming language
- Docker installed on your machine
- A code editor of your choice
Setup
-
Install Docker on your machine by following the official Docker installation guide for your operating system.
-
Verify the installation by running the following command in your terminal or command prompt:
``` docker version ``` This command should display the version information of Docker if the installation was successful.
Creating a Go Web Application
Let’s start by creating a simple Go web application. Open your code editor and create a new directory for your project. Inside the project directory, create a new file named main.go
and add the following code:
package main
import (
"fmt"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
}
func main() {
http.HandleFunc("/", helloHandler)
http.ListenAndServe(":8080", nil)
}
In this code, we define a helloHandler
function that writes “Hello, World!” as the response to any HTTP request. The main
function sets up a HTTP server to handle incoming requests using the http
package from Go’s standard library.
Save the main.go
file.
Building a Docker Image
To build a Docker image for our Go web application, we need to create a Dockerfile. Create a new file named Dockerfile
in your project directory and add the following code:
# Use the official Go image as the base image
FROM golang:1.17
# Set the working directory inside the container
WORKDIR /app
# Copy the Go mod and sum files to the working directory
COPY go.mod go.sum ./
# Download the Go module dependencies
RUN go mod download
# Copy the source code to the working directory
COPY . .
# Build the Go application
RUN go build -o main .
# Expose port 8080
EXPOSE 8080
# Command to run the executable
CMD ["./main"]
In this Dockerfile, we start with the official Go image as the base image. We set the working directory inside the container, copy the Go mod and sum files, and download the Go module dependencies. Then, we copy the source code to the working directory, build the Go application, and expose port 8080 to allow incoming connections. Finally, we specify the command to run the executable main
when the container starts.
Save the Dockerfile.
To build the Docker image, open your terminal or command prompt and navigate to your project directory. Run the following command:
docker build -t my-go-webapp .
This command builds a Docker image named my-go-webapp
using the current directory as the build context. The -t
option tags the image with the given name.
Wait for the build process to complete. Once it’s done, you can check if the image was created successfully by running the following command:
docker images
This command lists all the Docker images on your machine. You should see the my-go-webapp
image in the list.
Running the Docker Container
Now that we have built the Docker image, let’s run the Go web application inside a Docker container.
Run the following command in your terminal or command prompt to start a new container from the my-go-webapp
image:
docker run -p 8080:8080 my-go-webapp
This command starts a new container based on the my-go-webapp
image and maps port 8080 from the container to port 8080 on your machine. The -p
option specifies the port mapping.
Once the container is running, open your web browser and visit http://localhost:8080
. You should see “Hello, World!” displayed on the page.
To stop the container, press Ctrl + C
in your terminal or command prompt.
Conclusion
In this tutorial, you learned how to use Docker with a Go web application. You created a simple Go web application, built a Docker image for it using a Dockerfile, and ran the application inside a Docker container. Docker allows you to package and deploy your Go web applications easily, ensuring consistent behavior across different environments.
Feel free to explore more advanced features of Docker, such as container orchestration with Docker Compose or deploying your Dockerized Go web application to a cloud platform.
Remember to clean up after yourself by removing the Docker images and containers you created during this tutorial using the docker rm
and docker rmi
commands.
Happy containerizing!