How to Dockerize a node app
Photo by Rubaitul Azad
How to dockerize a Node.js application.
- Create a new directory for your project and navigate to it.
- Initialize a new Node.js project by running
- Install the dependencies that your application needs by running
- Create a new file called
Dockerfilein the root of your project directory. This file will define the instructions for building your Docker image.
- In your
Dockerfile, include the following instructions:
1FROM node:1223WORKDIR /app45COPY package.json .6RUN npm install78COPY . .910EXPOSE 30001112CMD ["npm", "start"]
- The first instruction,
FROM node:12, specifies that your Docker image should be based on the official Node.js 12 image from Docker Hub.
WORKDIRinstruction sets the working directory for any subsequent instructions.
COPYinstructions copy the
package.jsonfile and the application code into the Docker image.
RUNinstruction runs the
npm installcommand, which installs the dependencies listed in the
EXPOSEinstruction tells Docker to expose port 3000 on the container, which is the default port that a Node.js application listens on.
CMDinstruction specifies the command that will be run when the Docker container is started. In this case, it runs the
npm startcommand, which starts the Node.js application.
- Save your
Dockerfileand build your Docker image by running the following command:
1docker build -t my-node-app .
- This will create a new Docker image with the name
my-node-app. You can view a list of available Docker images on your system by running
- To run your Docker image as a container, you can use the
docker runcommand. For example, the following command will run your Node.js application in a new Docker container:
1docker run -p 3000:3000 my-node-app
- This command will start a new container based on the
my-node-appimage, and it will forward traffic on port 3000 on the host to port 3000 on the container. You should now be able to access your Node.js application at
To share your Docker container with others, you can push it to a Docker registry. A Docker registry is a service that stores Docker images and makes them available to users. The most popular registry is Docker Hub, which is provided by Docker.
To push your Docker image to Docker Hub, you first need to sign up for a Docker
Hub account. Once you have an account, you can log in to Docker Hub from the
command line using the
docker login command.
Once you are logged in to Docker Hub, you can use the
docker push command to
push your Docker image to your Docker Hub account. For example, if your Docker
image is called
my-node-app and your Docker Hub username is
can push your image by running the following command:
1docker push myusername/my-node-app
This will push your Docker image to Docker Hub and make it available to others.
You can share the URL of your Docker image with others, and they can use the
docker pull command to download and run the image on their own systems. For
example, they could run the following command to download and run your Docker
1docker pull myusername/my-node-app2docker run -p 3000:3000 myusername/my-node-app
This would download the
myusername/my-node-app image from Docker Hub and run
it in a new Docker container. The container would start the Node.js application
and make it available on port 3000.