Introduction
Docker has revolutionized the way developers build, package, and deploy applications. In this tutorial, you will learn how to run a Go program inside a Docker container, ensuring a consistent and reliable execution environment.
Docker has revolutionized the way developers build, package, and deploy applications. In this tutorial, you will learn how to run a Go program inside a Docker container, ensuring a consistent and reliable execution environment.
Docker is a popular containerization platform that allows developers to package their applications and all their dependencies into a single, portable container. This container can then be easily deployed and run on any system that has Docker installed, regardless of the underlying operating system or infrastructure.
Docker is an open-source software platform that automates the deployment, scaling, and management of applications within software containers. Containers are lightweight, standalone, executable packages of software that include everything needed to run an application, including the code, runtime, system tools, and libraries.
Docker offers several benefits for developers and IT professionals:
To get started with Docker, you'll need to install the Docker engine on your system. You can download and install Docker from the official Docker website (https://www.docker.com/get-started). Once you have Docker installed, you can start creating and running Docker containers.
Table 1: Docker CLI Commands
Command | Description |
---|---|
docker build |
Build a Docker image from a Dockerfile |
docker run |
Run a Docker container |
docker ps |
List running Docker containers |
docker images |
List Docker images |
docker push |
Push a Docker image to a registry |
docker pull |
Pull a Docker image from a registry |
Now that you have a basic understanding of Docker, let's move on to building a Go application in a Docker container.
Let's start by creating a simple Go application. Create a new file named main.go
with the following content:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, LabEx!")
})
fmt.Println("Starting server on :8080")
http.ListenAndServe(":8080", nil)
}
This Go application listens on port 8080 and responds with the message "Hello, LabEx!" when a request is made to the root URL (/
).
To run this Go application in a Docker container, we need to create a Docker image. Create a new file named Dockerfile
with the following content:
FROM golang:1.18-alpine
WORKDIR /app
COPY . .
RUN go build -o main .
CMD ["./main"]
This Dockerfile:
golang:1.18-alpine
image as the base image./app
.main
.main
executable when the container starts.To build the Docker image, run the following command in the same directory as the Dockerfile
:
docker build -t my-go-app .
This command builds a Docker image with the tag my-go-app
.
You can list the available Docker images on your system by running the following command:
docker images
You should see the my-go-app
image in the list.
Now that we have a Docker image for our Go application, let's run it in a Docker container.
To run the Go application in a Docker container, use the following command:
docker run -p 8080:8080 my-go-app
This command:
my-go-app
Docker image.You can list the running Docker containers by running the following command:
docker ps
You should see the my-go-app
container in the list.
To access the Go application running in the Docker container, open a web browser and navigate to http://localhost:8080
. You should see the message "Hello, LabEx!" displayed.
To stop the running Docker container, use the following command:
docker stop <container_id>
Replace <container_id>
with the ID of the running container, which you can find by running the docker ps
command.
In this tutorial, you learned how to:
By containerizing your Go applications with Docker, you can ensure consistent and reliable deployment across different environments, making it easier to develop, test, and deploy your applications.
By the end of this tutorial, you will have a solid understanding of how to build a Go application in a Docker container and run it, making it easy to distribute and deploy your software across different environments. Docker's containerization technology simplifies the development and deployment process, allowing you to focus on writing great code.