Create a PyTest container in Docker

Introduction

In this article you can study an example of creating a minimal container with pytest installed as well as a simple container based on Ubuntu with the preinstalled framework PyTest

To understand what is going on, you need the basics of Docker , Docker Compose , Dockerfile , Ubuntu , Python requirements

Project structure

Let's create a simple structure of the following original kind:

docker/ ├── docker-compose.yml ├── dockerfiles │ ├── Dockerfile.pytest │ └── requirements.pytest.txt └── src └── README.md

mkdir docker docker/dockerfiles docker/src touch docker/docker-compose.yml docker/src/README.md docker/dockerfiles/Dockerfile.pytest docker/dockerfiles/requirements.pytest.txt

Python container

Let's take as a base the official Python container - in this case you do not need to install Python separately.

Let's edit the Docker settings files:

Dockerfile and docker-compose.yml

And the file with Python package requirements requirements.pytest.txt using the editor vim or any other editor of your choice.

vi dockerfiles/Dockerfile

# Dockerfile.pytest FROM python:3.13.0rc2-bullseye LABEL maintainer="www.devhops.site" # install dependencies COPY requirements.pytest.txt . RUN python -m pip install -r requirements.pytest.txt ENV PYTHONPATH="/opt:$PATH"

vi docker-compose.yml

If there are several containers, a network is created automatically, and you can reach the containers by their names in it.

# docker-compose.yml services: pytest: container_name: pytest_container image: pytest_image build: context: ./dockerfiles dockerfile: Dockerfile.pytest args: buildversion: 1 ports: - "3000:3000" volumes: - ./src:/opt tty: true command: tail -F /dev/null

If for some reason you need to specify the network and an address in it explicitly, the file will be somewhat longer

# docker-compose.yml services: pytest: container_name: pytest_container image: pytest_image build: context: ./dockerfiles dockerfile: Dockerfile.pytest args: buildversion: 1 ports: - "3000:3000" networks: net1: ipv4_address: 10.5.0.2 volumes: - ./src:/opt tty: true command: tail -F /dev/null networks: net1: driver: bridge ipam: config: - subnet: 10.5.0.0/16 gateway: 10.5.0.1

vi requirements.pytest.txt

# requirements.pytest.txt pytest==8.3.3 requests==2.32.3

When all the files are ready you need to run docker compose

docker compose up -d

docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c69b88ea3ae1 pytest_image "tail -F /dev/null" About a minute ago Up About a minute 0.0.0.0:3000->3000/tcp pytest_container

Now you can enter the container using the command exec

docker exec -it pytest_ubuntu bash

# python -m pytest -V

pytest 8.3.3

Let's check that README.md is available in /opt

cd opt
ls

README.md

In this almost empty directory you can do a test run of pytest

python -m pytest

============================================================================== test session starts ============================================================================== platform linux -- Python 3.13.0rc2, pytest-8.3.3, pluggy-1.5.0 rootdir: /opt collected 0 items ============================================================================= no tests ran in 0.12s =============================================================================

Ubuntu container

If the Python container does not suit you, you can use a container with Linux , for example Ubuntu .

In this case you need to take care of installing Python yourself

FROM ubuntu:24.04 LABEL maintainer="www.devhops.site" ENV TEST_DIR=/opt ENV PYTHONPATH=TEST_DIR ENV TZ=Europe/Helsinki ENV DEBIAN_FRONTEND=noninteractive RUN mkdir -p $TEST_DIR WORKDIR $TEST_DIR # old way - ADD # ADD src $TEST_DIR # new way - create VOLUME VOLUME $TEST_DIR # apt-get is recommended for Docker files # install linux utils RUN apt-get -y update \ && apt-get install -y dialog \ && apt-get install -y apt-utils \ && apt-get install -y tree vim \ && apt-get install -y curl gcc g++ make \ && apt-get -y update \ && apt-get -y upgrade # install python RUN apt-get install -y python3 python3-pip \ && apt-get install -y curl gcc g++ make \ && cd /usr/local/bin \ && ln -s /usr/bin/python3 python \ && python -m pip install --upgrade pip # install dependencies COPY requirements.ubuntu.pytest.txt . RUN python -m pip install -r requirements.ubuntu.pytest.txt

# requirements.ubuntu.pytest.txt pytest==7.2.0 requests==2.28.1

# docker-compose.yml version: '3.8' services: pytest_ubuntu: container_name: pytest_ubuntu image: pytest_ubuntu # priveleged: true # user: root build: context: ./dockerfiles dockerfile: Dockerfile.ubuntu.pytest args: buildversion: 1 ports: - "3000:3000" networks: net1: ipv4_address: 10.5.0.2 volumes: - ./src:/opt tty: true command: tail -F /dev/null networks: net1: driver: bridge ipam: config: - subnet: 10.5.0.0/16 gateway: 10.5.0.1

Article author: Andrei Olegovich