Table of Contents
- Introduction
- Prerequisites
- Setting Up the Go Web Application
- Creating a Dockerfile
- Building the Docker Image
- Running the Docker Container
-
Introduction
In this tutorial, we will learn how to deploy a Go web application on Docker. Docker is a widely-used containerization platform that allows us to package and run applications in isolated environments. By following this tutorial, you will be able to containerize your Go web applications, making them portable and easily deployable.
Prerequisites
Before you start this tutorial, make sure you have the following prerequisites:
- Basic knowledge of Go programming language
- Docker installed on your system
Setting Up the Go Web Application
To demonstrate the deployment process, we will use a simple Go web application. If you already have an existing Go web application, you can skip this step.
-
Create a new directory for your Go web application:
$ mkdir mywebapp $ cd mywebapp
-
Initialize a new Go module:
$ go mod init github.com/your-username/mywebapp
-
Create a main.go file with the following code:
package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, World!") }) http.ListenAndServe(":8080", nil) }
-
Test the application by running it:
$ go run main.go
Visit
http://localhost:8080
in your browser to see the message “Hello, World!”.
Creating a Dockerfile
To containerize our Go web application, we need to create a Dockerfile. The Dockerfile defines the environment and dependencies required to run our application.
-
Create a new file named
Dockerfile
(without any file extension) in the root directory of your Go web application. -
Open the
Dockerfile
and add the following content:```Dockerfile
Use the official Go Docker image as the base image
FROM golang:latest
Set the working directory inside the container
WORKDIR /app
Copy the Go module files
COPY go.mod . COPY go.sum .
Download the application dependencies
RUN go mod download
Copy the source code to the container
COPY . .
Build the Go web application
RUN go build -o main .
Set the command to run the executable when the container starts
CMD [”./main”] ```
The Dockerfile starts with the official Go Docker image, sets the working directory, copies the Go module files, downloads the dependencies, copies the source code, builds the application, and sets the command to run the executable.
Building the Docker Image
Now that we have created the Dockerfile, we can build a Docker image for our Go web application.
-
Open a terminal and navigate to the root directory of your Go web application (where the
Dockerfile
is located). -
Build the Docker image using the following command:
$ docker build -t mywebapp .
The
-t
flag sets the name for the Docker image. In this example, we have named itmywebapp
. -
Wait for Docker to build the image. Once the process is complete, verify that the image has been created by running the following command:
$ docker images
You should see the
mywebapp
image listed.
Running the Docker Container
Now that we have built the Docker image, we can run a Docker container to deploy our Go web application.
-
Start a Docker container from the image using the following command:
$ docker run -p 8080:8080 mywebapp
The
-p
flag maps port8080
of the container to port8080
of the host machine. This allows us to access the web application running inside the container. -
Visit
http://localhost:8080
in your browser to see the message “Hello, World!” served by the Go web application running inside the Docker container.
Conclusion
By following this tutorial, you have learned how to deploy a Go web application on Docker. We started by setting up a simple Go web application and then created a Dockerfile to containerize it. Finally, we built a Docker image and ran a Docker container to deploy our application. Docker provides a convenient way to package and distribute Go web applications, making it easier to deploy them across different environments.
You can now experiment with different web applications or customize the Dockerfile to meet your specific requirements. Enjoy containerizing your Go web applications!