Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Dockerizing Go Applications

1. Introduction

Dockerizing Go applications involve packaging them into Docker containers to ensure consistent environments for development, testing, and production. This tutorial will guide you through the entire process from writing a simple Go application to deploying it using Docker.

2. Prerequisites

Before we start, ensure you have the following installed on your system:

  • Go (latest version)
  • Docker

3. Writing a Simple Go Application

Let's start by writing a simple Go application that we can later dockerize. Create a new directory for your project and navigate to it:

mkdir my-go-app && cd my-go-app

Next, create a file named main.go and add the following code:

package main
import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

This is a simple Go application that prints "Hello, World!" to the console.

4. Building the Go Application

We need to build our Go application before we can dockerize it. Run the following command to build the application:

go build -o app

This command compiles the Go source code and produces an executable named app.

5. Creating a Dockerfile

A Dockerfile is a script that contains a series of instructions on how to build a Docker image for our application. Create a file named Dockerfile in the project directory and add the following content:

# Start from the official Golang base image
FROM golang:alpine AS builder

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod .
COPY go.sum .

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the Working Directory inside the container
COPY . .

# Build the Go app
RUN go build -o app

# Start a new stage from scratch
FROM alpine:latest

# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/app /app/app

# Command to run the executable
CMD ["/app/app"]

This Dockerfile uses a multi-stage build to reduce the final image size. It starts with a Golang base image to compile the application and then copies the compiled binary to a minimal Alpine Linux image.

6. Building the Docker Image

To build the Docker image, run the following command in the project directory:

docker build -t my-go-app .

This command builds the Docker image and tags it as my-go-app.

7. Running the Docker Container

Once the Docker image is built, you can run it using the following command:

docker run --rm my-go-app

The --rm flag ensures the container is removed after it exits. You should see the following output:

Hello, World!

8. Conclusion

Congratulations! You have successfully dockerized a simple Go application. This process ensures that your application runs in a consistent environment, making it easier to manage dependencies and deploy to different stages in your workflow.