Go: using Docker

Go: using Docker

In this article, we'll explore how to use Go with Docker, step-by-step, to make the most of these complementary technologies.

Go (or Golang) is an open-source programming language developed by Google, known for its speed, efficiency and ease of use. Docker, on the other hand, is a popular containerization platform that allows you to easily build, deploy and manage applications in isolated environments, known as containers.

Using Go with Docker can bring many benefits to developers by simplifying the process of developing, deploying, and managing applications. In this article, well explore how to use Go with Docker, step-by-step, to make the most of these complementary technologies.

Install Docker and Go

First of all, lets make sure we have Docker and Go installed on our system.

To install Docker, follow the official instructions available on the official Docker site ( https://www.docker.com/ ).

To install Go, visit the official Go language site ( https://golang.org/ ) and follow the instructions for your operating system.

Set up the development environment

After installing Docker and Go, you need to set up your development environment. We create a new folder for our Go project and inside we create a file called "main.go" containing the application code.


package main

import "fmt"

func main() {
    fmt.Println("Ciao da Go in Docker!")
}

Create the Dockerfile

The Dockerfile is a text file that contains instructions for building a custom Docker image. Lets create a new file called "Dockerfile" in the same folder as our Go project.


# Usiamo l'immagine di base di Go
FROM golang:latest

# Impostiamo la directory di lavoro all'interno del container
WORKDIR /app

# Copiamo il codice dell'applicazione nella directory di lavoro
COPY . .

# Compiliamo l'applicazione Go all'interno del container
RUN go build -o main .

# Esponiamo la porta su cui l'applicazione ascolterà le richieste
EXPOSE 8080

# Comando di avvio dell'applicazione all'interno del container
CMD ["./main"]

Create and run the container

Now that weve created the Dockerfile, we can build the Docker image and launch a container to run the Go application.

Lets open a terminal window in the project folder and use the following command to build the Docker image:

docker build -t nome_immagine .

Where "image_name" is the name we want to assign to the Docker image.

Once the image build is complete, we can run the container with the following command:

docker run -p 8080:8080 nome_immagine

The "-p" flag is used to map the container port (8080) to the host port (8080), allowing you to access the application via the browser or API test tools.

Now that the container is running, lets open a browser and visit http://localhost:8080. We should see the "Hello from Go to Docker!" message.

Conclusion

In this guide, weve learned how to use Go with Docker to streamline the development, deployment, and management of Go applications. Using Docker to containerize Go applications offers many benefits, including isolation, efficiency, and reproducibility.