A simple Python Docker workspace template for developing Python projects within Docker containers.
You could use this template to start/containerize a development environment for your Python project.
-
Congfigure
docker-compose.yml
build: context: . dockerfile: Dockerfile args: PROJECT_NAME: "my-project" # Name you project and init for you UBUNTU_VERSION: "22.04" # Choose ubuntu version PYTHON_VERSION: "3.10" # Choose python version develop: watch: - action: sync path: ./my-project # Don't forget to modify the default folder name to your project name here!
-
Start the development environment:
$ docker compose up -d
Now you will find that your
{PROJECT_NAME}folder appears in the current directory and the container is running.From,
. ├── ... ├── Dockerfile ├── docker-compose.ymlto,
. ├── ... ├── {PROJECT_NAME} # See more details in https://docs.astral.sh/uv/concepts/projects/init/#applications. ├── (Default uv project structure) ├── Dockerfile ├── docker-compose.yml -
Access docker container terminal with:
$ docker exec -it dev /bin/bashExit terminal with
CTRL + D. -
Stop docker container with:
$ docker compose down
-
Add packages
- See more details in UV Document.
e.g. Install cuda available
torch:$ uv lock --index https://download.pytorch.org/whl/cu118 $ uv add numpy==1.26.4 torch==2.1.2
-
How to use GPU in container?
For gpu usage, ensure your system meets the following requirements:
-
Windows users need to enable WSL
To run Docker with GPU support on Windows, you must enable Windows Subsystem for Linux (WSL).
Follow the instructions here: Setting up WSL.
-
Install Docker
Windows users: Download and install Docker Desktop.
Linux users: Install Docker Engine following the instructions here.
-
Install CUDA Toolkit
Ensure that your system has the CUDA Toolkit installed, which is necessary for GPU acceleration.
Refer to the guide: Installing CUDA Toolkit.
-
Install NVIDIA Container Toolkit
This is required to enable Docker containers to use the GPU.
Follow the instructions to install the NVIDIA Container Toolkit here.
5 Test your gpu util with
test_gpu_util.py. -
-
Do I need to reinstall packages in the container once it closed?
No need. The packages you intalled in the container will still be remained. All packages will be kept in the
{your-project-name}/.venvwhich will be mounted to the container.