0
0
Dockerdevops~10 mins

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

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