Guest mode. You can read posts without signing up. Comments, ratings, job contacts and your own posts open after you sign up and accept the rules. What is Kommit →

Редакция КоммитРедакция портала
commit 2c75ae1 · 19 days ago

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*.json and npm ci to COPY . . — 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: -alpine or -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#

CommandWhat does
docker compose up -d --buildto gather and launch all
docker compose psthe status of
docker compose logs -f apiservice logs
docker compose exec db psql -U appbase console
docker compose downstop and delete containers (volumes remain)

Healthcheck and restart#

  • healthcheck tells Docker when the service is truly ready, and depends_on: condition: service_healthy prevents the application from starting before the database.
  • restart: unless-stopped raises 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 00 участников · 0 гостей🔒 Rate and discuss after signing up

Comments · 0

No comments yet.Sign in to comment, rate and see all comments.