DEV Community

Cover image for Easy Dockerization with Docker INIT
Pradumna Saraf
Pradumna Saraf

Posted on • Updated on

Easy Dockerization with Docker INIT

Docker Init is changing the game in how we Dockerize our applications.

With docker init, we can quickly generate the Dockerfile, compose.yml, and .dockerignore. In the past, we manually created these files and implemented the best practices.

Now, with just one command and by answering a series of prompts, Docker automatically sets up these necessary files for us. Notably, this new approach ensures that industry best practices are followed.

In today's article, we'll also see a demo of dockerizing a Node application with Docker init.

Prerequisites:

  • Docker Desktop 4.18 or later

Steps:

1) Initialize the Project and Install Dependencies:

For this demonstration, we'll set up a basic application using Node and Express. Begin by initializing your project:



npm init


Enter fullscreen mode Exit fullscreen mode

Then install the Express dependency:



npm i express


Enter fullscreen mode Exit fullscreen mode

2) Add a Start Script:

Add a start script to your package.json file:



"scripts": {
    "start": "node index.js"
},


Enter fullscreen mode Exit fullscreen mode

3) Create a Simple API:

Create an index.js file and insert the following code:



const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.listen(port, () => {
  console.log(`App listening on port ${port}`);
});


Enter fullscreen mode Exit fullscreen mode

4) Docker INIT:

Run the docker init command and select the language of your project. You'll then be prompted with a series of questions tailored to your project and its structure.

Image description

5) Running the App:

After the setup, execute the command docker compose up --build to construct the images and launch the application.

Terminal Screenshot

That's it. I hope you learned something from this. As the world moves towards containerizing applications, this can be instrumental in accelerating tasks and transitioning from monoliths to microservices.

Top comments (5)

Collapse
 
levelleor profile image
Levelleor • Edited

Why docker compose and not docker run? Is it in some way better? It feels like an excess technology to use when just simply running a single container.

Collapse
 
jimbozo profile image
James Stokes

The docker-compose approach means you only need to figure it out once. If you're going things like mounting volumes, opening ports, etc your docker run incantation can become complex.

Defining a docker-compose saves you time in the future ensuring all of your requirements are saved in your docker-compose.yml and you only have to run docker-compose up to get your server running.

Collapse
 
scofieldidehen profile image
Scofield Idehen

Nice.

Collapse
 
levelleor profile image
Levelleor

Got you. I was curious to why docker created that compose file instead of just expecting the users to run docker run command. Your reply answered that question.

Collapse
 
mdledoux profile image
Martin Ledoux • Edited

I have a couple reasons:

Arguably more containers will be added - so this is a trivial example.

But even for such a simple example, any startup params can be saved in that yml file for repeated usage, like volumes, select environment variables (this one container may not need everything in .env), port mappings, etc...
Before I learned Compose, I was writing shell scripts to maintain these values.

Plus, apparently this new Init tool writes the file for you, so why not?