proud-policeman-38871
09/13/2024, 9:30 AMpex
yet, especially for services where we have multiple scripts in the same project and just want a docker image similar to what we already have for each project.
Are there any docs about migrating our existing dockerfiles to work with pants, specifically around copying paths and setting up dependencies?proud-policeman-38871
09/13/2024, 9:30 AMFROM python:3.12-slim
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app:/app/libraries
# Install Postgres dependencies
RUN apt-get update && apt-get install -y --no-install-recommends libpq5 && rm -rf /var/lib/apt/lists/*
# Install pip requirements
WORKDIR /app
COPY requirements.txt .
RUN python -m pip install --no-cache-dir -r requirements.txt && rm requirements.txt
# Install and compile app components
COPY bin/* /app/bin/
COPY libraries/ /app/libraries/
COPY templates/ /app/templates/
COPY usage_instructions.py /app/
RUN adduser -u 10000 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
CMD ["python3", "usage_instructions.py"]
elegant-florist-94385
09/13/2024, 9:47 AMpex
. It doesn't look like you have a lot going on in your dockerfile that would give you too much trouble.
1. Put everything that's not "copy files" or "pip install" into a Dockerfile as a base image.
2. Write lots of `pex_binary`/`docker_image` pairs like:
pex_binary(name="module_pex"), entry_point="usage_instructions.py")
docker_image(name="module_image", instructions=[
"ARG BASE_IMAGE=/path/to/base:image",
"FROM $BASE_IMAGE",
"COPY src.module.module_pex /app/app.pex", # I probably got the generated name of the pex wrong here, but you get the idea
]
3. Wrap this up in a macro so your build files can just look like
python_docker_image(name=<abc>, entry_point=/path/to/usage_instructions.py"
4. Follow https://www.pantsbuild.org/blog/2022/08/02/optimizing-python-docker-deploys-using-pants#multi-stage-build-leveraging-2-pexs if you want cache-optimized imagesproud-policeman-38871
09/13/2024, 9:53 AMpex_binary
for each of those?elegant-florist-94385
09/13/2024, 9:55 AMproud-policeman-38871
09/13/2024, 9:56 AMcommand
override to specify the script to run for that deploymentproud-policeman-38871
09/13/2024, 9:57 AMcommand:
- /usr/local/bin/python3
- bin/this_thing.py
proud-policeman-38871
09/13/2024, 9:57 AMelegant-florist-94385
09/13/2024, 9:58 AM/bin
just a collection of various python script entrypoints? or do you have non-python starters in there too?proud-policeman-38871
09/13/2024, 9:58 AMelegant-florist-94385
09/13/2024, 10:00 AMproud-policeman-38871
09/13/2024, 10:00 AMproud-policeman-38871
09/13/2024, 10:01 AMelegant-florist-94385
09/13/2024, 10:04 AMpex_binary
for each entrypoint, and then copying each of them into the docker image would work (functionality-wise), though you'll probably end up with a number of common files copied into each pex, and a bit of image size bloat.proud-policeman-38871
09/13/2024, 10:04 AMproud-policeman-38871
09/13/2024, 10:07 AMelegant-florist-94385
09/13/2024, 10:09 AMpython_sources
targets
I know my team's situation was basically "figuring out and mapping the subset of the repo needed by each entrypoint is too difficult" and that's why we had the "super-image" pattern. But pants' dependency inference makes that all go away.elegant-florist-94385
09/13/2024, 10:10 AMbreezy-twilight-65275
09/13/2024, 12:26 PMelegant-florist-94385
09/13/2024, 1:56 PMproud-policeman-38871
09/13/2024, 1:57 PMelegant-florist-94385
09/13/2024, 2:03 PMfrom package.of.all.entry_points import A, B, C
entry = os.env.get(ENTRYPOINT, None)
match entry:
case "A": A.main()
case "B": B.main()
case "C": C.main()
default: raise ValueError(f"Unrecognized entrypoint: '{entry}')
elegant-florist-94385
09/13/2024, 2:03 PMproud-policeman-38871
09/13/2024, 2:03 PMproud-policeman-38871
09/13/2024, 2:03 PMelegant-florist-94385
09/13/2024, 2:04 PM