← All articles

Docker for QA: Containers Without the Pain

Docker comes up in interviews more and more often. Not DevOps-level — but 'spin up a test environment' and 'why do the tests fail on CI but pass locally'. I've covered everything QA actually needs to know: from docker run to docker-compose with a database and an API. With examples and interview tasks.

  1. A crate goes onto the boat: a container started from an image.
  2. A second identical crate: one image gives any number of the same containers.
  3. The third crate: the same environment for every tester and every CI run.
  4. The boat sails off: the containers run the same way on any machine.
  5. A green tick: the tests passed in a clean, repeatable environment.
Click the picture to play it again.

Everything QA needs to know about Docker — no extra DevOps, just real tasks


Docker for QA

Why Does QA Even Need Docker?

Because “it works on my machine” isn’t a bug report — it’s a cry for help.

Here’s a real situation. A tester runs the automated tests on their machine — everything’s green. Pushes to CI — three tests are red. They start digging: their machine has PostgreSQL 15, CI has PostgreSQL 14. Their machine has Node.js 20, CI has 18. Redis is running on their machine, but not in CI. Half a day spent debugging the environment instead of testing.

If the tests had been running in Docker — there would have been no problem. A container is the same everywhere: on your laptop, in CI, on a colleague’s machine. That’s the whole idea.

QA needs Docker for four things:

# Why Example
1 Identical environment Locally, in CI, on a colleague’s machine — the same version of everything
2 Fast dependency startup Need a database for tests? docker run postgres — 5 seconds
3 Isolation The test suite doesn’t break your system, and a container can be removed without a trace
4 Bug reproduction “The bug only happens in production” — spin up the same container and check

What Is Docker (in Plain Words)

Imagine you ordered a pizza. They bring you a box, and inside is a ready-made pizza. You don’t need to know what oven the kitchen has, what brand of flour they use, or what the dough recipe is. You open the box — and eat.

Docker works the same way. Instead of installing PostgreSQL, Node.js, Redis and 15 more dependencies on your machine — you run a container that already has all of it installed and configured. A container is a box with a ready-to-go application.

Three key concepts:

  • Image — the recipe. A description of what’s inside the container. Downloaded once.
  • Container — a running instance of an image. Like a running program.
  • Dockerfile — the instructions for building an image. A text file with commands.

An analogy: an image is a class, a container is an object (an instance of the class). You can run 10 containers from a single image.


Installing Docker

Windows

Download Docker Desktop and install it. After installation, a whale icon will appear in the system tray. If the whale is happy — Docker is running.

macOS

Docker Desktop as well. Download → drag to Applications → launch.

Linux

sudo apt-get update
sudo apt-get install docker.io docker-compose-v2
sudo usermod -aG docker $USER

After installation, log out and back in, then check:

docker --version

If you see something like Docker version 27.x.x — everything works.


docker run — Running Your First Container

Let’s start simple. We’ll run PostgreSQL:

docker run --name test-db -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres:16

Let’s break it down piece by piece:

Part What it does
docker run Create and start a container
--name test-db Give the container a name (so you don’t have to remember the ID)
-e POSTGRES_PASSWORD=secret Environment variable (the database password)
-p 5432:5432 Publish a port: host_port:container_port
-d Run in the background (detached)
postgres:16 Image and version

Five seconds later you have PostgreSQL running. Connect to it via DBeaver, pgAdmin or from your tests — localhost:5432, user postgres, password secret.

Trap: -p 5432:5432 — the port on the left is the port on your machine. If 5432 is already taken (you have Postgres installed locally), use -p 5433:5432. You connect to localhost:5433, while inside the container it’s still 5432.


Essential Docker Commands

Here’s the minimum you need to know:

Container Lifecycle

docker run -d --name my-app nginx
docker ps
docker stop my-app
docker start my-app
docker restart my-app
docker rm my-app
docker rm -f my-app
Command What it does
docker run Create + start a container
docker ps List running containers
docker ps -a List all containers (including stopped ones)
docker stop Stop a container
docker start Start a stopped container
docker rm Remove a container
docker rm -f Force remove (even a running one)

Working with Images

docker images
docker pull nginx:latest
docker rmi nginx:latest
Command What it does
docker images List downloaded images
docker pull Download an image
docker rmi Remove an image

Debugging

