Daemonizing the Agent with Docker

Docker is a versatile choice for persistently running the Prefect agent. Docker ensures that the agent stays up and is restarted when something goes wrong.

Here’s how to start it assuming you’re logged into Prefect Cloud (prefect cloud login):

# Load in PREFECT_API_KEY and PREFECT_API_URL
prefect profile inspect default > prefect_config.env

# Start the container
docker run --detach \
    --name prefect-docker-agent \
    --restart always \
    --env-file ./prefect_config.env \
    prefecthq/prefect:2-latest \
    prefect agent start --work-queue default

If you need to stop / remove the agent you can do so with:

# Kill the container and delete it
docker kill prefect-docker-agent
docker rm prefect-docker-agent
1 Like