Allows you to put the folder ~/dev
inside the container ("/home/user/dev") and run the code using the desired version of Python.
git clone https://github.com/svnikolaev/python-docker-envinonment.git
cd python-docker-envinonment
Create secrets_file
like secrets_file.example
and set passwords for users root
and user
:
root:password1
user:password2
PYTHON_VER=3.11 docker compose up -d --build
PYTHON_VER=3.10 docker compose up -d --build
PYTHON_VER=3.11 HTTP_PROXY=http://example.com:8080 docker compose up -d --build
docker exec -it python_env bash
cd /path/to/project/dirrectory
virtualenv --python="$(which python)" .venv
source .venv/bin/activate
pip install -r requirements.txt
example:
(.venv) user@37b61e5ac0fb:~/dev/project$ python -m pytest
=============================== test session starts ===========================
platform linux -- Python 3.11.1, pytest-7.2.1, pluggy-1.0.0
rootdir: /home/user/dev/project
plugins: anyio-3.6.2
collected 2 items
tests/test_cases.py ..
================================ 2 passed in 0.84s ============================
docker compose down
You can also create a Docker image for later use.
docker build --no-cache \
--build-arg PYTHON_VER=3.11 \
--secret id=SECRETS,src=secrets_file -t python_env:3.11 .
docker build --no-cache \
--build-arg PYTHON_VER=3.10 \
--secret id=SECRETS,src=secrets_file -t python_env:3.10 .
docker build --no-cache \
--build-arg HTTP_PROXY=http://example.com:8080 \
--build-arg HTTPS_PROXY=http://example.com:8080 \
--build-arg PYTHON_VER=3.11 \
--secret id=SECRETS,src=secrets_file -t python_env:3.11 .