QA AND TEST AUTOMATION · PART 8 · CI/CD AND TEST INFRASTRUCTURE · INFRA · 76 OF 112

Docker for Testers: Test Environments in Containers

Why testers use Docker: the same environment on every laptop and CI agent. Images, containers, ports, volumes and environment variables, using ShopRung's own Dockerfile and compose file, a compose setup that waits for a health check and runs the tests in the official Playwright image, Selenium Grid in containers, Testcontainers for throwaway databases, and common Docker mistakes.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Images and containers
  • ShopRung's Dockerfile
  • compose with healthchecks
  • service names
  • pinned images
  • Testcontainers

Notes

The words

A few words. An image is a read only package: an operating system layer, a runtime like Node, and the application. A container is a running copy of an image, isolated from the machine and from other containers. Port mapping connects a port on your machine to a port inside the container. A volume keeps data after a container is removed. And environment variables configure the container when it starts.

Docker basics
  • image: a read-only recipe (OS + runtime + app)
  • container: a running copy of an image, isolated
  • port mapping: host 8080 → container 8080
  • volume: data that outlives the container
  • env vars: configure at start

ShopRung's Dockerfile

Here is ShopRung's real Dockerfile. From starts with a small official Node twenty four image. Workdir sets the folder. Copy brings in package dot json, the source and the public files; ShopRung has no dependencies to install. Env sets the port, expose documents it, and CMD is the command that runs when the container starts. Testers should be able to read files like this, because they define the environment under test.

practice/shoprung/Dockerfile
FROM node:24-alpine
WORKDIR /app
COPY package.json ./
COPY src ./src
COPY public ./public
ENV PORT=8080
EXPOSE 8080
CMD ["node",
  "--disable-warning=ExperimentalWarning",
  "src/server.js"]

Run it

ShopRung's compose file builds the image, maps port eight thousand and eighty, passes the SHOPRUNG BUGS variable and keeps the database in a volume. Up with build and dash d starts it in the background. Ps shows what is running, logs dash f follows the app's log, and exec opens a shell inside. Down stops it; down dash v also deletes the volume, which gives you a clean database. Set SHOPRUNG BUGS to turn on a practice bug.

Everyday commands
docker compose up --build -d
docker compose ps
docker compose logs -f shoprung
docker compose exec shoprung sh
docker compose down      # stop
docker compose down -v   # + data

# turn on a practice bug:
SHOPRUNG_BUGS=coupon-boundary \
  docker compose up -d

App plus tests

To run tests against the app, add a second service in a separate compose file for tests. The shop gets a health check that calls its health endpoint every two seconds. The e2e service uses the official Playwright image, which already contains the browsers and their system libraries. It depends on the shop with the condition service healthy, so the tests start only when the shop answers. No sleeps, no guessing.

