codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

51 / 72 topics
51Docker for Go Applications52Kubernetes with Go53CI/CD Pipelines for Go Projects54Monitoring and Logging in Go Applications
Tutorials/Go (Golang)/Docker for Go Applications
🐹Go (Golang)

Docker for Go Applications

Updated 2026-04-20
4 min read

Docker for Go Applications

Docker is a powerful tool that allows developers to package their applications and dependencies into containers, ensuring consistency across different environments. This tutorial will walk you through the process of containerizing a Go application using Docker, from setting up your environment to deploying your application.

Prerequisites

Before we dive into the details, ensure you have the following installed on your machine:

  • Docker: Install Docker for your operating system.
  • Go (Golang): Install Go and set up your GOPATH.

Step 1: Create a Simple Go Application

Let's start by creating a simple "Hello, World!" application in Go. This will serve as the basis for our Docker container.

Directory Structure

Create a new directory for your project:

mkdir go-docker-app
cd go-docker-app

Inside this directory, create a file named main.go with the following content:

package main

import "fmt"

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

Build and Run the Application Locally

Before containerizing, let's ensure our application works locally. Compile and run your Go application:

go build -o hello-docker .
./hello-docker

You should see the output:

Hello, Docker!

Step 2: Create a Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Let's create a Dockerfile in your project directory.

# Use the official Go image from the Docker Hub as the base image
FROM golang:1.17-alpine AS builder

# Set the working directory inside the container
WORKDIR /app

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

# Download all dependencies
RUN go mod download

# Copy the source code into the container
COPY . .

# Build the Go application
RUN CGO_ENABLED=0 GOOS=linux go build -o hello-docker .

# Use a minimal image for running the application
FROM alpine:latest

# Set the working directory inside the container
WORKDIR /root/

# Copy the binary from the builder stage to the final image
COPY --from=builder /app/hello-docker .

# Expose port 8080 if your application uses it (not needed for this simple app)
EXPOSE 8080

# Run the Go application
CMD ["./hello-docker"]

Explanation of the Dockerfile

  1. Base Image: We use golang:1.17-alpine as the base image for building our application. This is a lightweight image that includes Go and Alpine Linux.
  2. Working Directory: The WORKDIR /app command sets the working directory inside the container to /app.
  3. Copy Dependencies: We copy go.mod and go.sum to download dependencies.
  4. Build Application: The application is built using CGO_ENABLED=0 GOOS=linux go build -o hello-docker .. This ensures that the binary is compiled for Linux without CGO, making it smaller and more portable.
  5. Final Image: We use a minimal alpine:latest image to run our application. This reduces the final image size.
  6. Copy Binary: The binary from the builder stage is copied into the final image.
  7. Expose Port: If your application listens on a specific port, you can expose it using EXPOSE.
  8. Run Command: Finally, we specify the command to run our application with CMD ["./hello-docker"].

Step 3: Build and Run the Docker Container

Now that we have our Dockerfile, let's build and run our Docker container.

Build the Docker Image

Run the following command in your project directory:

docker build -t go-docker-app .

This command builds a Docker image tagged as go-docker-app using the Dockerfile in the current directory.

Run the Docker Container

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

docker run -d --name my-go-app go-docker-app
  • -d: Runs the container in detached mode.
  • --name my-go-app: Names the running container my-go-app.

Verify the Running Container

You can verify that your container is running by listing all containers:

docker ps

You should see an entry for my-go-app in the list.

Step 4: Accessing the Application

Since our simple application doesn't expose any ports, you won't be able to access it via a web browser. However, if your application listens on a port (e.g., 8080), you can map that port when running the container:

docker run -d --name my-go-app -p 8080:8080 go-docker-app

This command maps port 8080 of your host to port 8080 inside the container.

Step 5: Docker Compose (Optional)

For more complex applications, especially those with multiple services, Docker Compose is a powerful tool. Let's create a docker-compose.yml file for our application.

version: '3'
services:
  app:
    build: .
    ports:
      - "8080:8080"
    environment:
      - ENV_VAR=example_value

Explanation of docker-compose.yml

  1. Version: Specifies the version of Docker Compose.
  2. Services: Defines the services that make up your application.
  3. Build: Points to the directory containing the Dockerfile.
  4. Ports: Maps ports between the host and the container.
  5. Environment: Sets environment variables for the service.

Running with Docker Compose

To start your application using Docker Compose, run:

docker-compose up -d

This command builds and starts all services defined in your docker-compose.yml file.

Best Practices

  1. Use Multi-Stage Builds: As shown in our Dockerfile, multi-stage builds help reduce the final image size by separating build and runtime environments.
  2. Keep Images Lightweight: Use minimal base images like Alpine Linux to keep your containers lightweight.
  3. Manage Dependencies with Go Modules: Always use Go modules (go.mod and go.sum) to manage dependencies.
  4. Use Environment Variables: For configuration, use environment variables instead of hardcoding values in your application.
  5. Regularly Update Base Images: Keep your base images updated to benefit from security patches and improvements.

Conclusion

In this tutorial, we covered the basics of containerizing a Go application using Docker. We created a simple "Hello, World!" application, wrote a Dockerfile, built and ran a Docker container, and optionally used Docker Compose for more complex setups. By following these steps and best practices, you can ensure that your Go applications are consistent and easy to deploy across different environments.

Feel free to explore more advanced features of Docker and Go, such as using Docker volumes for persistent storage or integrating with CI/CD pipelines for automated deployments.


PreviousProtocol BuffersNext Kubernetes with Go

Recommended Gear

Protocol BuffersKubernetes with Go