Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Dockerizing a Node.js Application with Multi-Stage Builds#
In this blog post, we’ll walk through the process of dockerizing a Node.js application using multi-stage builds. We’ll also explore Docker best practices and the docker init command.
Step 1: Clone the Repository#
First, let’s clone a simple Node.js application from GitHub:
1git clone https://github.com/yourusername/nodejs-express-app.git2cd nodejs-express-appStep 2: Explore docker init#
Before we create our Dockerfile, let’s explore the docker init command:
1docker initThis command initiates an interactive session to help set up a new Docker project. For our Node.js app, we might answer:
- Application platform: Node.js
- Application entrypoint:
node src/index.js - Port to expose: 3000
Docker will generate a Dockerfile, .dockerignore, and compose.yaml based on these answers.
Step 3: Create a Multi-Stage Dockerfile#
While docker init provides a good starting point, let’s create a more optimized Dockerfile using multi-stage builds:
1# Stage 1: Build2FROM node:18-alpine AS build3
4WORKDIR /app5
6COPY package*.json ./7RUN npm ci8
9COPY . .10RUN npm run build11
12# Stage 2: Production13FROM node:18-alpine14
15WORKDIR /app16
17COPY --from=build /app/dist ./dist18COPY --from=build /app/package*.json ./19RUN npm ci --only=production20
21EXPOSE 300022CMD ["node", "dist/index.js"]Let’s break down this Dockerfile:
- We use two stages:
buildand production. - In the
buildstage, we install all dependencies and build the application. - In the production stage, we only copy the built artifacts and production dependencies.
Step 4: Build the Docker Image#
Now, let’s build our Docker image:
1docker build -t nodejs-express-app .This command builds a Docker image tagged as nodejs-express-app using the Dockerfile in the current directory.
Step 5: Run the Docker Container#
With our image built, we can now run a container:
1docker run -p 3000:3000 nodejs-express-appThis command runs a container from our image, mapping port 3000 in the container to port 3000 on our host machine.
Benefits of Multi-Stage Builds#
Multi-stage builds offer several advantages:
- Smaller final image: By only copying necessary files from the build stage, we reduce the final image size.
- Improved security: Build dependencies and sensitive data don’t make it into the final image.
- Faster builds: Subsequent builds can leverage Docker’s layer caching more effectively.
- Easier maintenance: Separating build and runtime environments makes the Dockerfile easier to understand and maintain.
Best Practices for Writing Dockerfiles#
After reviewing the Docker documentation on best practices, here are some key points:
- Use official base images: They’re maintained and typically more secure.
- Minimize the number of layers: Combine commands where it makes sense (e.g., using
&&in RUN instructions). - Use .dockerignore: Exclude unnecessary files from the build context.
- Use specific tags: Instead of
latest, use specific version tags for base images. - Use multi-stage builds: As we’ve done, to optimize image size and build process.
- Order instructions from least to most frequently changing: This optimizes caching.
- Use environment variables: For values that might change between environments.
Conclusion#
We’ve successfully dockerized a Node.js application using multi-stage builds! Here’s a quick recap of the commands we used:
git clone: Clone the repositorydocker init: Initialize a new Docker projectdocker build: Build a Docker imagedocker run: Run a Docker container
By following these steps and best practices, you can create efficient, secure, and maintainable Docker images for your applications.
You can find the complete code for this project, including the Dockerfile, in my GitHub repository: https://github.com/yourusername/nodejs-express-app
Happy Dockerizing!