docker logs my-app
docker logs -f my-app
docker exec -it my-app bash
docker inspect my-app
Command What it does
docker logs View container logs
docker logs -f Follow logs in real time (like tail -f)
docker exec -it ... bash Get inside the container
docker inspect Detailed information about the container (network, ports, variables)

Interview task: “Tests on CI fail with a connection refused error to the database. How do you debug it?”

Answer: docker ps — make sure the database container is running. docker logs <name> — check for errors on startup. docker inspect <name> — check which port and which network the container is on. A common cause: the application container and the database container are in different Docker networks.


Dockerfile — Building Your Own Image

For QA this matters when you need to build an image with your tests. For example, packaging Selenium tests into a container to run them in CI.

Structure

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["pytest", "--tb=short", "-v"]

Breakdown:

Instruction What it does
FROM Base image (what we build on)
WORKDIR Working directory inside the container
COPY Copy files from the host into the container
RUN Execute a command while building the image
CMD Default command when the container starts

Build and Run

docker build -t my-tests .
docker run my-tests

-t my-tests — gives the image a tag (a name). The dot . is the build context (the current folder, where the Dockerfile lives).

Common Dockerfile Mistakes

1. COPY before installing dependencies:

# ❌ Bad — every code change rebuilds the dependencies
COPY . .
RUN pip install -r requirements.txt

# ✅ Good — dependencies are cached
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .

Docker caches layers. If requirements.txt hasn’t changed — pip install won’t run again. But if you copy all the code first, any change in a single file invalidates the cache.

2. Using latest instead of a specific version:

# ❌ Today it's Python 3.12, tomorrow 3.13 — and the tests are broken
FROM python:latest

# ✅ Pinned version
FROM python:3.12-slim

Docker Compose — Spinning Up the Whole Environment

docker run is a single container. But for testing you usually need several: a database, an API, a cache, a queue. Starting each one by hand is torture. Docker Compose lets you describe everything in one file and bring it all up with one command.

Example: API + PostgreSQL + Redis

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_DB: testdb
      POSTGRES_USER: tester
      POSTGRES_PASSWORD: secret
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U tester -d testdb"]
      interval: 5s
      timeout: 3s
      retries: 5

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

  api:
    image: myapp/api:latest
    ports:
      - "8080:8080"
    environment:
      DATABASE_URL: postgresql://tester:secret@db:5432/testdb
      REDIS_URL: redis://redis:6379
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started

Notice a few things:

  1. db, redis, api — service names. They also work as DNS names inside the Docker network. The API reaches the database at db:5432, not localhost:5432.

  2. depends_on — startup order. The API will start only after the database becomes healthy (passes its healthcheck).

  3. healthcheck — a check that the service is actually ready to accept connections. Without it, depends_on just waits for the container to start, not for the service to be ready.

Docker Compose Commands

docker compose up -d
docker compose ps
docker compose logs api
docker compose logs -f
docker compose down
docker compose down -v
Command What it does
up -d Bring everything up in the background
ps Status of all services
logs <service> Logs of a specific service
down Stop and remove the containers
down -v Same + remove volumes (data)

Trap: docker compose down doesn’t remove volumes. If the tests wrote garbage into the database — the data will still be there on the next up. Use down -v for a clean start.


Volumes — Data That Outlives the Container

A container is a disposable thing. Remove the container — the data inside is gone. Volumes solve this problem.

services:
  db:
    image: postgres:16
    volumes:
      - db-data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

volumes:
  db-data:

There are two different kinds of mounts here:

  • db-data:/var/lib/... — a named volume. Docker manages it itself. The database data persists between restarts.
  • ./init.sql:/docker-entrypoint-initdb.d/init.sql — a bind mount. A file from the host is mounted into the container. PostgreSQL automatically executes the SQL files in docker-entrypoint-initdb.d on first startup.

When to use which:

Type When
Named volume Database data, cache — things Docker manages itself
Bind mount Configs, init scripts, source code — things you edit on the host

Docker for Tests: Practical Scenarios

Scenario 1: A Test Database with Seed Data

Create an init.sql file:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
);

INSERT INTO users (name, email) VALUES
    ('Atajan', 'atajan@test.com'),
    ('Maria', 'maria@test.com'),
    ('John', 'john@test.com');

And a docker-compose.yml:

services:
  test-db:
    image: postgres:16
    environment:
      POSTGRES_DB: testdb
      POSTGRES_USER: tester
      POSTGRES_PASSWORD: secret
    ports:
      - "5432:5432"
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
docker compose up -d

