Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 4 OF 5 · 2 MIN READ

Docker for Backend Developers

Docker packages your app with its runtime and dependencies into an image that runs the same everywhere. Backend interviews ask about images vs containers, writing a small and secure Dockerfile, layers, and docker compose for local development.

What you will learn

  • Explain images, containers, layers and registries
  • Write a production multi-stage Dockerfile for Node.js
  • Use docker compose for an app with PostgreSQL and Redis
  • Know container vs virtual machine

Key concepts

Image vs container

An image is a read-only template built in layers. A container is a running instance of an image with its own writable layer, process and network.

Layer caching

Each Dockerfile instruction makes a layer. Copy package files and install dependencies before copying source code, so code changes do not reinstall everything.

Container vs VM

Containers share the host kernel and isolate processes with namespaces and cgroups: they start in seconds and are small. VMs run a full guest OS on a hypervisor: stronger isolation but heavier.

Step by step

A production Dockerfile for Node.js

FROM node:24-slim AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev

FROM node:24-slim
WORKDIR /app
ENV NODE_ENV=production
COPY --from=deps /app/node_modules ./node_modules
COPY . .
USER node
EXPOSE 3000
CMD ["node", "src/server.js"]
  • Small base image
  • npm ci for reproducible installs
  • Run as non-root
  • CMD with node directly so SIGTERM reaches your process
  • .dockerignore for node_modules, .env, .git

Examples

docker compose for local development

services:
  api:
    build: .
    ports: ['3000:3000']
    environment:
      DATABASE_URL: postgres://app:app@db:5432/app
      REDIS_URL: redis://cache:6379
    depends_on: [db, cache]
  db:
    image: postgres:17
    environment: { POSTGRES_USER: app, POSTGRES_PASSWORD: app, POSTGRES_DB: app }
    volumes: [pgdata:/var/lib/postgresql/data]
  cache:
    image: redis:7
volumes:
  pgdata:

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Shrink a Node.js image and compare sizes before and after a multi-stage build.
  • Explain why npm start as CMD can break graceful shutdown.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Why should containers run as non-root?

If an attacker breaks into the app, root inside the container makes escaping to the host or damaging mounted volumes much easier. A normal user limits the damage.

Where do you keep secrets for containers?

Not in the image or the Dockerfile. Inject them at runtime from a secrets manager or orchestrator secrets (Kubernetes Secrets, AWS Secrets Manager) as environment variables or mounted files.

Summary

Images are layered templates, containers are running instances. Use small multi-stage images, order layers for caching, run as non-root, handle signals, and use compose for local stacks.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus