
Docker from scratch, part 2: Dockerfile and Docker Compose for your application
🌐 Machine-translated from Russian. Show original
In this part:
Dockerfile: image recipe#
An example for a Node.js application with multi-stage build: only what is needed for work is included in the final image, without compilers or dev dependencies.
# 1. Сборка
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev
# 2. Запуск
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY package.json ./
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]
Assembly and startup:
docker build -t my-api:1.0 .
docker run -d -p 3000:3000 my-api:1.0
Rules for a good Dockerfile#
- Dependencies first, then code.
COPY package*.jsonandnpm citoCOPY . .— then, when changing code, the dependency layer is taken from the cache and assembly takes seconds. - Not from root.
USER node(or their own user) — if the app is hacked, the attacker will have fewer rights. - Small base image:
-alpineor-slim— downloads faster, fewer vulnerabilities. - One task — one container. Database , application, and queue — different containers.
- Secrets are not in character. Passwords and keys are passed by environment variables at startup, not through
COPY .env.
.dockerignore#
Without it, node_modules, .git , and worst of all, secret files will disappear into the image:
node_modules
.git
.env*
dist
*.log
Docker Compose: the entire stack in one file#
compose.yaml describes the application, the database, and their relationships:
services:
api:
build: .
image: my-api:1.0
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
depends_on:
db:
condition: service_healthy
restart: unless-stopped
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 5s
retries: 10
restart: unless-stopped
volumes:
pgdata:
The password is placed in the file .env next to it (and it is in .gitignore!):
DB_PASSWORD=сложный-пароль
Compose commands#
| Command | What does |
|---|---|
docker compose up -d --build | to gather and launch all |
docker compose ps | the status of |
docker compose logs -f api | service logs |
docker compose exec db psql -U app | base console |
docker compose down | stop and delete containers (volumes remain) |
Healthcheck and restart#
healthchecktells Docker when the service is truly ready, anddepends_on: condition: service_healthyprevents the application from starting before the database.restart: unless-stoppedraises the container after the server crashes and reboots.
What's next#
The stack is built and run with a single command. In part 3 , we will assign the build and testing to the CI server — for each commit.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.