single command to stop and remove docker container

前端 未结 11 1146
失恋的感觉
失恋的感觉 2021-01-30 15:17

Is there any command which can combine the docker stop and docker rm command together ? Each time I want to delete a running container, I need to execu

11条回答
  •  野的像风
    2021-01-30 16:02

    Use the docker ps command with the -a flag to locate the name or ID of the containers you want to remove

    docker ps -a
    

    To remove: $ docker rm ID_or_Name ID_or_Name

    Remove a container upon exit:

    If you know when you’re creating a container that you won’t want to keep it around once you’re done, you can run docker run --rm to automatically delete it when it exits.

    Run and Remove : docker run --rm image_name

    Remove all exited containers:

    You can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exited. To review the list of exited containers, use the -f flag to filter based on status. When you've verified you want to remove those containers, using -q to pass the IDs to the docker rm command.

    List:

    docker ps -a -f status=exited
    
    docker rm $(docker ps -a -f status=exited -q)
    

    Remove containers using more than one filter:

    Docker filters can be combined by repeating the filter flag with an additional value. This results in a list of containers that meet either condition. For example, if you want to delete all containers marked as either Created (a state which can result when you run a container with an invalid command) or Exited, you can use two filters:

    docker ps -a -f status=exited -f status=created
    

    Stop and Remove all the containers:

    docker stop $(docker ps -a -q)
    docker rm $(docker ps -a -q)
    

提交回复
热议问题