alert-beard-13327
10/14/2024, 7:57 PMadventurous-pillow-38958
10/15/2024, 4:37 PMalert-beard-13327
10/15/2024, 4:39 PMadventurous-pillow-38958
10/15/2024, 4:43 PMadventurous-pillow-38958
10/15/2024, 4:50 PMBUILD
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:
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:
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
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
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
FROM python:3.10
EXPOSE 8000
ENTRYPOINT ["bin/app"]
COPY apps.hello/bin.pex /bin/app
adventurous-pillow-38958
10/15/2024, 4:51 PMadventurous-pillow-38958
10/15/2024, 4:53 PMalert-beard-13327
10/15/2024, 4:55 PMadventurous-pillow-38958
10/15/2024, 4:56 PMadventurous-pillow-38958
10/15/2024, 4:57 PM