Bird
Raised Fist0
Dockerdevops~10 mins

Running tests in containers in Docker - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Running tests in containers
Write test code
↓
Create Dockerfile with test environment
↓
Build Docker image
↓
Run container from image
↓
Execute tests inside container
↓
View test results
↓
Stop and remove container
This flow shows how to write tests, build a Docker image with the test setup, run tests inside a container, and then view results.
Execution Sample
Docker
FROM python:3.12-slim
WORKDIR /app
COPY . /app
RUN pip install pytest
CMD ["pytest"]
Dockerfile sets up a Python 3.12 environment, copies test code, installs pytest, and runs tests when container starts.
Process Table
StepActionCommand/OperationResult/Output
1Write test codeCreate test_example.py with simple testTest file ready
2Create DockerfileWrite Dockerfile with Python and pytestDockerfile ready
3Build imagedocker build -t test-image .Image 'test-image' created
4Run containerdocker run --rm test-imageContainer starts and runs pytest
5Execute testspytest runs inside containerTests pass or fail output shown
6View resultsOutput displayed in terminalTest summary visible
7Container stopsContainer exits after testsContainer removed (--rm flag)
💡 Container stops after tests complete and --rm flag removes it automatically
Status Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 7
test codenonetest_example.py createdcopied into imageused inside containergone with container
Docker imagenonenonetest-image builtused to run containerexists locally
containernonenonenonerunning testsstopped and removed
Key Moments - 3 Insights
Why do we use the --rm flag when running the container?
The --rm flag automatically removes the container after tests finish, keeping your system clean as shown in step 7 of the execution table.
How does the test code get inside the container?
The test code is copied into the image during build (step 3), so when the container runs (step 4), it has the test files ready to execute.
What happens if tests fail inside the container?
The pytest output will show failure details in step 5, but the container still stops and is removed as per step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the Docker image created?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Build image' action in the execution table rows
According to the variable tracker, what happens to the container after step 7?
AIt is stopped and removed
BIt is stopped but remains on disk
CIt keeps running
DIt restarts automatically
💡 Hint
Look at the 'container' variable state after step 7 in the variable tracker
If you remove the COPY command from the Dockerfile, what changes in the execution?
ATests will run successfully
BTests will fail because test code is missing
CImage build will fail
DContainer will not start
💡 Hint
Refer to how test code is copied into the image in the execution table and variable tracker
Concept Snapshot
Running tests in containers:
- Write test code and Dockerfile
- Build image with test environment
- Run container to execute tests
- Use --rm to auto-remove container
- View test results in terminal output
Full Transcript
This visual execution shows how to run tests inside Docker containers. First, you write your test code and create a Dockerfile that sets up the test environment. Then you build a Docker image that includes your tests. Running a container from this image executes the tests inside it. The container outputs test results to your terminal and stops automatically if you use the --rm flag, which also removes the container to keep your system clean. The test code is copied into the image during build, so it is available inside the container. Even if tests fail, the container stops and is removed as expected.

Practice

(1/5)
1. What is the main benefit of running tests inside Docker containers?
easy
A. It creates a clean and consistent test environment isolated from your local machine.
B. It makes tests run faster than on any other system.
C. It automatically fixes bugs in your code during testing.
D. It allows tests to run without writing any test commands.

Solution

  1. Step 1: Understand container isolation

    Docker containers run applications in isolated environments separate from the host system.
  2. Step 2: Benefits for testing

    This isolation ensures tests run in a clean, consistent environment every time, avoiding conflicts with local setup.
  3. Final Answer:

    It creates a clean and consistent test environment isolated from your local machine. -> Option A
  4. Quick Check:

    Container isolation = clean test environment [OK]
Hint: Think: containers keep tests separate from your computer [OK]
Common Mistakes:
  • Assuming containers speed up tests automatically
  • Believing Docker fixes code bugs
  • Thinking tests run without commands
2. Which of the following docker run commands correctly runs tests inside a container and removes the container after completion?
easy
A. docker run -it myapp pytest
B. docker run --name testcontainer myapp pytest
C. docker run -d myapp pytest
D. docker run --rm myapp pytest

