How to run a Go program built in a Docker container?

DockerDockerBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/DockerfileGroup(["`Dockerfile`"]) docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/start("`Start Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") docker/DockerfileGroup -.-> docker/build("`Build Image from Dockerfile`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/ps -.-> lab-411595{{"`How to run a Go program built in a Docker container?`"}} docker/run -.-> lab-411595{{"`How to run a Go program built in a Docker container?`"}} docker/start -.-> lab-411595{{"`How to run a Go program built in a Docker container?`"}} docker/stop -.-> lab-411595{{"`How to run a Go program built in a Docker container?`"}} docker/build -.-> lab-411595{{"`How to run a Go program built in a Docker container?`"}} docker/ls -.-> lab-411595{{"`How to run a Go program built in a Docker container?`"}} end

Introduction to Docker

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.

What is Docker?

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.

Why Use Docker?

Docker offers several benefits for developers and IT professionals:

  1. Consistency: Docker containers ensure that applications run the same way, regardless of the underlying infrastructure.
  2. Portability: Docker containers can be easily moved between different environments, such as development, testing, and production.
  3. Scalability: Docker makes it easy to scale applications up or down, depending on the workload.
  4. Efficiency: Docker containers are lightweight and use fewer resources than traditional virtual machines, making them more efficient to run.

Getting Started with Docker

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.

graph TD A[Install Docker] --> B[Create Docker Image] B --> C[Run Docker Container] C --> D[Deploy Application]

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.

Building a Go Application in Docker

Creating a Go Application

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 (/).

Building a Docker Image

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:

  1. Uses the official golang:1.18-alpine image as the base image.
  2. Sets the working directory to /app.
  3. Copies the Go application code to the container.
  4. Builds the Go application and creates an executable named main.
  5. Sets the command to run the main executable when the container starts.

Building the Docker Image

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.

Verifying the Docker Image

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.

Running the Go Application in Docker

Running the 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:

  1. Runs the my-go-app Docker image.
  2. Maps the container's port 8080 to the host's port 8080, allowing you to access the application from your local machine.
graph TD A[Docker Host] --> B[Docker Container] B --> C[Go Application] A --"HTTP 8080"--> B

Verifying the Running Container

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.

Accessing the Go Application

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.

Stopping the Docker Container

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.

Conclusion

In this tutorial, you learned how to:

  1. Create a simple Go application.
  2. Build a Docker image for the Go application.
  3. Run the Go application in a Docker container.
  4. Access the Go application running in the Docker container.

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.

Summary

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.

Other Docker Tutorials you may like