Go, also known as Golang, is an open - source programming language developed by Google. It is designed to be efficient, simple, and easy to read and write. Golang has built - in support for concurrency, which makes it well - suited for developing network - based applications, microservices, and system software. It compiles to machine code quickly and has a garbage collector to manage memory efficiently.
Docker is an open - source platform that enables developers to automate the deployment, scaling, and management of applications using containerization technology. Containers are lightweight, isolated environments that package an application and all its dependencies, ensuring that it runs the same way regardless of the underlying infrastructure. Docker uses container images, which are read - only templates that contain the application code, libraries, and other necessary components.
To install Golang, visit the official Golang website ( https://golang.org/dl/ ) and download the appropriate installer for your operating system. Follow the installation instructions provided on the website. After installation, you can verify the installation by running the following command in your terminal:
go version
Visit the official Docker website ( https://www.docker.com/get - started) and download the Docker Desktop application for your operating system. Follow the installation instructions. After installation, you can verify the installation by running the following command in your terminal:
docker --version
Let’s create a simple “Hello, World!” Golang application. Create a new file named main.go
with the following code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
To run this application locally, open your terminal, navigate to the directory containing main.go
, and run the following command:
go run main.go
A Dockerfile is a text file that contains instructions for building a Docker image. Create a new file named Dockerfile
in the same directory as main.go
with the following content:
# Use an official Golang runtime as a parent image
FROM golang:1.17
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Build the Go application
RUN go build -o main .
# Run the executable
CMD ["./main"]
Open your terminal, navigate to the directory containing the Dockerfile
, and run the following command to build the Docker image:
docker build -t hello - world - golang .
Here, -t
is used to tag the image with the name hello - world - golang
, and the .
at the end indicates that the build context is the current directory.
After the image is built, you can run a container based on this image using the following command:
docker run hello - world - golang
Multi - stage builds in Docker allow you to use multiple FROM
statements in a single Dockerfile. This is useful for reducing the size of the final Docker image. Here is an updated Dockerfile
using multi - stage builds:
# Build stage
FROM golang:1.17 as builder
WORKDIR /app
COPY . /app
RUN go build -o main .
# Final stage
FROM alpine:3.14
WORKDIR /app
COPY --from=builder /app/main .
CMD ["./main"]
You can pass environment variables to the Golang application inside the container. For example, if your Golang application reads a port number from an environment variable, you can modify the main.go
file as follows:
package main
import (
"fmt"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
fmt.Printf("Listening on port %s\n", port)
}
To pass the PORT
environment variable to the container, you can use the following command when running the container:
docker run -e PORT=9090 hello - world - golang
Containerizing Golang applications using Docker provides numerous benefits, including portability, isolation, scalability, and reproducibility. By following the steps and best practices outlined in this blog, you can easily containerize your Golang applications and deploy them in various environments. With the combination of Golang’s performance and Docker’s flexibility, you can build and manage robust and efficient applications.