compose.test.yaml
services:
  shoprung:
    build: ./shoprung
    healthcheck:
      test: ["CMD", "wget", "-qO-",
        "http://localhost:8080/api/health"]
      interval: 2s
  e2e:
    image: mcr.microsoft.com/
      playwright:v1.63.0-noble
    depends_on: { shoprung: ...

Names, not localhost

A common surprise: inside the tests container, localhost means the tests container itself. Compose puts services on one network where each service name is a host name, so the base URL is http colon slash slash shoprung colon eight thousand and eighty. Your suite reads it from an environment variable. The tests folder is mounted as a volume, and exit code from e2e makes the whole command pass or fail with the test result, which is exactly what CI needs.

e2e service, continued
    depends_on:
      shoprung:
        condition: service_healthy
    environment:
      BASE_URL: http://shoprung:8080
    working_dir: /e2e
    volumes: ["./e2e:/e2e"]
    command: npx playwright test

docker compose -f compose.test.yaml
  up --exit-code-from e2e

Pin the image

Pin the image tag to the same Playwright version as your package file; a mismatch is a classic error, because each Playwright release expects its own browser builds. For Selenium, the Selenium project publishes Grid images: a hub with Chrome and Firefox nodes, or a standalone Chrome image. Never use the latest tag in CI. GitHub Actions can run a whole job inside an image with the container key, and Jenkins with a Docker agent.

Browser images
  • Playwright: mcr.microsoft.com/ playwright:v1.63.0-noble = same version as package.json
  • Selenium Grid: selenium/hub + node-chrome, node-firefox or standalone-chrome
  • tags pinned, never :latest
  • CI: run the job inside the image (container: in Actions, agent { docker } in Jenkins)

Testcontainers

Testcontainers is a library that starts containers from inside test code. In Java, a container field annotated with at container starts a real Postgres database before the tests and removes it afterwards; the test asks it for the connection URL. You get a real database, fresh for every run, instead of a mock or a shared server. Libraries exist for Python, Node, dot net and Go too. It needs a Docker engine on the machine.

Testcontainers (Java)
@Container
static PostgreSQLContainer<?> db =
  new PostgreSQLContainer<>(
    "postgres:17-alpine");

@Test void savesOrder() {
  var url = db.getJdbcUrl();
  // real database, fresh per run
}
// also: Python, Node, .NET, Go

Why it matters

What you gain. The same versions everywhere. Fresh data for each run, because a new container starts clean. A new team member runs one command instead of following a long setup document. CI agents only need Docker. And you can reproduce a CI failure on your laptop with the same image. The costs are image size, start up time, and learning Docker, which is now a normal expectation for SDET roles.

What you gain
  • same versions everywhere
  • fresh data each run (down -v, new container)
  • new team member: one command
  • CI agents stay simple
  • reproduce a CI failure on your laptop exactly
  • cost: image size, start time, Docker knowledge

Common mistakes

Docker mistakes. Calling localhost from inside a container when the app is another service. Sleeping instead of using a health check. Latest tags, so the environment changes without anyone noticing. A Playwright image that does not match the installed Playwright version. Old volumes that keep data between runs by accident. And secrets copied into images, where anyone who pulls the image can read them.

Avoid these
  • localhost from inside a container
  • sleep instead of a healthcheck
  • :latest image tags
  • browser image version not matching Playwright
  • data kept between runs by accident (old volume)
  • secrets baked into images

Say it like this

In the interview: compose starts the application with a health check, and the tests run in the official Playwright image, pinned to our version. The tests wait for the service to be healthy and use the service name as the base URL, and exit code from makes CI pass or fail. Each run gets fresh volumes. For cross browser runs we use Selenium Grid images, and Testcontainers gives integration tests a real, throwaway database.

In the interview
  • "Compose starts the app with a healthcheck and the tests in the pinned Playwright image;
  • tests wait on service_healthy
  • and use the service name as the base URL. exit-code-from makes
  • CI pass or fail. Fresh volumes
  • per run, Selenium Grid images
  • for cross-browser, and Testcontainers for real DBs."

Interview questions

Q1What is the difference between an image and a container?

An image is a read-only package (OS layer, runtime, app); a container is a running, isolated instance of an image.

Q2Why can a test in a container not reach the app on localhost?

Inside a container, localhost is the container itself; on a compose network, use the other service's name as the host.

Q3How do you make tests wait for the app in Docker Compose?

Give the app a healthcheck and set depends_on with condition: service_healthy on the test service.

Q4How does a compose test run pass or fail in CI?

docker compose up --exit-code-from <tests service> returns the test container's exit code.

Q5Which image should you use for Playwright tests?

The official mcr.microsoft.com/playwright image with a tag that matches the Playwright version in your project.

Q6What is Testcontainers?

A library that starts real services such as databases in containers from test code and removes them afterwards.

Q7How do you start from a clean database with compose?

docker compose down -v removes the volumes, so the next up starts with fresh data.

Q8Name three Docker mistakes in test setups.

Using localhost between containers, sleeping instead of healthchecks, unpinned :latest tags, and secrets baked into images.