Should I be concerned about excess, non-running, Docker containers?

前端 未结 5 1593
花落未央
花落未央 2021-01-29 23:35

Every docker run command, or every RUN command inside a Dockerfile, creates a container. If the container is no longer running it can still be seen wit

5条回答
  •  后悔当初
    2021-01-30 00:19

    The docker run documentation describes how to automatically clean up the container and remove the file system when the container exits:

      --rm=false: Automatically remove the container when it exits (incompatible with -d)
    

    The above shows that by default containers are not removed, but adding --rm=true or just the short-hand --rm will work like so:

    sudo docker run -i -t --rm ubuntu /bin/bash
    

    When you exit from the container it will be automatically removed.

    You can test this by listing your docker containers in one terminal window:

    watch -n1 'sudo ls -c /var/lib/docker/containers'
    

    And then in another window run this command to run multiple docker containers that will all automatically exit after sleeping for up to 10 seconds.

    for i in {1..10}; do sudo docker run --rm ubuntu /bin/sleep $i & done
    

提交回复
热议问题