Table of Contents
- Introduction
- Prerequisites
- Setting Up Docker
- Creating a Go Module
- Building a Docker Image
- Running the Docker Container
-
Introduction
In this tutorial, we will learn how to use Go modules in Docker to containerize and deploy a Go application. Go modules provide a way to manage dependencies and versioning in a Go project. Docker, on the other hand, allows us to package an application along with its dependencies into a container, making it easier to distribute and deploy.
By the end of this tutorial, you will be able to create a Go module, build a Docker image, and run a Docker container with your Go application.
Prerequisites
- Basic understanding of Go programming language
- Docker installed on your machine
Setting Up Docker
Before we begin, make sure you have Docker installed and running on your system. You can verify the installation by running the following command in the terminal:
docker --version
If Docker is installed correctly, you should see the version information printed on the console.
Creating a Go Module
-
Create a new directory for your Go project:
mkdir my-go-app cd my-go-app
-
Initialize a Go module:
go mod init <module-name>
Replace
<module-name>
with the name of your module. This command creates ago.mod
file that tracks the dependencies of your project. -
Add dependencies to your Go module:
go get <dependency-name>
Replace
<dependency-name>
with the name of the dependency you want to add. This command downloads the dependency and updates thego.mod
file with the required version. -
Import the required packages in your Go application using the syntax:
import "example.com/my-go-app/mypackage"
Replace
"example.com/my-go-app/mypackage"
with the correct import path for your package.
Building a Docker Image
-
Create a Dockerfile in your project directory:
touch Dockerfile
-
Open the Dockerfile in a text editor and add the following contents:
```Dockerfile
Use an official Go runtime as the base image
FROM golang:1.16-alpine
Set the working directory inside the container
WORKDIR /go/src/example.com/my-go-app
Copy the local package files to the container’s workspace
COPY . .
Build the Go application
RUN go build .
Expose the port on which the application will listen
EXPOSE 8080
Run the Go application
CMD [”./my-go-app”] ```
The Dockerfile specifies the base image, sets the working directory, copies the local package files, builds the Go application, exposes the required port, and finally runs the application.
-
Build the Docker image by running the following command in the terminal:
docker build -t my-go-app .
This command builds the Docker image with the tag
my-go-app
.
Running the Docker Container
-
Once the image is built, you can run a Docker container using the following command:
docker run -p 8080:8080 my-go-app
This command starts a Docker container based on the
my-go-app
image and forwards port8080
from the container to the host. -
Open your web browser and navigate to
http://localhost:8080
to access your Go application running inside the Docker container.
Conclusion
In this tutorial, we learned how to use Go modules in Docker to containerize a Go application. We set up Docker, created a Go module, built a Docker image, and ran a Docker container. By combining the power of Go modules and Docker, we can easily manage dependencies and deploy our applications in a reproducible and portable manner.
Now that you have a basic understanding of using Go modules in Docker, you can explore more advanced concepts such as multi-stage builds, deploying to a container orchestration platform, or optimizing your Docker image size. Happy coding!