DEV Community

SethGiddy
SethGiddy

Posted on

Getting Started with Docker.

Containerize Your Node.js Todo List Application with Docker.
Containerizing applications can streamline development and deployment, and it’s simpler than it seems! In this guide, we’ll walk through the process of containerizing a basic Node.js todo list manager using Docker. Don’t worry if you’re new to Docker, this guide is designed to be beginner-friendly.

Prerequisites
Before we dive in, make sure you have the following tools installed on your machine:

-Docker Desktop: This is essential for building and running Docker containers.
https://docs.docker.com/desktop/install/windows-install/

-Git Client: For cloning repositories.https://git-scm.com/downloads

-IDE or Text Editor: Docker recommends Visual Studio Code, but any editor will work. In this write up, I will be using VM workstation pro.

Getting the Application
A. Clone repo from Github

First, we need the source code for our application. We’ll be using a simple todo list manager that’s already prepared for us. To get this code, clone the repository using the command:

First, we need to get the source code for the todo list manager. Open terminal and clone the repository using: git clone https://github.com/docker/getting-started-app.git

B. Build the app’s image.

In order to containerize our application, we will need to create a Docker image using a Dockerfile. This file contains a set of instructions that Docker will use to build our container.

1. CD into the getting-started-app directory- Navigate to the getting-started-app directory and create a Dockerfile. Depending on your operating system, use one of the following commands: cd /path/to/getting-started-app

Image description

2. Create the Dockerfile
Navigate to the getting-started-app directory and create a Dockerfile. Depending on your operating system, use one of the following commands:

Image description

3. Add Instructions to the Dockerfile

Open the Dockerfile with your text editor i.e VIM and add the following content: vim Dockerfile

Image description

Here’s a brief overview of what each line does:

Here's a brief overview of what each line does:

FROM node:18-alpine: Specifies the base image with Node.js installed.
WORKDIR /app: Sets the working directory inside the container.
COPY . .: Copies the current directory contents into the container.
RUN yarn install --production: Installs the application's dependencies.
CMD ["node", "src/index.js"]: Defines the default command to run the application.
EXPOSE 3000: Informs Docker that the container will listen on port 3000.

3. Build the Docker Image

In the terminal, we will use the bellow command to build the Docker image :

Image description

This command uses the Dockerfile to create an image named getting-started. The -t flag tags the image with a name, making it easier to refer to. The . at the end tells Docker to look for the Dockerfile in the current directory.

C. Start the getting-started App Container

Now that your image is ready, we can run it as a container:

Image description

Here’s what the flags mean:

-d: Runs the container in the background.
-p 127.0.0.1:3000:3000: Maps port 3000 of the container to port 3000 on your localhost.
After running this command, we can access the todo list app by opening a browser and navigating to http://localhost:3000. The application is up and running. Without the port mapping, you wouldn’t be able to access the application from the host. Feel free to add items to your todo list and check that everything functions as expected.

Image description

D. Verify the Container

To ensure that our container is running, we will use the Docker Dashboard or run: docker ps

Image description

The out will be as below.

Image description

This will list all running containers. You should see our container with the getting-started image and port 3000 mapped correctly.

Summary

Congratulations! You’ve successfully containerized a Node.js application. By creating a Dockerfile, building an image, and running a container, you’ve taken your first steps into Docker containerization. This process not only makes your application easier to deploy but also ensures consistency across different environments. Happy containerizing!

Top comments (0)