Basic Dockerfile Structure
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "server.js"]
.dockerignore File
node_modules
dist
.env
.git
*.log
Build and Run the Image
docker build -t myapp .
docker run -d -p 3000:3000 myapp
Key Points
- Separating
COPY package*.json ./andRUN npm ciinto two steps enables layer caching - Without
.dockerignore,node_modulesgets copied and the image becomes huge
If you want to auto-deploy the image built from a Dockerfile via CI/CD, combining it with GitHub Actions: How to Set Up Basic Auto-Deploy will greatly improve efficiency.
Related Articles
- Docker Basic Commands Cheatsheet
- How to Use docker-compose
- How to Persist Data with Docker Volumes
- How to Install Docker on Windows
Recommended Cloud Hosting
Looking for reliable cloud infrastructure? Check out these developer-friendly services.
- Cherry Servers - High-performance VPS and dedicated servers
- Cloudways - Managed cloud hosting for developers