Containerize An Application

Scaibu
7 min readSep 2, 2023

--

Introduction:
In this task, we will delve into the world of containerization by exploring how to containerize an application. The application in question is a straightforward yet functional to-do list manager that operates on Node.js. Whether you’re a seasoned Node.js developer or completely new to the world of JavaScript, you’re in the right place. This guide is designed to be accessible to everyone, regardless of your prior experience with Node.js or JavaScript. So, let’s get started on this containerization journey and learn how to package our to-do list manager into a container efficiently.

Prerequisites

Get the app

Before you can run the application, you need to get the application source code onto your machine.

Clone the getting-started-app repository open in new using the following command:

git clone https://github.com/docker/getting-started-app.git

View the contents of the cloned repository. You should see the following files and sub-directories.

getting-started-app/
--- package.json
--- README.md
--- spec/
--- src/
--- yarn.lock

Build the app’s image

To build the image for containerizing your Node.js-based todo list manager, you will need to create a Dockerfile. A Dockerfile is a fundamental component of containerization, as it provides a set of instructions that Docker uses to construct a container image. This image encapsulates your application and its dependencies, making it portable and reproducible across different environments.

A Dockerfile is essentially a plain text file with no specific file extension, and it contains a series of commands and configurations to define the environment and setup required for your application to run within a container. These instructions can include specifying a base image, copying files into the container, setting environment variables, and running commands during image build.

In the upcoming steps, we will walk you through the process of creating a Dockerfile for your todo list manager and then demonstrate how to use it to build a container image.

Certainly, you can create a Dockerfile in the same directory as your package.json file. The Dockerfile is essential for defining how your application should be containerized. Depending on your operating system, you can use different commands to create this file.

For Linux or macOS: Open your terminal and navigate to the directory where your package.json file is located (the “getting-started-app” directory), then use a text editor like nano, vim, or gedit to create and edit the Dockerfile.

cd /path/to/getting-started-app
nano Dockerfile

This will open the Nano text editor (you can replace “nano” with your preferred text editor). You can then add the necessary instructions and configurations to the Dockerfile.

For Windows (using Command Prompt or PowerShell):
Open Command Prompt or PowerShell and navigate to the directory where your `package.json` file is located (the “getting-started-app” directory). You can use a command like `cd` to change the directory, then use a text editor like `notepad` to create and edit the Dockerfile.

cd C:\path\to\getting-started-app
notepad Dockerfile

This will open Notepad, and you can add the Dockerfile content.

Here’s a simple example of what your Dockerfile might look like:

# syntax=docker/dockerfile:1

FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000

Make sure to adjust the instructions in the Dockerfile according to your specific application’s requirements.

Once you have created and edited your Dockerfile, save the changes and proceed with building the container image using Docker.

Build the image using the following commands:

In the terminal, make sure you’re in the getting-started-app directory. Replace /path/to/getting-started-app with the path to your getting-started-app directory.

cd /path/to/getting-started-app

Build the image.

docker build -t getting-started .

The `docker build` command is a critical step in containerizing your application. It leverages the instructions provided in your Dockerfile to construct a new container image. Here’s a breakdown of the process you described:

1. Downloading Base Image: When you executed the `docker build` command, it specified that you wanted to start from the `node:18-alpine` base image. If this image wasn’t already present on your local system, Docker automatically fetched it from a container registry (e.g., Docker Hub). This base image serves as the foundation for your custom image.

2. Copying Application: Following the base image download, the instructions from your Dockerfile were executed. In this case, your application’s source code, as well as the package.json and package-lock.json files, were copied into the container.

3. Dependency Installation: Docker then proceeded to run yarn install inside the container to install your application’s dependencies. This step ensures that the container has all the necessary libraries and modules to run your Node.js application.

4. Default Command: The CMD directive in the Dockerfile specifies the default command that should be executed when a container is started from this image. In your case, it is set to `[“yarn”, “start”]`, indicating that your Node.js application will be launched using `yarn start` by default.

5. Image Tagging: The -t flag followed by the image name (“getting-started” in your example) allows you to provide a human-readable name or tag to your newly created image. This tag makes it easier to reference the image when you want to run a container based on it.

6. Current Directory: The `.` at the end of the docker build command specifies that the Dockerfile should be located and used from the current directory. This is particularly useful when your Dockerfile is located in the same directory where you’re running the `docker build` command.

In summary, the docker build process involves downloading a base image, following the instructions in your Dockerfile to set up the application environment, and then tagging the resulting image. Once the image is built, you can use it to create and run containers, ensuring that your Node.js application operates consistently across different environments.

Start an app container

Now that you have an image, you can run the application in a container using the docker run command.

Run your container using the docker run command and specify the name of the image you just created:

docker run -dp 127.0.0.1:3000:3000 getting-started

You’re absolutely correct, and you’ve provided a clear explanation of the flags used in the docker run command. Let’s recap their significance:

1. -d (or — detach): This flag runs the container in the background, allowing you to continue using your terminal while the container runs independently.

2. -p (or — publish): This flag creates a port mapping, allowing network traffic to flow between the host and the container. The format for specifying port mapping is HOST:CONTAINER, where HOST is the address on the host machine, and CONTAINER is the port on the container. This mapping is essential for accessing services or applications running inside the container from the host machine or external network.

In your example, -p 127.0.0.1:3000:3000 maps port 3000 on the host (127.0.0.1 or localhost) to port 3000 inside the container. This means that any requests made to http://localhost:3000 on the host will be forwarded to the container’s port 3000, allowing you to access your application.

Without this port mapping, the application running inside the container would be isolated and inaccessible from the host, as the container runs in its own network namespace. Port mapping bridges this gap and facilitates communication between the host and the container, making your application accessible and usable.

After a few seconds, open your web browser to http://localhost:3000 open_in_new. You should see your app.

Great! It sounds like your Dockerized Node.js todo list manager is up and running. To confirm that it’s working as expected, you can follow these steps to interact with your application:

1. Access the Application: Open a web browser and navigate to http://localhost:3000 (or the appropriate URL if you used a different port mapping).

2. Add Items: Use the interface to add one or two to-do list items. You should be able to input task descriptions and save them.

3. Mark Items as Complete: Check off some of the added items to mark them as complete. Verify that the changes are reflected in the user interface, indicating that the front end successfully communicates with the backend.

4. Remove Items: Try removing some of the completed or uncompleted items from the list. The removal action should also update the user interface, confirming that the front end interacts with the backend appropriately.

By performing these actions, you are testing the functionality of your to-do list manager, including the communication between the front end and the back end, which is essential for a well-functioning application. If everything works as expected, congratulations on successfully containerizing your application and ensuring that it operates smoothly within a Docker container!

At this point, you have a running todo list manager with a few items.

If you take a quick look at your containers, you should see at least one container running that’s using the getting-started image and on port 3000. To see your containers, you can use the CLI or Docker Desktop’s graphical interface

In Docker Desktop, select the Containers tab to see a list of your containers.

Summary

In this section, you learned the basics about creating a Dockerfile to build an image. Once you built an image, you started a container and saw the running app.

--

--

Scaibu
Scaibu

Written by Scaibu

Revolutionize Education with Scaibu: Improving Tech Education and Building Networks with Investors for a Better Future

No responses yet