Docker Init command: Create Docker projects automatically

docker-init

Docker Init lets you set up a Docker project in a snap. With just one CLI command, docker init, you can generate everything you need.

Docker has revolutionised the software development process, simplifying software deployment and making it more accessible to other developers. The recently-introduced Docker Init command streamlines project creation by generating configuration files with a single command.

TL;DR: docker init dockerises your application in seconds! 🙂

Docker Init

The command creates three files in your project:

  • a Dockerfile: contains the instructions for creating a Docker image based on a container.
  • a .dockerignore: lists files and directories that should be ignored when creating the image.
  • a compose.yaml: defines the services, networks and volumes for the orchestration of Docker containers with Docker Compose.
  • and a README.Docker.md

In other words, all the files that are necessary for a complete Docker App.

To use the Docker Init command, you only need Docker Desktop > version 4.18. And of course a non-dockerised project, i.e. a project for which no Docker files exist yet.

Download Docker Desktop

Supported languages

Currently, the command supports 7 programming languages:

  • ASP.NET
  • Go
  • Java
  • Node
  • PHP
  • Python
  • Rust

If a project does not involve any of the languages, the Dockerfile is configured to establish an entry point that is universally applicable.

However, it is highly likely that the capabilities of this command will be extended in future updates, leaving us intrigued.

Docker Init in Action

Let’s get down to business and look at the command in action in a Node.js application.

Are you getting an error when entering the command? How to fix the Docker Permission Denied error.

> docker init
Docker Init command in a Node project in Visual Studio Code
Docker Init command in a Node project in Visual Studio Code
Executed Docker Init command
Executed Docker Init command

You will then be guided through a range of questions.

? What application platform does your project use? Node
? What version of Node do you want to use? (20.5.1) 
? What application platform does your project use? Node
? What version of Node do you want to use? 20.5.1
? Which package manager do you want to use?  [Use arrows to move, type to filter]
> npm - (detected)
  yarn
  pnpm
? What application platform does your project use? Node
? What version of Node do you want to use? 20.5.1
? Which package manager do you want to use? npm
? What command do you want to use to start the app? [tab for suggestions] (node index.js)
? What application platform does your project use? Node
? What version of Node do you want to use? 20.5.1
? Which package manager do you want to use? npm
? What command do you want to use to start the app? node index.js
? What port does your server listen on? 3000

And that is all. We get the three said files that we need for a complete Docker project.

Result of the Docker Init command
Result of the Docker Init command
Dockerfile, .dockerignore and compose.yaml
Dockerfile, .dockerignore and compose.yaml

To check the configuration, we run the docker compose command: Running!

docker compose up
docker compose up

And the Dockerfile also works for a single Docker container. We get a running container for our application.

docker build -t docker-init-app .

docker run -p 3000:3000 docker-init-app

How to containerize other types of applications

For optimal Docker utilization, it’s best to use the language-specific template provided by docker init. Alternatively, selecting the “Other” template type offers minimal files, ready for customization. This approach helps you grasp Docker basics with well-documented output.

Why you should use Docker Init

In my eyes, the command offers some advantages for developers and teams:

  • Accelerated project initialisation: Docker Init automates the creation of all configuration files. This eliminates the time-consuming manual creation of these files and speeds up development processes.
  • Consistency and standardisation: Like CI/CD tools, using the Docker Init command ensures that all projects have a consistent and standardised Docker configuration. This leads to fewer configuration errors and improved team collaboration.
  • Reduced error-proneness: Docker Init helps minimise errors in Docker configuration by implementing best practices and recommended structures for Docker projects. This leads to more robust and reliable applications.

Psssst! Do you know the difference between (Docker) containers and virtual machines (VMs)?

Conclusion

The Docker Init command highlights Docker’s prominence in software development. By using this command, developers can rapidly create Docker projects while minimising configuration file setup.

Show external resources
Related Posts
Join the Conversation

3 Comments

Your email address will not be published. Required fields are marked *

bold italic underline strikeThrough
insertOrderedList insertUnorderedList outdent indent
removeFormat
createLink unlink
code

This can also interest you