CKA Full Course 2024: Day 2/40 How To Dockerize a Project

Lloyd Rivers - Oct 20 - - Dev Community

Dockerize an Express Project: A Step-by-Step Guide

In this blog post, I’ll walk you through the process of dockerizing a (very) simple Express application. If you’ve just stumbled onto this article, it is part of a series, so here’s some background filler for you:

Docker is a powerful tool that allows developers to package applications and their dependencies into containers, ensuring that they run consistently across different environments.

We’ll cover everything from setting up the Dockerfile to running our application inside a container.

Prerequisites

Before we start, make sure you have the following:

  • Docker installed on your machine
  • Basic understanding of Node.js and Express
  • A terminal (command line) access

Step 1: Setting Up the Project

First, we need to either clone an existing application from GitHub or create our own. For this example, I'll create a simple Express app.

  1. Create a new directory for your project:
   mkdir my-express-app
   cd my-express-app
Enter fullscreen mode Exit fullscreen mode
  1. Initialize a new Node.js project:
   npm init -y
Enter fullscreen mode Exit fullscreen mode
  1. Install Express:
   npm install express
Enter fullscreen mode Exit fullscreen mode
  1. Create an index.js file:

Here’s a simple Express server that logs requests:

   const express = require('express');
   const app = express();

   app.use((req, res, next) => {
     console.log(req.method, req.url);
     next();
   });

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

   app.listen(3000, () => {
     console.log('Server is running on port 3000');
   });
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating the Dockerfile

Next, we’ll create a Dockerfile to define our container's environment.

  1. Create a Dockerfile in the root of your project directory:
   FROM node:alpine

   WORKDIR /app 

   COPY package*.json ./

   RUN npm install --production

   COPY . .

   EXPOSE 3000

   CMD ["node", "index.js"]
Enter fullscreen mode Exit fullscreen mode

Explanation of the Dockerfile

  • FROM node:alpine: We’re using a lightweight Node.js image based on Alpine Linux.
  • WORKDIR /app: This sets the working directory inside the container to /app.
  • COPY package*.json ./: We copy package.json and package-lock.json to the working directory.
  • RUN npm install --production: This installs only the production dependencies, reducing the size of our final image by excluding development dependencies.
  • COPY . .: This copies the rest of our application files into the container.
  • EXPOSE 3000: This tells Docker that the container listens on port 3000 at runtime.
  • CMD ["node", "index.js"]: This specifies the command to run our application.

Step 3: Building the Docker Image

Now that we have our Dockerfile, we can build our Docker image.

  1. Build the Docker image:
   docker build -t my-express-app .
Enter fullscreen mode Exit fullscreen mode

Here, -t my-express-app tags the image with the name my-express-app.

Step 4: Running the Docker Container

Once the image is built, we can run our application inside a Docker container.

  1. Run the container:
   docker run -p 3000:3000 my-express-app
Enter fullscreen mode Exit fullscreen mode

This maps port 3000 on your host to port 3000 in the container, allowing you to access the application at http://localhost:3000.

Exploring Docker Init Command

The docker init command is useful for creating a new Docker container from an existing image. However, in this context, we focused on building and running our container manually. The docker init command typically sets up a new container, allowing for more complex configurations.

Conclusion

Congratulations! You have successfully dockerized a simple Express application. Dockerizing applications helps maintain consistent environments and simplifies deployment.

Key Takeaways

  • Using the --production flag with npm install helps reduce image size by excluding development dependencies.
  • Docker containers can package all dependencies, ensuring that your application runs seamlessly in different environments.

GitHub Repository

Feel free to check out my GitHub repository for the complete code: My Express App on GitHub


Tags and Mentions

@piyushsachdeva

Day 2 video

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