Solution

  1. Step 1: Understand the --rm flag

    The --rm option tells Docker to remove the container after it finishes running.
  2. Step 2: Match command to requirement

    docker run --rm myapp pytest uses --rm and runs pytest inside the container, which fits the question.
  3. Final Answer:

    docker run --rm myapp pytest -> Option D
  4. Quick Check:

    --rm removes container after run [OK]
Hint: Use --rm to auto-remove containers after tests [OK]
Common Mistakes:
  • Using -d runs container detached, not suitable for tests
  • Forgetting --rm leaves stopped containers
  • Using --name does not remove container automatically
3. Given this command:
docker run --rm -v $(pwd):/app -w /app python:3.12 pytest test_sample.py
What will happen when you run it?
medium
A. Mounts the current directory but runs tests on the host machine, not inside the container.
B. Runs pytest on test_sample.py inside the container using the current directory code, then removes the container.
C. Starts a container but does not run any tests because pytest is missing.
D. Creates a container but keeps it running after tests finish.

Solution

  1. Step 1: Analyze volume mount and working directory

    The -v $(pwd):/app mounts current folder to /app in container; -w /app sets working directory inside container.
  2. Step 2: Understand command execution

    The container runs pytest test_sample.py inside /app, using the mounted code, then --rm removes container after.
  3. Final Answer:

    Runs pytest on test_sample.py inside the container using the current directory code, then removes the container. -> Option B
  4. Quick 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]
Hint: Mount code and run tests inside container with --rm [OK]
Common Mistakes:
  • Thinking tests run on host, not inside container
  • Assuming container stays after tests
  • Ignoring volume mount effect
4. You run this command to test your app:
docker run -v $(pwd):/app -w /app python:3.12 pytest
But the container stays running after tests finish. What is the likely cause?
medium
A. The -w flag causes the container to hang.
B. The -v flag is incorrect and prevents container exit.
C. You forgot to add the --rm flag to remove the container after running.
D. pytest command is missing, so container never runs tests.

Solution

  1. Step 1: Check container removal flags

    Without --rm, Docker keeps the container stopped but present after run.
  2. Step 2: Verify other flags

    Volume mount and working directory flags do not affect container removal behavior.
  3. Final Answer:

    You forgot to add the --rm flag to remove the container after running. -> Option C
  4. Quick Check:

    Missing --rm = container stays [OK]
Hint: Add --rm to auto-remove containers after tests [OK]
Common Mistakes:
  • Blaming volume mount for container state
  • Thinking working directory affects container lifecycle
  • Assuming pytest missing causes container to stay
5. You want to run tests inside a container using your local code, but your tests require environment variables API_KEY and DEBUG=true. Which command correctly runs tests with these variables and cleans up after?
hard
A. docker run --rm -v $(pwd):/app -w /app -e API_KEY=12345 -e DEBUG=true python:3.12 pytest
B. docker run --rm -v $(pwd):/app -w /app -e API_KEY -e DEBUG=true python:3.12 pytest
C. docker run -v $(pwd):/app -w /app -e API_KEY=12345 -e DEBUG=true python:3.12 pytest
D. docker run --rm -v $(pwd):/app -w /app python:3.12 pytest API_KEY=12345 DEBUG=true

Solution

  1. Step 1: Set environment variables correctly

    Use -e VAR=value to pass environment variables with values into the container.
  2. Step 2: Ensure container cleanup

    The --rm flag removes the container after tests finish, keeping environment clean.
  3. Final Answer:

    docker run --rm -v $(pwd):/app -w /app -e API_KEY=12345 -e DEBUG=true python:3.12 pytest -> Option A
  4. Quick Check:

    Use -e VAR=value and --rm for env vars and cleanup [OK]
Hint: Pass env vars with -e VAR=value and add --rm [OK]
Common Mistakes:
  • Passing -e without values
  • Omitting --rm causing leftover containers
  • Appending env vars as command arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard