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 lis…


This content originally appeared on DEV Community and was authored by SethGiddy

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!


This content originally appeared on DEV Community and was authored by SethGiddy


Print Share Comment Cite Upload Translate Updates
APA

SethGiddy | Sciencx (2024-09-14T20:37:42+00:00) Getting Started with Docker.. Retrieved from https://www.scien.cx/2024/09/14/getting-started-with-docker-2/

MLA
" » Getting Started with Docker.." SethGiddy | Sciencx - Saturday September 14, 2024, https://www.scien.cx/2024/09/14/getting-started-with-docker-2/
HARVARD
SethGiddy | Sciencx Saturday September 14, 2024 » Getting Started with Docker.., viewed ,<https://www.scien.cx/2024/09/14/getting-started-with-docker-2/>
VANCOUVER
SethGiddy | Sciencx - » Getting Started with Docker.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/14/getting-started-with-docker-2/
CHICAGO
" » Getting Started with Docker.." SethGiddy | Sciencx - Accessed . https://www.scien.cx/2024/09/14/getting-started-with-docker-2/
IEEE
" » Getting Started with Docker.." SethGiddy | Sciencx [Online]. Available: https://www.scien.cx/2024/09/14/getting-started-with-docker-2/. [Accessed: ]
rf:citation
» Getting Started with Docker. | SethGiddy | Sciencx | https://www.scien.cx/2024/09/14/getting-started-with-docker-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.