Recommended base image
As suggested in my comment, you could write a Dockerfile that looks like:
FROM python:3
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir nibabel pydicom matplotlib pillow med2image
# Note: we had to merge the two "pip install" package lists here, otherwise
# the last "pip install" command in the OP may break dependency resolution…
CMD ["cat", "/etc/os-release"]
And the command example above could confirm at runtime (docker build --pull -t test . && docker run --rm -it test) that this image is based on the GNU/Linux distribution "Debian stable".
Generic Dockerfile template
Finally to give a comprehensive answer, note that a good practice regarding Python dependencies consists in specifying them in a declarative way in a dedicated text file (in alphabetical order, to ease review and update) so that for your example, you may want to write the following file:
requirements.txt
matplotlib
med2image
nibabel
pillow
pydicom
and use the following generic
Dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "./your-daemon-or-script.py"]
To be more precise, this is the approach suggested in the documentation of the Docker official image python, §. How to use this image
I thought today, about using Docker to setup virtual environments instead of using Pipenv because I'd like to learn Docker a bit. I watched some Python videos on it and all of these were setting up fastapi's which I don't need so I wanted to make a more generic dockerfile and yml file that I can come back to over and over again.
I created a dockerfile like below:
FROM python:3.12-slim WORKDIR /code COPY ./requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY ./src ./src
I created a docker-compose.yml file like below:
services:
app:
build: .
container_name: py_env
volumes:
- .:/codeI ran the command docker compose up --build -d and had no errors, it said Container py_env Started but when doing docker ps there is no container and when using dev containers in VS code to try to attach to a running container it says there are no running containers to attach to.
What am I missing?
Videos
Recommended base image
As suggested in my comment, you could write a Dockerfile that looks like:
FROM python:3
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir nibabel pydicom matplotlib pillow med2image
# Note: we had to merge the two "pip install" package lists here, otherwise
# the last "pip install" command in the OP may break dependency resolution…
CMD ["cat", "/etc/os-release"]
And the command example above could confirm at runtime (docker build --pull -t test . && docker run --rm -it test) that this image is based on the GNU/Linux distribution "Debian stable".
Generic Dockerfile template
Finally to give a comprehensive answer, note that a good practice regarding Python dependencies consists in specifying them in a declarative way in a dedicated text file (in alphabetical order, to ease review and update) so that for your example, you may want to write the following file:
requirements.txt
matplotlib
med2image
nibabel
pillow
pydicom
and use the following generic
Dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "./your-daemon-or-script.py"]
To be more precise, this is the approach suggested in the documentation of the Docker official image python, §. How to use this image
Some of the other answers/comments are suggesting to change your base image but if you want to keep your ubuntu 16.04 you can also simply specify your version of pip/python to use pip3 or pip3.5 like shown below.
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.5 \
python3-pip \
&& \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN pip3 install nibabel pydicom matplotlib pillow
RUN pip3 install med2image
Having a reliable Dockerfile as your base can save you hours of headaches and bigger problems down the road.
This article shares a Dockerfile base that has been battle-tested through many different projects.
https://luis-sena.medium.com/creating-the-perfect-python-dockerfile-51bdec41f1c8
This can also serve as a succinct tutorial of the different features/commands used to improve the final image.
Nothing is perfect I know! Please feel free to provide any feedback and we can iterate on the shared Dockerfile if needed.
Those are nice and surprising insights, especially on the performance side.
I wonder however what's the point of using virtual envs in a docker machine ?
Virtual envs are necessary on your local machine to separe different projects with different requirements, but in the docker context your code should be fairly isolated.
This:
WORKDIR /home/myuser
USER myuser
RUN mkdir code
ADD . code
WORKDIR code
should probably better be:
USER myuser
RUN mkdir /home/myuser/code
WORKDIR /home/myuser/code
COPY . .
Cleaner in my opinion. Most importantly, COPY is preferred over ADD.
» pip install dockerfile