Building and Dockerizing a Simple Go HTTP Server (A Beginner's Guide)
This tutorial will guide you through creating a basic HTTP server using Go and then packaging it into a Docker container. Go is a powerful and efficient programming language, while Docker allows you to package your applications and their dependencies into isolated containers, ensuring they run consistently across different environments. This is a great way to get started with both technologies!
1. Setting up the Go Project π
First, let’s create a new directory for our project. Open your terminal and navigate to where you want to store your project. Then, create a new directory and navigate into it:
mkdir my-go-app
cd my-go-app
Next, create a file named main.go
inside this directory. This file will contain our Go code. Open main.go
in your favorite text editor and add the following code:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from Go!")
})
fmt.Println("Server listening on port 8080")
http.ListenAndServe(":8080", nil)
}
Let’s break down this code:
package main
: This line declares the package asmain
, which is the entry point for executable programs in Go.import (...)
: This section imports the necessary packages:fmt
for printing to the console andnet/http
for creating HTTP servers.http.HandleFunc("/", ...)
: This registers a handler function for the root path ("/"). Whenever a request comes to this path, the provided function will be executed.fmt.Fprintf(w, "Hello from Go!")
: This writes the response “Hello from Go!” to the client.fmt.Println("Server listening on port 8080")
: This prints a message to the console indicating that the server has started.http.ListenAndServe(":8080", nil)
: This starts the HTTP server and listens on port 8080.
2. Creating the Dockerfile π
Now, let’s create a Dockerfile
in the same directory as main.go
. This file will contain instructions for building our Docker image. Create a file named Dockerfile
(no extension) and add the following code:
FROM golang:latest
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o main .
EXPOSE 8080
CMD ["./main"]
Here’s what each line in the Dockerfile
does:
FROM golang:latest
: This specifies the base image for our Docker image. We’re using thegolang:latest
image, which includes the latest version of Go.WORKDIR /app
: This sets the working directory inside the container to/app
.COPY go.mod go.sum ./
: This copies the Go module files (if you have any) into the container. It’s a good practice to copy these first and rungo mod download
before copying the rest of the code to leverage Docker’s caching mechanism.RUN go mod download
: This downloads the Go module dependencies inside the container.COPY . .
: This copies all the files and directories from our project directory into the/app
directory in the container.RUN go build -o main .
: This builds the Go application inside the container and creates an executable file namedmain
.EXPOSE 8080
: This exposes port 8080 from the container, making it accessible from the outside.CMD ["./main"]
: This specifies the command to run when the container starts. In this case, it runs themain
executable, which starts our Go server.
3. Building the Docker Image π
Open your terminal in the project directory and run the following command to build the Docker image:
docker build -t my-go-app .
docker build
: This is the command to build a Docker image.-t my-go-app
: This tags the image with the namemy-go-app
. This makes it easier to refer to the image later..
: This specifies the build context, which is the current directory in this case.
4. Running the Docker Container π
Now that we’ve built the image, we can run a container from it using the following command:
docker run -p 8080:8080 my-go-app
docker run
: This is the command to run a Docker container.-p 8080:8080
: This maps port 8080 on your host machine to port 8080 in the container. This allows you to access the Go server from your browser.my-go-app
: This is the name of the Docker image we built earlier.
Open your web browser and go to http://localhost:8080
. You should see the “Hello from Go!” message.
5. Cleaning Up π
After you’re done, you can stop the running container using:
docker stop <container_id_or_name>
You can find the container ID or name using docker ps
.
To remove the container:
docker rm <container_id_or_name>
And finally, to remove the Docker image:
docker rmi my-go-app
Congratulations! You’ve successfully built and Dockerized a simple Go HTTP server. This is a fundamental step towards building more complex applications with Go and Docker. As you progress, you can explore more advanced topics like multi-stage builds, Docker Compose, and container orchestration.
I hope you enjoyed reading this post as much as I enjoyed writing it. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .