Sorry, sent early. Basically trying to figure out ...
# general
a
Sorry, sent early. Basically trying to figure out if pants can make it easy to build the docker image of app1, while importing a library (lib1), without excessive configuration and reorganization. We try and implement the living at HEAD philosophy, but if that doesn't sound practical in this case I'm willing to modify that thought process.
a
If I understand correctly, are you 1. Looking for a straightforward way to build the Docker image for app1 using Pants without requiring excessive configuration or restructuring of your project. Specifically, you want to ensure that, out of all the libraries, if app1 depends only on lib1, the app1 image should include only lib1. 2. While you want to make the Docker image building process straightforward (as mentioned point), you also want to retain the ability to directly import lib1 while developing app1, without the need to package lib1 ? If yes*, I can point you to the steps I took while adopting Pants. I am currently preparing documentation for my team on the same topic, and I would be more than happy to share it with you if it helps. (Note: Our team mainly uses Python, so what I have is a quick start guide for Python.)
a
Yes! If you have documentation on that, that would be incredible. That's pretty much exactly our use case 🙂
a
Sure, doc is still in draft, I will share the relevant points here in 2 min
❤️ 1
Why use pants,pex and docker ? ◦ This article that explains this in detail ◦ But TL;DR: Pants simplifies the process of building PEX files and automatically includes your transitive dependencies (i.e., any modules your code imports or uses). By combining Pants and PEX—specifically by packaging PEX files inside Docker—you can leverage Pants to rebuild all transitive dependencies whenever a module changes. This ensures that your PEX files are updated accordingly and seamlessly integrated into your Docker containers. 13. Package Your App Using PEX and Docker: Update the
BUILD
file with the necessary targets: Once you've tested your app and everything is in order, you can package it using PEX . To do this, add the following target types to your app's
BUILD
file:
Copy code
python_sources(name="lib")
pex_binary(
    name="bin",
    dependencies=[":lib"],
    execution_mode="venv",
    entry_point='app.py'  # Ensure this points to your main application file
)
Entry Point: Specify a proper entry point so that when the PEX file runs, it executes your application. For instance, if your
app.py
is a FastAPI application that starts a Uvicorn server, ensure it's set as the entry point. To build pex, run the following command:
pants package path/to/mybinary:bin
example: pants package apps/hello
14. Building the Docker Image: To build Docker images for your applications, ensure that each app's BUILD file contains a
docker_image
target along with the necessary Dockerfile. Below is an example of the required targets for an app that needs to be containerized:
Copy code
docker_image(
    name="world",
    dependencies=[":bin"],
    image_tags=["{pants.hash}"]
)
• Your Dockerfile should copy the PEX file; pants will take care of building this pex files when running the goal
Copy code
FROM python:3.10
EXPOSE 8081
ENTRYPOINT ["bin/app"]
COPY apps.world/bin.pex /bin/app
at the end, this is how app's build file looks like
Copy code
python_sources(name="app")


pex_binary(
    name="bin",
    dependencies=[":app"], 
    execution_mode="venv",
    entry_point='app.py'
)

docker_image(
    name="hello",
    dependencies=[":bin"],
    image_tags= ["{pants.hash}"]  #It is effectively a hash of the Docker build context
)
and docker
Copy code
FROM python:3.10
EXPOSE 8000
ENTRYPOINT ["bin/app"]
COPY apps.hello/bin.pex /bin/app
This was just a POC. so it may not be following the best practices. Please confirm with an expert before moving to production. Additionally, if the points I mentioned are not helpful, please refer to the article I attached from Benjy, as it should provide valuable insights.
Sorry for the lengthy explanation 😬, but in a nutshell, you can combine PEX and Dockerfile, and that should resolve your problem.
a
No thats great! If I'm understanding basically its just creating the binary with Pants, and then building the docker image with the binary instead of trying to import/COPY dependencies in the docker build stage, and without having to set up a separate packaging workflow and import strategy. Is that roughly correct?
a
Yes Chole, correct
This is what I understood by going through the docs.