https://pantsbuild.org/ logo
#general
Title
# general
a

ancient-rose-27306

08/22/2022, 3:43 PM
Hi guys, I have a question around building a Docker image from a python source. So far I have understood that if I want to publish a python library from a python source, I should be using
python_distribution()
target and for a Docker image it should be
docker_image()
target. Throwing in gRPC and Protobufs in the mixture and there I know that
python_distribution()
target type will resolve the proto dependency at the time of executing
package
goal. Would the target type
docker_image()
do the same? Is
package
the right goal for my use case here as in to build a Docker image?
r

refined-addition-53644

08/22/2022, 4:20 PM
This might be helpful. https://blog.pantsbuild.org/pants-pex-and-docker/ Generally
pex_binary
is the recommended target for docker image.
a

ancient-rose-27306

08/22/2022, 4:43 PM
This might be helpful.
https://blog.pantsbuild.org/pants-pex-and-docker/
Generally
pex_binary
is the recommended target for docker image.
Ok, what would be the workflow then? Adding the dependency of
pex_binary()
target to the
docker_image()
target?
r

refined-addition-53644

08/22/2022, 4:56 PM
You don't need to explicitly add dependency on
pex_binary
. It's from the docs.
When you COPY PEX binaries into your image, the dependency on the pex_binary target will be inferred, so you don't have to add that explicitly to the list of dependencies on your docker_image target.
https://www.pantsbuild.org/docs/docker
One important thing to know about
pex_binary
is that they are platform dependent and when you copy them inside docker, they are first built on the host not inside the docker container. So if your host is macos and you copy a PEX inside Linux docker Image, it won't work.
a

ancient-rose-27306

08/22/2022, 5:23 PM
Yeah, that’s why I was looking for building a Docker image from
python_distribution()
.
r

refined-addition-53644

08/22/2022, 5:51 PM
It has its cons too like if you have local python distribution (1st party) on which your
python_distribution
depends on then you will have to host them somewhere and then pass the necessary secrets to docker build so that it gets installed inside the image.
pex_binary
bundles all 1st party and 3rd party dependencies without much from your side.
a

ancient-rose-27306

08/22/2022, 5:53 PM
Ok, but right now I am just focused on getting the
package
goal working and see the results. I can think about the target types later once I have a clear picture in mind and I proceed towards implementing the learnings in prod.
2 Views