create a docker container that auto-exits after 1 hour

Viewed 498

It is possible to create a docker container that exists and deletes itself after a specific amount of time?

for example, if I have an app that I run using:

docker run -d \
    --name=my_name\
    -p 3800:3800 \
    -v /docker/appdata/folder:/folder:rw \
    -v $HOME:/storage:rw \
    image/here

I normally do docker ps, find container id, stop it manually then rm it, is it possible to replace the manual part, by setting a 1 hour expiry for each container to self-destruct 1 hour after the run command?

Thanks in advance

1 Answers

You can add next parameters:

--stop-timeout # (API 1.25+) Timeout (in seconds) to stop a container
--rm # to Automatically remove the container when it exits

So, your command will look like:

docker run -d \
    --stop-timeout 3600 \
    --rm \
    --name=my_name\
    -p 3800:3800 \
    -v /docker/appdata/folder:/folder:rw \
    -v $HOME:/storage:rw \
    image/here
Related