Introducing Docker Init: Generating Docker Assets for Your Projects
Published: 2023-04-06 07:17:24

[Docker Desktop 4.18 ](https://docs.docker.com/desktop/release-notes/#4180)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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2izvkdfivxt7rzg932fl.png)

Another new feature is [Docker init](https://docs.docker.com/engine/reference/commandline/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
```

![Image002](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6lhd9iz5vrndkwua11o.png)

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


![Image102](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89tlqu0yixv2virmqdt0.png)



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

![Image004](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnsnkcf0t5zw6hc8se91.png)

Another interesting addition is the [Docker Scout CLI](https://docs.docker.com/scout/#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zjsptviod3t89atonhr.png)

## Introducing the `docker init` CLI

[Docker init](https://docs.docker.com/engine/reference/commandline/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

- [Download Docker Desktop for Mac](https://desktop.docker.com/mac/main/arm64/Docker.dmg?utm_source=docker&utm_medium=webreferral&utm_campaign=docs-driven-download-mac-arm64)


## 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
```

## Step 2. Choose your preferred language

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



![Image1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xojtlz80isnb42ujgcbu.png)


## 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.

```go
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=
```

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

```


![Image3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rv0dzrtwqs3mraunhrrl.png)


## 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
```

## 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:

- [Docker Desktop 4.18 Release Notes](https://docs.docker.com/desktop/release-notes/#4180)
- [Download Docker Desktop for Windows](https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe?utm_source=docker&utm_medium=webreferral&utm_campaign=docs-driven-download-win-amd64)
- [Download Docker Desktop for Apple Chip](https://desktop.docker.com/mac/main/arm64/Docker.dmg?utm_source=docker&utm_medium=webreferral&utm_campaign=docs-driven-download-mac-arm64)
- [Docker scout CLI](https://docs.docker.com/scout/#docker-scout-cli)