Deploy Go Minimal API to Docker and Kubernetes

Deploy Go Minimal API to Docker and Kubernetes


4 min read

Play this article


Go Lang is a powerful programming language known for its simplicity, performance, and strong concurrency support. With the rise of containerization and orchestration technologies like Docker and Kubernetes, deploying Go applications has become even more convenient and scalable. In this article, we will explore how to deploy a Go Lang minimal API to Docker and Kubernetes, enabling you to easily containerize and scale your Go applications.


Before we begin, make sure you have the following installed:

  1. Go Lang: You can download and install Go from the official website (

  2. Docker: Install Docker from the official website ( for your operating system.

  3. Kubernetes: If you want to deploy to a Kubernetes cluster, make sure you have a running Kubernetes cluster set up or use a cloud-based Kubernetes service like Google Kubernetes Engine (GKE) or Amazon Elastic Kubernetes Service (EKS).

Step 1: Create a Minimal Go API

First, let's create a minimal Go API. Open your preferred text editor and create a new file called main.go. Add the following code to create a simple HTTP server that listens on port 8080 and responds with a "Hello, World!" message:

package main

import (

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)

Save the file and exit the editor. We now have a minimal Go API that we can deploy to Docker and Kubernetes.

Step 2: Dockerize the Go API

To containerize our Go API, we'll create a Dockerfile that specifies the build steps and runtime environment. Create a new file called Dockerfile in the same directory as main.go and add the following code:

# Use the official Go image as the base image
FROM golang:1.16-alpine

# Set the working directory inside the container

# Copy the Go modules files
COPY go.mod .
COPY go.sum .

# Download and install Go dependencies
RUN go mod download

# Copy the source code into the container
COPY . .

# Build the Go application
RUN go build -o main .

# Expose the port the application listens on

# Set the entry point of the container
CMD ["./main"]

Save the Dockerfile and exit the editor. We've defined a multi-stage Docker build that downloads Go dependencies, copies the source code, builds the Go application, and sets the entry point to run our API.

Step 3: Build and Run the Docker Image

Next, let's build and run the Docker image locally. Open a terminal or command prompt in the same directory as main.go and Dockerfile. Run the following command to build the Docker image:

docker build -t my-go-api .

The -t flag sets the name and tag for the Docker image (you can choose any name you like).

Once the build is complete, run the following command to start a Docker container using the image we just built:

docker run -p 8080:8080 my-go-api

The -p flag maps the host port 8080 to the container port 8080. Now, if you open your browser and navigate to http://localhost:8080, you should see the "Hello, World!" message.

Step 4: Deploy to Kubernetes

If you have a Kubernetes cluster set up, you can deploy our Go API as a Kubernetes deployment. Create a new file called deployment.yaml and add the following Kubernetes deployment configuration:

apiVersion: apps/v1
kind: Deployment
  name: go-api-deployment
  replicas: 3
      app: go-api
        app: go-api
        - name: go-api
          image: my-go-api
            - containerPort: 8080

Save the deployment.yaml file. This configuration specifies a deployment with three replicas and exposes port 8080. It uses the Docker image we built earlier.

To deploy the application to Kubernetes, run the following command:

kubectl apply -f deployment.yaml

Kubernetes will create the deployment and manage the desired number of replicas of our Go API.


In this article, we learned how to deploy a minimal Go Lang API to Docker and Kubernetes. We started by creating a simple Go API, then containerized it using Docker and created a Docker image. We also explored how to build and run the Docker image locally. Finally, we deployed the application to a Kubernetes cluster using a deployment configuration.

Now you know how to take your Go applications and deploy them to containers, enabling scalability and portability across different environments. Containerization with Docker and orchestration with Kubernetes provide a powerful infrastructure for deploying and managing Go applications at scale.

Did you find this article valuable?

Support Nouman Rahman by becoming a sponsor. Any amount is appreciated!