Dockerizing a Node.js app is a useful technique that allows you to package your application and its dependencies into a container, making it easier to deploy and run consistently across different environments. In this blog post, we'll walk through the steps to dockerize a Node.js app. Let's get started!
Prerequisites
Before we begin, make sure you have the following installed on your machine:
Docker:You can download and install Docker from the official website (https://www.docker.com/).
Step 1: Set up your Node.js app
Assuming you already have a Node.js app, create a new directory for your project (if you haven't already) and navigate to it in your terminal.
Step 2: Create a Dockerfile
A Dockerfile is a text file that contains instructions for building a Docker image. Create a new file called Dockerfile in the root directory of your project and open it in a text editor.
Add the following content to your Dockerfile:
FROM node: 18-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD [ "node", "server.js" ]
Let's go through what each line does:
-
FROM node:18-alpine:Specifies the base image to use. In this case, we're using the official Node.js 14 image. -
WORKDIR /usr/src/app:Sets the working directory in the container. -
COPY package*.json ./:Copies the package.json and package-lock.json files to the working directory. -
RUN npm install:Installs the app dependencies. -
COPY . .:Copies the rest of the app source code to the working directory. -
EXPOSE 3000:Exposes the port your app is listening on (change the port number if necessary). -
CMD ["node", "app.js"]:Defines the command to start your Node.js application.
Save the Dockerfile.
Step 3: Build the Docker image
In your terminal, navigate to the root directory of your project (where the Dockerfile is located).
Run the following command to build the Docker image:
docker build -t your-image-name .
Make sure to replace your-image-name with the desired name for your Docker image. The
.at the end specifies the build context as the current directory.Docker will now execute the instructions in the Dockerfile and build the image. This might take a while, especially if it's the first time you're running this command, as Docker needs to download the base image and install the dependencies.
Step 4: Run the Docker container
Once the Docker image is built, you can create a container and run your Node.js app using the following command:
docker run --name your-container-name -p 3000:3000 your-image-name
This command maps port 3000 from the your container to port 3000 on your machine, allowing you to access the app on http://localhost:3000. Replace your-image-name with the name you specified when building the image.
You should now see your Node.js app running inside the Docker container.
Conclusion
Dockerizing a Node.js app provides several benefits, including easy deployment, consistent environments, and improved scalability. By following the steps outlined in this blog post, you've learned how to create a Dockerfile, build a Docker image, and run a Docker container for your Node.js app.
Top comments (3)
As your base image I would suggest using:
node: 18-alpine
or
node: 20-alpine
your image sizes will be a lot smaller.
Happy Coding
Thanks for your comment. I will also update that part.
adding .dockerignore to the recipe will give you a lot of optimisations.