Learning Docker Fundamentals
Published by Azat Akmyradov
Docker has become a popular tool for building, packaging, and deploying applications. It allows developers to package their application along with its dependencies, configuration, and other necessary files into a container that can run on any system. In this post, we'll walk through the steps to teach you how to use Docker to containerize a Node.js Express app.
Install Docker
Before we start using Docker, we need to install it on our machine. You can download and install Docker from the official Docker website (https://www.docker.com/).
Create a Node.js Express App
The first step to containerizing our Node.js Express app is to create the app. We'll use the We'll create super simple app so we don't have to worry about building complex container.
- Open a terminal window and create a new directory for your app:
mkdir myapp
- Change into the new directory:
cd myapp
- Initialize the directory as a Node.js project:
npm init -y
- Install Express:
node index.js
You should see the message "App listening at http://localhost:3000" in the terminal. Open a web browser and navigate to http://localhost:3000 to see the "Hello World!" message. Now we're done with the express app. Now, let's learn how we can create a Docker container.
Creating Docker container
- Create a Dockerfile
touch Dockerfile
A Dockerfile is a script that contains instructions for Docker to build a Docker image. We need to create a Dockerfile to specify how we want our app to be packaged into a Docker container.
Here's the code to put inside the Dockerfile
FROM node:lts WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD [ "npm", "start" ]
Let's break down what each line of the Dockerfile does:
- FROM node:lts: We're using the latest LTS Node.js image as the base image for our Docker container. You can go to Docker HUB to find the images you need for your container.
- WORKDIR /app: We set the working directory inside the Docker container to /app.
- COPY package*.json ./: We copy the package.json and package-lock.json files to the working directory inside the Docker container.
- RUN npm install: We run the npm install command to install the dependencies specified in the package.json file.
- COPY . .: We copy all the files in the current directory to the working directory inside the Docker container.
- EXPOSE 3000: We expose port 3000 on the Docker container. This is the port that our Node.js app will run on.
- CMD [ "npm", "start" ]: We specify the command to start our Node.js app inside the Docker container.
- Build the Docker Image
Now that we have a Dockerfile, we can build a Docker image of our app. Run the following command in your terminal to build the Docker image:
docker build --no-cache -t myapp .
- --no-cache flag indicated not to use the cache for building the image
- -t we can specify the name of the image using -t flag. In this case we set it to myapp
- Run the Docker Container
Now that we have a Docker image, we can run a Docker container from that image. Run the following command to start a Docker container:
docker run --rm -p 3000:3000 myapp
- --rm: this flag will remove the container after it exits
- -p: this flag is used to set the ports for host machine and Docker container. First port is used for PORT of the host machine and second is used for PORT of the Docker image
This will start a Docker container from the "myapp" image and map port 3000 on the Docker container to port 3000 on the host machine. You should be able to access your Node.js Express app by navigating to http://localhost:3000 in your web browser.