<@ULVA73B9P> Is there a setting I can turn on that...
# ask-marvin
t
@Marvin Is there a setting I can turn on that makes prefect clean up old docker containers when the flow is done running? I have a docker worker on a linux box that will spin up a container, run the flow, and then it won't fully clean up the container. This means I have to manually prune all the containers eventually myself
m
thought for 185.5 seconds
Yes — with the Docker worker you can enable automatic container cleanup using Docker’s auto-remove behavior. In Prefect 3.x, set the
auto_remove
job variable to
true
(this is equivalent to
docker run --rm
). By default it’s
false
, so containers stick around after the flow exits. Ways to enable it: - Per deployment (job variables) - Python deploy:
Copy code
from prefect import flow

    @flow
    def my_flow():
        ...

    if __name__ == "__main__":
        my_flow.deploy(
            name="auto-cleanup",
            work_pool_name="my-docker-pool",
            job_variables={"auto_remove": True},
        )
- prefect.yaml:
Copy code
deployments:
      - name: auto-cleanup
        work_pool:
          name: my-docker-pool
        job_variables:
          auto_remove: true
- Default for a Docker work pool (base job template) - In the UI: Work Pools -> your Docker pool -> Edit base job template -> set the
variables.auto_remove.default
to
true
and ensure
job_configuration.auto_remove
is wired to
{{ auto_remove }}
. - Or via CLI, inspect the current template:
Copy code
prefect work-pool inspect my-docker-pool -o json
Then update the pool with a modified base job template. Notes and caveats: - This only removes the container; images, networks, and volumes are not pruned by Prefect. - With
auto_remove: true
, Docker deletes the container as soon as it exits. If you rely on inspecting containers after failure, leave it
false
. - If you still see leftover containers after enabling
auto_remove
, check whether a deployment-level
job_variables
is overriding the pool default. Want me to help you update your pool template or deployment config? Also, are you on Prefect 3.x and are the leftovers strictly containers (not volumes/images)?