Five seconds later you have a database with test data. Connect from your tests to localhost:5432 — and get to work. After the tests — docker compose down -v — and everything’s clean.

Scenario 2: Selenium Tests in a Container

services:
  chrome:
    image: selenium/standalone-chrome:latest
    ports:
      - "4444:4444"
      - "7900:7900"
    shm_size: "2g"

  tests:
    build: .
    depends_on:
      - chrome
    environment:
      SELENIUM_URL: http://chrome:4444/wd/hub

shm_size: "2g" is an important detail. Chrome in a container uses shared memory, and the default 64MB isn’t enough. Without this parameter the tests will fail with cryptic errors. Port 7900 is VNC — you can connect and watch what the browser is doing.

Scenario 3: A Full Test Environment

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_DB: appdb
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d appdb"]
      interval: 5s
      timeout: 3s
      retries: 5

  api:
    build: ./backend
    ports:
      - "8080:8080"
    environment:
      DATABASE_URL: postgresql://app:secret@db:5432/appdb
    depends_on:
      db:
        condition: service_healthy

  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    environment:
      API_URL: http://api:8080

  tests:
    build: ./tests
    depends_on:
      - api
      - frontend
    environment:
      BASE_URL: http://frontend:3000
      API_URL: http://api:8080

One command, docker compose up --abort-on-container-exit tests — and the whole environment comes up, the tests run, and everything stops. The --abort-on-container-exit flag shuts down all services once the tests container exits.


Networking — Why Containers Can (or Can’t) See Each Other

This is the most common source of problems. Let’s sort it out once and for all.

Rule 1: Containers in the same docker-compose.yml see each other by service name. The API reaches the database at db:5432, not localhost:5432.

Rule 2: localhost inside a container is the container itself, not your machine. If an API in a container calls localhost:5432 — it’s looking for Postgres inside itself, not in the neighboring container.

Rule 3: Ports published with -p are only needed for access from the host. Containers talk to each other over the internal network without port publishing.

┌─────────────────────────────────────────┐
│              Docker Network             │
│                                         │
│  ┌─────────┐         ┌─────────┐       │
│  │   api   │───5432──│   db    │       │
│  │  :8080  │         │  :5432  │       │
│  └────┬────┘         └─────────┘       │
│       │                                 │
└───────┼─────────────────────────────────┘
        │ -p 8080:8080
        │
   ┌────┴────┐
   │  Host   │  ← your laptop
   │  :8080  │
   └─────────┘

Interview task: “The tests connect to the database at localhost:5432 but get connection refused. The database container is running. What’s the problem?”

Answer: if the tests are also in a container — you need to use the service name (db:5432), not localhost. If the tests run on the host — make sure the port is published (-p 5432:5432).


Environment Variables and .env

Don’t hardcode passwords and URLs in docker-compose.yml. Use .env:

# .env
POSTGRES_USER=tester
POSTGRES_PASSWORD=secret
POSTGRES_DB=testdb
API_PORT=8080
services:
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      - "${DB_PORT:-5432}:5432"

${DB_PORT:-5432} — if DB_PORT isn’t set, 5432 is used. Handy for overriding on CI.

Docker Compose automatically picks up the .env file from the current directory. You don’t need to specify anything extra.


Docker in CI/CD

Docker is used on CI all the time. A typical pipeline:

# .github/workflows/tests.yml (GitHub Actions)
name: Tests
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Start services
        run: docker compose up -d

      - name: Wait for DB
        run: |
          until docker compose exec db pg_isready -U tester; do
            sleep 2
          done

      - name: Run tests
        run: docker compose run tests

      - name: Cleanup
        if: always()
        run: docker compose down -v

Key points:

  • if: always() in cleanup — the containers will be stopped even if the tests failed
  • Waiting for the database to be ready — depends_on doesn’t guarantee that Postgres is accepting connections
  • docker compose run tests starts a one-off container — it does its job and stops

Tasks from Real Interviews

Task 1: “It works locally, but not in CI”

The tests use an API at localhost:8080. Locally everything works. In CI (GitHub Actions) — connection refused. The Docker Compose file is the same. What could be the problem?

Answer: locally the tests run on the host and hit the port published with -p. In CI the tests are most likely running in a container — you need to use the service name (api:8080) instead of localhost:8080. Or run the tests via docker compose run, where the network is shared.

Task 2: “The tests affect each other”

