DEV Community

Cover image for Introducing Docker Init: Generating Docker Assets for Your Projects
Ajeet Singh Raina for Docker

Posted on • Edited on • Originally published at collabnix.com

Introducing Docker Init: Generating Docker Assets for Your Projects

Docker Desktop 4.18 is an exciting release with a lot of great new features that will make it easier for developers to work with Docker.

One of the most exciting features is the new Learning Center, which is designed to help users get started with Docker. This new area provides a wealth of resources and tutorials to help beginners get up to speed quickly.

Image001

Another new feature is Docker init, which is currently in beta. 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.

docker init
Enter fullscreen mode Exit fullscreen mode

Image002

The Container File Explorer is now generally available, allowing users to explore the contents of Docker containers with ease.

Image102

Additionally, the Images tab now includes an Artifactory view, which allows users to view and analyze images from Artifactory registries.

Image004

Another interesting addition is the Docker Scout CLI, which provides security update recommendations in the CLI. This is a handy tool for developers who want to ensure that their Docker assets are up to date and secure.

The experimental Docker Compose watch command is also included in this release. This command updates running Compose services as you edit and save your code, making it easier to make changes on the fly.

Finally, the new Adminless Mac Install is a new installation flow that doesn't require admin privileges. This makes it easier for developers to get started with Docker on their Macs without having to worry about administrative restrictions.

Image005

Introducing the docker init CLI

Docker init is a CLI command that was introduced in Docker Desktop 4.18 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

docker init

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]
> Node - (detected) suitable for a Node server application
  Go - suitable for a Go server application
  Python - suitable for a Python server application
  Other - general purpose starting point for containerizing your application
  Don't see something you need? Let us know!
  Quit
Enter fullscreen mode Exit fullscreen mode

Step 2. Choose your preferred language

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

Image1

Step 3. Choosing the relative directory

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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=
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Image3

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
tree
.
├── Dockerfile
├── docker-compose.yaml
├── go.mod
├── go.sum
└── main.go

1 directory, 5 files
Enter fullscreen mode Exit fullscreen mode

Step 5. Building the container

 % docker compose up --build -d
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 7. Accessing the App

% curl localhost:8080
Hello from Docker!
Enter fullscreen mode Exit fullscreen mode

References:

Top comments (1)

Collapse
 
toukirkhan profile image
Mohd Toukir Khan

Docker init is gonna make things really easier!