pdm icon indicating copy to clipboard operation
pdm copied to clipboard

The `Use PDM in a multi-stage Dockerfile` is overdue.

Open Svtter opened this issue 3 years ago • 3 comments

In the documentation, Dockerfile depends on the PEP582 and __pypackages__, which is enabled by default in version 1.*.

# build stage
FROM python:3.8 AS builder

# install PDM
RUN pip install -U pip setuptools wheel
RUN pip install pdm

# copy files
COPY pyproject.toml pdm.lock README.md /project/
COPY src/ /project/src

# install dependencies and project
WORKDIR /project
RUN pdm install --prod --no-lock --no-editable


# run stage
FROM python:3.8

# retrieve packages from build stage
ENV PYTHONPATH=/project/pkgs
COPY --from=builder /project/__pypackages__/3.8/lib /project/pkgs

# set command/entrypoint, adapt to fit your needs
CMD ["python", "-m", "project"]


However, the PEP582 is disabled by default in the current default version 2.*, and the Dockerfile is not worked nowadays.

Svtter avatar Sep 08 '22 07:09 Svtter

A minor change should do the trick

-RUN pdm install --prod --no-lock --no-editable
+RUN mkdir __pypackages__ && pdm install --prod --no-lock --no-editable

Can you help check if it works and update the docs if it does?

frostming avatar Sep 08 '22 07:09 frostming

I am glad to check it.

It worked!

 => CACHED [builder 3/7] COPY qms/pip.conf ~/.config/pip/pip.conf                                                              0.0s
 => CACHED [builder 4/7] RUN pip install -U pip pdm -i https://pypi.tuna.tsinghua.edu.cn/simple                                0.0s
 => CACHED [builder 5/7] COPY pyproject.toml pdm.lock README.md /project/                                                      0.0s
 => CACHED [builder 6/7] WORKDIR /project                                                                                      0.0s
 => CACHED [builder 7/7] RUN mkdir __pypackages__ && pdm install --prod --no-lock --no-editable                                0.0s
 => CACHED [stage-1 2/4] COPY --from=builder /project/__pypackages__/3.9/lib /project/pkgs                                     0.0s
 => CACHED [stage-1 3/4] COPY . /project/src                                                                                   0.0s
 => CACHED [stage-1 4/4] WORKDIR /project/src                                                                                  0.0s
 => exporting to image                                                                                                         0.0s
 => => exporting layers                                                                                                        0.0s
 => => writing image sha256:825470bd73f3ddb449119046742d7ad89d30de9c41a74b8c429de4a1536f10ce                                   0.0s
 => => naming to docker.io/library/default    

Svtter avatar Sep 08 '22 09:09 Svtter

You can also write this line in your container before the install. I prefer it because for me it's more clear to know what and why are you doing it than just creating the directory.

RUN pdm config python.use_venv False

SirMartin avatar Sep 08 '22 14:09 SirMartin

@frostming will there be a guide for installing pdm in docker with the recommended installer? or is it better to install through pip when using docker?

thoughtfuldata avatar Oct 26 '22 23:10 thoughtfuldata

It is better to use the recommend installer inside the docker too.

SirMartin avatar Oct 27 '22 06:10 SirMartin

@SirMartin do you have an example I could look at? I have been trying to get it to work with the recommended installer and it doesn't work.

thoughtfuldata avatar Oct 27 '22 19:10 thoughtfuldata

This is how we install it now in our company. You can remove the version, but I fixed versions in 2.1.4 for now.

RUN curl -sSL https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py | python3 - --version=2.1.4
ENV PATH=/root/.local/bin:${PATH}

And to make the pdm install, we install the libraries globally inside the container, and with the check option.

RUN pdm install -p /project --check -g

SirMartin avatar Oct 28 '22 09:10 SirMartin