0
0
Dockerdevops~5 mins

Running tests in containers in Docker - Commands & Configuration

Choose your learning style9 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.