0
0
Dockerdevops~30 mins

Running tests in containers in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Running Tests in Containers
📖 Scenario: You are working on a small Python project. You want to run your tests inside a Docker container to make sure they work the same way on any computer.
🎯 Goal: Build a Docker setup that runs Python tests inside a container using docker run.
📋 What You'll Learn
Create a Dockerfile that uses the official Python 3.12 image
Copy the test script into the container
Set a command to run the test script
Run the container and see the test output
💡 Why This Matters
🌍 Real World
Running tests inside containers ensures your code works the same on any machine, avoiding the 'it works on my computer' problem.
💼 Career
Many companies use containers to run tests in continuous integration pipelines, so knowing this helps you work in modern DevOps environments.
Progress0 / 4 steps
1
Create a simple Python test script
Create a file named test_script.py with a Python test function called test_addition that checks if 2 + 2 equals 4 and prints Test passed if true.
Docker
Need a hint?

Write a function named test_addition that prints Test passed if 2 + 2 equals 4.

2
Write a Dockerfile to run the test script
Create a Dockerfile that uses the base image python:3.12-slim, copies test_script.py into the container, and sets the default command to run python test_script.py.
Docker
Need a hint?

Use FROM python:3.12-slim and copy the test script to /app. Set CMD to run the script.

3
Build the Docker image
Write the exact docker build command to build an image named test-runner from the current directory.
Docker
Need a hint?

Use docker build -t test-runner . to build the image.

4
Run the container to execute the tests
Write the exact docker run command to run the test-runner image and show the test output.
Docker
Need a hint?

Use docker run --rm test-runner to run the container and see the output.