Easy Dockerization with Docker INIT

Easy Dockerization with Docker INIT

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

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

Then install the Express dependency:

npm i express
  1. Add a Start Script:

Add a start script to your package.json file:

"scripts": {
    "start": "node index.js"
},
  1. 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}`);
});
  1. 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

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

Also, at Livecycle, we are building Preevy, which helps you create a preview environment for Docker Compose applications. Do check out https://github.com/livecycle/preevy. It's open source, and don't forget to leave a star to show your support.