Serve flows in a long-lived Docker container
Learn how to serve a flow in a long-lived Docker container
The .serve
method allows you to easily elevate a flow to a deployment, listening for scheduled work to execute as a local process.
However, this “local” process does not need to be on your local machine. In this example we show how to run a flow in Docker container on your local machine, but you could use a Docker container on any machine that has Docker installed.
Overview
In this example, you will set up:
- a simple flow that retrieves the number of stars for some GitHub repositories
- a
Dockerfile
that packages up your flow code and dependencies into a container image
Writing the flow
Say we have a flow that retrieves the number of stars for a GitHub repository:
We can serve this flow on our local machine using:
… but how can we package this up so we can run it on other machines?
Writing the Dockerfile
Assuming we have our Python requirements defined in a file:
and this directory structure:
We can package up our flow into a Docker container using a Dockerfile
.
Using pip
, the image is built in about 20 seconds, and using uv
, the image is built in about 3 seconds.
You can learn more about using uv
in the Astral documentation.
Build and run the container
Now that we have a flow and a Dockerfile, we can build the image from the Dockerfile and run a container from this image.
Build (and push) the image
We can build the image with the docker build
command and the -t
flag to specify a name for the image.
At this point, you may also want to push the image to a container registry such as Docker Hub or GitHub Container Registry. Please refer to each registry’s respective documentation for details on authentication and registry naming conventions.
Run the container
You’ll likely want to inject some environment variables into your container, so let’s define a .env
file:
Then, run the container in detached mode (in other words, in the background):
Verify the container is running
You should see your container in the list of running containers, note the CONTAINER ID
as we’ll need it to view logs.
View logs
You should see logs from your newly served process, with the link to your deployment in the UI.
Stop the container
Next steps
Congratulations! You have packaged and served a flow on a long-lived Docker container.
You may now easily deploy this container to other infrastructures, such as:
- Modal
- Google Cloud Run
- AWS Fargate / ECS
- Managed Kubernetes (e.g. GKE, EKS, AKS)
… or anywhere else you can run a Docker container!
Was this page helpful?