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

Introducing Docker Init: Generating Docker Assets for Your Projects

3 min read


Docker Desktop 4.18 is an exciting release with a lot of great new features like Docker Scout, Compose Watch, Container File Explorer etc. that will make it easier for developers to work with Docker.

One of the most exciting features introduced is Docker init. This feature generates Docker assets for projects, making it easier to create Docker images and containers. This is a great addition for developers who want to quickly create and manage Docker assets without having to manually configure everything.


Introducing the docker init CLI

Docker init is a CLI command that was introduced in Docker Desktop  to simplify the process of initializing a new project to run in a Docker container. When you run the docker init command in your project directory, it will guide you through the creation of the necessary files for your project with sensible defaults. These files include:

  • .dockerignore
  • Dockerfile
  • docker-compose.yaml

The docker init command also allows you to choose the application platform that your project uses and the relative directory of your main package. Additionally, it can generate Docker assets for your project using the Docker Scout CLI and provides an adminless Mac install flow that does not require admin privileges.

Getting Started

Step 1. Initialize the Docker assets


Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml

Let's get started!

? What application platform does your project use?  [Use arrows to move, type to filter]
  ASP.NET Core - (detected) suitable for an ASP.NET Core application
 > Go - suitable for a Go server application
  Python - suitable for a Python server application
  Node - suitable for a Node server application
  Rust - suitable for a Rust server application
  Other - general purpose starting point for containerizing your application
  Don't see something you need? Let us know!
  Quit
 

Step 2. Choose your preferred language

For this demo, I will choose Go and 1.20 as Go version.

Image1

Before you choose a relation directory in the next step,  follow the next step.

Step 3. Creating a simple Go Program

Before you choose relative directory of your main package, let’s open a new terminal and create a simple Go program that exposes a “Hello, World!” app over HTTP.

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, World!")
    })

    fmt.Println("Listening on port 8080...")
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        panic(err)
    }
}
 

This program listens for incoming HTTP requests on port 8080 and responds with the text “Hello, World!” for any incoming request. You can run this program using the go run command and then visit http://localhost:8080 in your web browser to see the output.

go run main.go
 

The output should show Listening on port 8080…. Then, visit http://localhost:8080 in your web browser to see the “Hello, World!” message.

Ensure that you have the go.sum file with the following content. The go.sum file is created automatically by the Go module system whenever new dependencies are added or removed from a module. When a new dependency is added to a module, the Go module system fetches the dependency and computes its checksum, which is then added to the go.sum file. If the dependency is already present in the go.sum file, the Go module system verifies that its checksum matches the expected value.:

cat go.sum
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

Step 4. Visualising the Project Directory

This is how the project directory structure look like:

tree
.
├── Dockerfile
├── docker-compose.yaml
├── go.sum
└── main.go

1 directory, 4 files
 

Go mod is a command-line tool that is built into the Go programming language. It is used to manage dependencies in Go projects. Go mod provides a way to declare and manage dependencies, versioning, and modules, which are collections of related Go packages.The Go mod CLI is the command-line interface for Go mod. It is used to run commands to manage modules, dependencies, and versions in Go projects.

Let’s initializes a new Go module in the current directory.

go mod init helloworld
 
tree
.
├── Dockerfile
├── docker-compose.yaml
├── go.mod
├── go.sum
└── main.go

1 directory, 5 files

Getting Ready !

Image3

Open the original terminal and just press “.” so that it picks up the default directory where our main.go file is located. It will show the following results:

? What's the relative directory (with a leading .) of your main package? .
? What port does your server listen on? 8080

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 5. Building the container

 % docker compose up --build -d
 

Step 6. Listing the services

docker compose ps
NAME                  IMAGE               COMMAND             SERVICE             CREATED             STATUS              PORTS
sample_app-server-1   sample_app-server   "/bin/server"       server              6 seconds ago       Up 5 seconds        0.0.0.0:8080->8080/tcp
 

Step 7. Accessing the App

% curl localhost:8080
Hello from Docker!

References:

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

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