Docker App Development: A Step-by-Step Guide to Containerize an E-Commerce App

Mohammed Affan
AWS in Plain English
6 min readAug 24, 2023

--

Welcome to a comprehensive guide on containerizing an e-commerce web app built with NodeJS. In modern software development, agility, scalability, and consistency are essential. Developers strive to create applications that seamlessly operate across various environments, eliminating the notorious “it works on my machine” issue. Docker has emerged as a transformative solution, revolutionizing app development, deployment, and management, thereby enhancing reliability and efficiency.

Prerequisites:

  • NodeJs Installed
  • Docker Desktop or Docker Engine Installed (For beginners, Docker Desktop is recommended; however, advanced users may prefer Docker Engine on a Linux Machine)

Understanding Docker and Containerization:

Docker, a powerful platform, empowers developers to craft, deliver, and run applications within lightweight containers. Containers represent isolated environments bundling applications and dependencies, ensuring consistent behavior regardless of the host setup. Unlike traditional virtual machines that replicate the entire OS, containers leverage the host system’s kernel, rendering them lightweight and efficient.

Containerization offers numerous advantages:

  1. Consistency: Containers encapsulate all necessary dependencies, libraries, and configurations, vanquishing the “it works on my machine” dilemma and guaranteeing uniform behavior across diverse environments.
  2. Isolation: Containers are isolated from one another and from the host system. This isolation enhances security and prevents conflicts between different applications running on the same machine.
  3. Portability: Since containers encapsulate everything needed to run an application, you can easily move them between different environments, whether it’s from a developer’s local machine to a staging server or from an on-premises data center to a cloud provider.
  4. Scalability: Containers can be easily scaled up or down to meet the demands of your application. This is particularly valuable for applications that experience varying levels of traffic.

Steps to containerize your application with Docker:

First, you need to get the e-commerce app. You can clone this app from GitHub by using this simple command :

git clone https://github.com/Affan-7/nodejs-ecommerce.git

Let’s dive into the process of containerizing your application using Docker.

Containerizing your application involves a few key steps:

1. Define a Dockerfile:

A Dockerfile is a script that defines how to build a Docker image. Think of it as a recipe for creating a Docker image. It specifies the base image, adds application code, sets environment variables, and configures any required dependencies. The Dockerfile is the blueprint for your application’s container.

In this project, you will create your own docker file from scratch. Begin by creating a file named ‘Dockerfile’ (without specifying any file extension) within the project directory. Open this file using your preferred code editor.

The Dockerfile follows a specific syntax, outlined comprehensively in Docker’s official documentation, the Dockerfile reference.

Define a base image:

Every Dockerfile starts with a base image. Since your app uses NodeJS you will use the latest version of this Node image from Docker Hub. Add the subsequent line to your Dockerfile:

FROM node:latest

This line specifies the use of the latest version of Node as the base image for containerizing your app.

Copy the application:

Refer to the ‘how to use this image’ section in the Documentation for instructions on using the NodeJS image.

This image employs the /home/node/app directory for storing source code and running the Node.js application. So, we need to put our source code in this image directory.

To achieve this, include the following lines in your Dockerfile:

WORKDIR /home/node/app

COPY . .

The WORKDIR command switches the current working directory within the container image, while the COPY command transfers files from the host machine to the image's working directory. Given that the Dockerfile is placed in your app's directory, this command copies all the source files from the host machine's app directory to the image's /home/node/app directory.

Install dependencies:

Now once you’ve copied your app inside the image, you want to install all the dependencies required by your app. You can do this by adding the following line.

RUN npm install

The RUN command executes shell commands within the container as it's being created.

Expose the network port:

Since your app will operate within a Docker container, you must specify the container port accessible to the host machine. This is achieved via the following command:

EXPOSE 3000

Launching the application:

Now you are all done with the app. Let’s specify how the container will start our app using the npm run start command.

CMD ["npm", "run", "start"]

The CMD instruction is a comma-separated list of strings that specifies the command for running your application alongside the required CLI arguments.

Dockerfile preview:

After following these steps your Dockerfile should look like the following.

FROM node:latest

WORKDIR /home/node/app

COPY . .

RUN npm install

EXPOSE 3000

CMD ["npm", "run", "start"]

2. Build the Docker image:

Once you have your Dockerfile, you can use the docker build command to create a Docker image. The image contains all the necessary components to run your application. This process might involve installing libraries, configuring settings, and copying files.

To build the docker image, open your terminal or shell, navigate to the directory containing your app and Dockerfile, and execute the following:

docker build -t myapp .

The -t flag specifies the name of the image that you are creating. In this case, I am using the name myapp. You can specify whatever name you want for your image.

Optionally, you can also add a version tag to your image by using a command like:

docker build -t myapp:1.0 .

After executing the command you’ll see a similar output.

3. Run containers:

With the Docker image in hand, you can create and run containers based on it. The docker run command starts a new container instance. You can also specify various options, such as port mappings, environment variables, and resource limits.

To launch your NodeJS app, use the following command:

docker run -d -p 3000:3000 myapp

This command specifies creating a container from the myapp image. The -d flag specifies starting the container in detached mode (not attaching the container to your terminal session). The -p flag specifies exposing the container's port to the host machine ( -p <host_port>:<container_port>).

After executing this command you can access your e-commerce app by navigating to http://localhost:3000.

4. Orchestration (optional):

For more complex applications, you might need to manage multiple containers that work together. Docker Compose, Docker Swarm, and Kubernetes are popular tools for orchestrating and managing containers. They enable you to define multi-container setups, networking, and scaling configurations.

Best practices for Docker app development:

To ensure a smooth transition to containerized development, here are some best practices to keep in mind:

  1. Keep Images Small: Minimize the size of your Docker images by using lightweight base images and only including necessary dependencies.
  2. Use .dockerignore: Just like .gitignore, the .dockerignore file allows you to exclude files and directories from being copied into the image.
  3. Single Responsibility Principle: Follow the principle of having a single process per container. This enhances maintainability and scalability.
  4. Version Dependencies: Pin versions for your dependencies to prevent unexpected changes that might affect your application.
  5. Security Considerations: Regularly update base images and application dependencies to patch security vulnerabilities. Avoid running containers with root privileges.
  6. Logging and Monitoring: Implement proper logging and monitoring within your containers to gain insights into the application’s behavior and performance.
  7. Documentation: Keep your Dockerfiles, Compose files, and any deployment scripts well-documented. This ensures that the development team can easily understand and maintain the containerized setup.

Conclusion:

Docker revolutionizes software development with consistent, efficient, and scalable containerization. It liberates developers from compatibility and deployment concerns, fostering faster cycles, seamless deployments, and robust apps. Adopt Docker to experience direct advantages during your development journey.

Last words:

Congratulations on completing this guide! 🥳️ 🎉️

Thank you for reaching the end of this article. If you found it helpful, consider showing your appreciation with a few claps!

For more insights into Docker 🐳, explore my other articles. If cloud technologies or DevOps fascinate you, feel free to follow my profile. If you have any questions or queries, drop them in the comments — I typically respond within a day.

I look forward to connecting with you on LinkedIn: https://www.linkedin.com/in/mohammedaffan7/

In Plain English

Thank you for being a part of our community! Before you go:

--

--

DevOps Engineer♾️| Cloud Computing☁️| Linux🐧| AWS🖥 | Python🐍| Docker⚓| Kubernets🛞| Terraform⚙️| Connect with me👇 www.linkedin.com/in/mohammedaffan7