Easy Dockerization with Docker INIT

Pradumna Saraf - Sep 7 '23 - - Dev Community

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player