Running tests in containers in Docker - Time & Space Complexity
When running tests inside Docker containers, it is important to understand how the time taken grows as the number of tests increases.
We want to know how the total test execution time changes when we add more tests to run in containers.
Analyze the time complexity of the following Docker commands used to run multiple tests sequentially.
# Run tests one by one in separate containers
for test_script in test1.sh test2.sh test3.sh; do
docker run --rm -v "$PWD":/app -w /app my-test-image sh "$test_script"
done
This script runs three test scripts one after another, each inside a new Docker container.
Look for repeated actions that affect total time.
- Primary operation: Starting a Docker container and running a test script inside it.
- How many times: Once for each test script in the list.
Each test runs in its own container, so total time adds up with more tests.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 tests | 3 container runs |
| 10 tests | 10 container runs |
| 100 tests | 100 container runs |
Pattern observation: Total time grows directly with the number of tests because each test runs separately.
Time Complexity: O(n)
This means the total time increases in a straight line as you add more tests to run in containers.
[X] Wrong: "Running tests in containers happens all at once, so time stays the same no matter how many tests there are."
[OK] Correct: Each container starts and runs separately, so time adds up with each test unless you run them in parallel.
Understanding how test execution time grows helps you design better test workflows and explain your choices clearly in real projects.
What if we ran all tests in parallel containers instead of one after another? How would the time complexity change?
Practice
Solution
Step 1: Understand container isolation
Docker containers run applications in isolated environments separate from the host system.Step 2: Benefits for testing
This isolation ensures tests run in a clean, consistent environment every time, avoiding conflicts with local setup.Final Answer:
It creates a clean and consistent test environment isolated from your local machine. -> Option AQuick Check:
Container isolation = clean test environment [OK]
- Assuming containers speed up tests automatically
- Believing Docker fixes code bugs
- Thinking tests run without commands
docker run commands correctly runs tests inside a container and removes the container after completion?Solution
Step 1: Understand the --rm flag
The--rmoption tells Docker to remove the container after it finishes running.Step 2: Match command to requirement
docker run --rm myapp pytest uses--rmand runspytestinside the container, which fits the question.Final Answer:
docker run --rm myapp pytest -> Option DQuick Check:
--rm removes container after run [OK]
- Using -d runs container detached, not suitable for tests
- Forgetting --rm leaves stopped containers
- Using --name does not remove container automatically
docker run --rm -v $(pwd):/app -w /app python:3.12 pytest test_sample.pyWhat will happen when you run it?
Solution
Step 1: Analyze volume mount and working directory
The-v $(pwd):/appmounts current folder to /app in container;-w /appsets working directory inside container.Step 2: Understand command execution
The container runspytest test_sample.pyinside /app, using the mounted code, then--rmremoves container after.Final Answer:
Runs pytest on test_sample.py inside the container using the current directory code, then removes the container. -> Option BQuick Check:
Volume mount + pytest run + --rm = Runs pytest on test_sample.py inside the container using the current directory code, then removes the container. [OK]
- Thinking tests run on host, not inside container
- Assuming container stays after tests
- Ignoring volume mount effect
docker run -v $(pwd):/app -w /app python:3.12 pytestBut the container stays running after tests finish. What is the likely cause?
Solution
Step 1: Check container removal flags
Without--rm, Docker keeps the container stopped but present after run.Step 2: Verify other flags
Volume mount and working directory flags do not affect container removal behavior.Final Answer:
You forgot to add the--rmflag to remove the container after running. -> Option CQuick Check:
Missing --rm = container stays [OK]
- Blaming volume mount for container state
- Thinking working directory affects container lifecycle
- Assuming pytest missing causes container to stay
API_KEY and DEBUG=true. Which command correctly runs tests with these variables and cleans up after?Solution
Step 1: Set environment variables correctly
Use-e VAR=valueto pass environment variables with values into the container.Step 2: Ensure container cleanup
The--rmflag removes the container after tests finish, keeping environment clean.Final Answer:
docker run --rm -v $(pwd):/app -w /app -e API_KEY=12345 -e DEBUG=true python:3.12 pytest -> Option AQuick Check:
Use -e VAR=value and --rm for env vars and cleanup [OK]
- Passing -e without values
- Omitting --rm causing leftover containers
- Appending env vars as command arguments
Ready to prove it?
Pass the test and this pattern moves unknown
