Is there a way to refer to a python dependency ver...
# general
i
Is there a way to refer to a python dependency version in the from field of a dockerfile? For example, i have dependency declared in a BUILD file as
requirements=["x==1.0.1"],
, and i would like to build a docker image with
FROM x-1.0.1-py3.8
. I’ve figured out how to do it through docker build args, but that involves a
pants peek
and some command line wrangling. Any recommendations for a simpler way to do it?
c
Hi! Interesting. Could you share a bit more (still redacted is fine) the BUILD file targets involved, and the shell wrangling performed to help me understand what you’re doing? 🙂 My guess there isn’t any simpler way to do it, atm. But perhaps there is a simple thing to add to make it possible to have a simpler way to get what you want.
s
@icy-hair-30586 can you create a Repo for tinkering maybe? I’m pretty proficient when it comes to docker and I’m up for helping 🙌
❤️ 4
i
before I get to a repo:
3rdparty/python/build
contains
Copy code
python_requirement(
    name="prefect",
    requirements=["prefect==1.1.0"],
)
Dockerfile now contains
Copy code
ARG PREFECT_VERSION=1.1.0
FROM prefecthq/prefect:${PREFECT_VERSION}-python3.8
and the CI pipeline does something like
Copy code
PREFECT_VERSION=$(./pants peek 3rdparty/python:prefect | jq '.[0].requirements[0]' | tr -d '"' | sed -e 's/prefect==//')
...
./pants package src/docker:hello --docker-build-args="['PREFECT_VERSION=$(PREFECT_VERSION)']"
This works fine in CI, but locally it could happen that the dev forgets to update the default PREFECT_VERSION in the dockerfile ending up with weirdness when running the locally built image
the docker_image target contains nothing special, the dependencies, source, tags…
I could just leave out the default for PREFECT_VERSION in the dockerfile, then the local builds will require the build args, making the “dangerous bit” a little more obvious
s
I would probably solve that using an Environment-Variable that I’d print out before every build. You are probably using a shell-file to call the Docker-build, right? -> If something implicit makes things break in development, I’d try to make it more explicit. Removing the default would also work.
👍 1
i
using shell fragments in azure devops, which is really the worst way to do anything… hopefully we change that soon too
but just the amount of mess related to running tests/linters/docker/… that pants allows us to remove is amazing.
❤️ 2
c
What I’ve done to ease local dev situation for this, is to have a
.ci-vars.sh
script that sets up the environemnt, then source that from the
./pants
bootstrap script. That way, you can have the pants peek operation run also for the devs without them having to worry about it.
🙌 2
Another minor reflection is that you can leave off the value for the build arg, it will check the env for it by default then.
(from your command line incantation)