Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

Docker Init for Go Developers

3 min read

Are you a Go developer who still writes Dockerfile and Docker Compose manually? Containerizing Go applications is a crucial step towards ensuring consistent and reliable deployments, but the process can be time-consuming and error-prone when done manually.

Fortunately, docker init, a powerful command-line interface, streamlines the Dockerization of Go applications.docker init is a command-line interface tool that helps you initialize a new Docker project. It is especially useful for Go developers, as it can generate a Dockerfile and Docker Compose file that is tailored to the specific requirements of a Go application with the best practices. For example, the Dockerfile that Docker init generates will include the steps necessary to build your Go application, install its dependencies, and expose the port that your application listens on. Also, it creates Docker Compose with the required services and with the best practices

Docker Init is Highly Customizable

Docker init is highly customizable. You can specify a number of options when running the command, such as:

  • The application platform that you are using.
  • The Go version that you want to use.
  • The relative directory of your main package.
  • The port that your server listens on.
  • The name of the Docker image that you want to create.

By customizing the command and the generated Docker files, you can create Docker images and Compose files that are tailored to the specific needs of your project.It allows you to customize the Dockerfile and Compose file that docker init generates by editing them directly. For example, to add an additional step to the Dockerfile to build your application’s dependencies, add the step to the RUN section of the Dockerfile. To add an additional service to the Compose file, add the service to the services section of the Compose file.

Benefits of using Docker Init for Go Developers

Docker init helps you to create Dockerfile and Compose file with the best practices. The advantages of using the docker init command include:

  • Simplified Docker asset creation: The command streamlines the creation of necessary Docker files, reducing the chances of errors and ensuring that best practices are followed.
  • Saves time and effort: With the default settings and guided prompts, users can quickly create Docker assets without the need for extensive knowledge of Docker or its syntax.
  • Better project organization: The generated files provide a standardized and organized structure for the project, making it easier for developers to maintain and update the project over time.
  • Enhanced portability: By using Docker assets, projects become more portable across different environments, making it easier to move the project from development to production.

Getting Started

In this blog post, we will explore the Docker Init CLI tool, which streamlines the Dockerization of Go applications. By following a few simple steps, you can containerize your Go applications with ease.

Prerequisites

Before we dive into the details, ensure that you have Docker Desktop 4.18 or a higher version installed on your system. If you haven’t already, you can download it from the official Docker website.

Step 1: Clone the Repository

To get started with Docker Init for Go, we need a Go project to work with. We’ll use a simple Go program that creates an HTTP server listening on port 8080. This program responds to requests with a message and an ASCII art. First, clone the repository using the following command:

git clone https://github.com/dockersamples/helloworld-go-demo

Step 2: Initialize Docker Assets

Change your working directory to the cloned project folder, ‘hello-world-go-demo,’ and run the ‘docker init’ command:

cd hello-world-go-demo
docker init

This command will guide you through creating essential Docker assets for your project, including a .dockerignore file, a Dockerfile, and a docker-compose.yaml file.

Step 3: Choose Your Application Platform

The Docker Init CLI is intelligent enough to identify the code present in the working directory. It prompts you to select your project’s application platform. In our case, we’re building a Go application, so choose "Go – suitable for a Go server application."

What version of Go do you want to use? 1.20

Step 4: Choose Your Preferred Go Version

Specify the version of Go you want to use. For example, you can enter "1.20" as the version.

What version of Go do you want to use? 1.20

Step 5: Choose the Relative Directory

Assuming you’re already in the project directory (hello-world-go-demo), use a period (.) to indicate the present directory.

? What's the relative directory (with a leading .) of your main package? .

Step 6: Choose the Default Port

Specify the port your server listens on. In this case, it’s "8080."

? What port does your server listen on? (8080) 8080

Results

CREATED: .dockerignore
CREATED: Dockerfile
CREATED: docker-compose.yaml

✔ Your Docker files are ready!

Take a moment to review them and tailor them to your application.

When you're ready, start your application by running: docker compose up --build -d

Your application will be available at http://localhost:8080

To stop your application, run: docker compose down

Step 7: Viewing the Compose File

Once you’ve completed the setup, the Docker Init CLI will generate a docker-compose.yaml file. This file defines the services needed for your application, including their build contexts and port mappings.

services:
  server:
    build:
      context: .
      target: final
    ports:
      - 8080:8080

Step 8: Starting the Application

To start your Go application in a Docker container, use the following command:

docker compose up -d --build

This command builds your Docker image and starts the containerized application in detached mode.

Step 9: Accessing the Application

You can access your Go application by sending an HTTP request to ‘localhost:8080’ or using a tool like ‘curl’:

curl localhost:8080

          ##         .
    ## ## ##        ==
 ## ## ## ## ##    ===
/"""""""""""""""""\___/ ===
{                       /  ===-
\______ O           __/
 \    \         __/
  \____\_______/

Hello from Docker!

Conclusion

Docker has revolutionized the way we package, distribute, and run applications. It simplifies the process of containerization, making it easier for developers to build and deploy applications in consistent and reproducible environments.

Docker Init for Go simplifies the process of containerizing Go applications, enabling developers to efficiently package their code and dependencies. With just a few simple commands, you can create Docker assets, build container images, and run your Go applications in a consistent environment. Embrace containerization to enhance the portability and scalability of your Go projects. Happy coding!

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index