Bird
Raised Fist0
Dockerdevops~5 mins

Running tests in containers in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Running tests in containers helps you check if your code works correctly in a clean, isolated environment. It avoids problems caused by differences in computers or setups.
When you want to test your application in the same environment it will run in production.
When you need to run tests without installing dependencies on your local machine.
When you want to share the exact test environment with your team to avoid 'it works on my machine' issues.
When you want to automate tests in a continuous integration system using containers.
When you want to quickly try out changes without affecting your main system.
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . ./
CMD ["pytest", "tests/"]

This Dockerfile creates a small container with Python 3.11.

It sets the working folder to /app.

It copies the requirements file and installs the needed Python packages.

It copies your app code and test files into the container.

Finally, it runs pytest to execute tests inside the container.

Commands
This command builds a Docker image named 'my-app-test' using the Dockerfile in the current folder. It prepares the test environment.
Terminal
docker build -t my-app-test .
Expected OutputExpected
Sending build context to Docker daemon 10.24kB Step 1/6 : FROM python:3.11-slim ---> 3a1b2c3d4e5f Step 2/6 : WORKDIR /app ---> Using cache ---> 6f7g8h9i0j1k Step 3/6 : COPY requirements.txt ./ ---> Using cache ---> 2l3m4n5o6p7q Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in abcdef123456 Collecting pytest Downloading pytest-7.3.1-py3-none-any.whl (320 kB) Installing collected packages: pytest Successfully installed pytest-7.3.1 Removing intermediate container abcdef123456 ---> 9r8s7t6u5v4w Step 5/6 : COPY . ./ ---> 1x2y3z4a5b6c Step 6/6 : CMD ["pytest", "tests/"] ---> Running in 7d8e9f0g1h2i Removing intermediate container 7d8e9f0g1h2i ---> 3j4k5l6m7n8o Successfully built 3j4k5l6m7n8o Successfully tagged my-app-test:latest
→
-t - Names the image for easy reference
This command runs the 'my-app-test' image as a container to execute the tests. The --rm flag removes the container after tests finish.
Terminal
docker run --rm my-app-test
Expected OutputExpected
============================= test session starts ============================== collecting ... collected 3 items tests/test_example.py ... [100%] ============================== 3 passed in 0.12s ===============================
→
--rm - Automatically removes the container after it exits
Key Concept

If you remember nothing else from this pattern, remember: running tests inside containers ensures your tests run in a clean, consistent environment every time.

Common Mistakes
Not copying the test files into the container before running tests.
The tests won't be found inside the container, so no tests will run.
Use COPY to include your test files in the Docker image before running tests.
Running the container without the --rm flag during testing.
Containers accumulate and waste disk space if not removed after tests.
Add --rm to docker run to clean up containers automatically after tests.
Not installing test dependencies inside the container.
Tests may fail because required packages are missing.
Include test dependencies in requirements.txt and install them in the Dockerfile.
Summary
Create a Dockerfile that sets up the test environment and runs tests with pytest.
Build the Docker image using 'docker build -t my-app-test .'.
Run tests inside a container with 'docker run --rm my-app-test' to see results in a clean environment.

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