One minute
Containerize a Nodejs Application Using Docker
- Create a Dockerfile:
FROM node:22.20.0-alpine3.21
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]
- Create a docker-compose file for this NodeJS project:
# Change network proxy to your desired Docker network.
services:
node-app:
build:
context: .
dockerfile: Dockerfile
container_name: node-app
volumes:
- ./data:/app/data
networks:
- "proxy"
# If using traefik as a reverse proxy, this can be commented out or removed.
ports:
- 3000:3000
restart: unless-stopped
networks:
proxy:
external: true
-
Create a data folder in the project, add nodejs app files, package.json, and anything else needed for NodeJS into here.
-
Create a
.dockerignorefile in the root of the project, add this content into it
.git
*Dockerfile*
*docker-compose*
node_modules
- Lastly, run the docker compose project:
docker compose up -d
Credit for some of this guide
Credit for some of this guide goes to Oluwatobi on Freecodecamp
I have followed some of their guide, and found a dockerignore for nodejs.
Dockerignore for nodeJS: NodeJS .dockerignore