Each test is green on its own, but together they fail. One test creates a user with the email test@test.com, and so does another. When run sequentially, the second one hits a unique constraint violation. How do you fix it with Docker?

Answer: bring up a clean database before each test suite. docker compose down -v && docker compose up -d before the run. Or use transactions — each test is wrapped in a transaction and does a ROLLBACK at the end. The Docker solution is more “brute-force”, but reliable.

Task 3: “The container starts, but the service doesn’t respond”

docker compose up -d completed successfully, docker compose ps shows all containers as running. But the API returns 502. How do you debug it?

Answer:

  1. docker compose logs api — look at the API logs
  2. docker compose logs db — maybe the database didn’t start
  3. docker compose exec api curl localhost:8080/health — check that the API responds from the inside
  4. Check the healthcheck — a container can be running but not healthy
  5. docker inspect — check the network and ports

Task 4: “Explain what this docker-compose.yml does”

The interviewer shows you a file and asks you to explain every line. A common interview format — they’re checking whether you actually understand it or just memorized the commands.

For this you need to understand:

  • What services, volumes, networks are
  • What depends_on is for and its limitations
  • The difference between build and image
  • What healthcheck does
  • Why -v in docker compose down

Task 5: “Write a docker-compose.yml for a test environment”

Required: PostgreSQL, Redis, a backend (image myapp/api:v2), and a test run (folder ./tests, the Dockerfile already exists). The tests must start after the API and the database are ready.

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_DB: testdb
      POSTGRES_USER: tester
      POSTGRES_PASSWORD: secret
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U tester -d testdb"]
      interval: 5s
      timeout: 3s
      retries: 5

  redis:
    image: redis:7-alpine
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5

  api:
    image: myapp/api:v2
    environment:
      DATABASE_URL: postgresql://tester:secret@db:5432/testdb
      REDIS_URL: redis://redis:6379
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  tests:
    build: ./tests
    environment:
      API_URL: http://api:8080
      DB_URL: postgresql://tester:secret@db:5432/testdb
    depends_on:
      api:
        condition: service_healthy

5 Traps Candidates Fall Into

1. localhost inside a container ≠ your machine.

Inside a container, localhost is the container itself. To reach a neighboring container, use the service name. To reach the host — host.docker.internal (Docker Desktop) or --network host.

2. depends_on doesn’t wait for the service to be ready.

depends_on only waits for the container to start, not for the application inside to be ready. PostgreSQL can take 10 seconds to start, while depends_on lets the dependent container go right away. Use condition: service_healthy + healthcheck.

3. Data in a container is temporary.

Remove the container — lose the data. Use volumes to keep it. For tests this is usually a plus (a clean start), but for test databases with migrations — you need to be aware of it.

4. The difference between docker compose down and docker compose down -v.

Without -v, volumes are kept. With -v — they’re removed. If the tests cluttered the database and you forgot -v — the next run may fail on dirty data.

5. The difference between CMD and ENTRYPOINT in a Dockerfile.

CMD can be overridden at docker run. ENTRYPOINT can’t (or rather, it’s harder). For tests it’s usually CMD — so you can run a specific test: docker run my-tests pytest tests/test_login.py.


Checklist: What to Know Before the Interview

  • Concepts: image vs container, Dockerfile, Docker Compose
  • Commands: run, ps, stop, rm, logs, exec, build
  • Dockerfile: FROM, COPY, RUN, CMD, WORKDIR — and layer order for caching
  • Docker Compose: services, ports, environment, volumes, depends_on, healthcheck
  • Networking: why localhost doesn’t work between containers, service names as DNS
  • Volumes: named volumes vs bind mounts, why -v in down
  • CI/CD: how Docker solves the “works on my machine” problem, waiting for services to be ready
  • Debugging: logs, exec, inspect — the three main tools

That’s enough for 90% of QA interviews. You don’t need to know Kubernetes, Docker Swarm or multi-stage builds — that’s DevOps territory already.


Useful Resources

  1. Docker Getting Started — the official tutorial. Well structured, with examples.
  2. Play with Docker — a browser sandbox. Nothing to install — Docker right in your browser.
  3. Docker Compose documentation — the docker-compose.yml reference.
  4. Awesome Docker — a curated list of resources.

Free QA course with hands-on exercises at annayev.com (English, Russian, Turkish).

Give it a thumbs up if this was useful. Do you use Docker in your day-to-day work as a tester? Tell us in the comments which scenarios come up most often for you.