docker:remove_a_container
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
docker:remove_a_container [2020/04/15 22:12] – [Remove containers according to a pattern] peter | docker:remove_a_container [2020/05/13 08:52] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Docker - Remove a container ====== | ||
- | |||
- | ===== Remove one or more specific containers ===== | ||
- | |||
- | Use the **docker ps** command with the **-a** flag to locate the name or ID of the containers you want to remove: | ||
- | |||
- | To list the containers: | ||
- | |||
- | <code bash> | ||
- | docker ps -a | ||
- | </ | ||
- | |||
- | |||
- | To remove the containers: | ||
- | |||
- | <code bash> | ||
- | 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: | ||
- | |||
- | <code bash> | ||
- | 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. | ||
- | |||
- | To list the containers: | ||
- | |||
- | <code bash> | ||
- | docker ps -a -f status=exited | ||
- | </ | ||
- | |||
- | |||
- | To remove the containers: | ||
- | |||
- | <code bash> | ||
- | 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: | ||
- | |||
- | To list the containers: | ||
- | |||
- | <code bash> | ||
- | docker ps -a -f status=exited -f status=created | ||
- | </ | ||
- | |||
- | |||
- | To remove the containers: | ||
- | |||
- | <code bash> | ||
- | docker rm $(docker ps -a -f status=exited -f status=created -q) | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Remove containers according to a pattern ===== | ||
- | |||
- | You can find all the containers that match a pattern using a combination of **docker ps** and grep. | ||
- | |||
- | When you're satisfied that you have the list you want to delete, you can use awk and xargs to supply the ID to **docker rmi**. | ||
- | |||
- | <WRAP info> | ||
- | **NOTE: | ||
- | </ | ||
- | |||
- | |||
- | To list the containers: | ||
- | |||
- | <code bash> | ||
- | docker ps -a | grep " | ||
- | </ | ||
- | |||
- | |||
- | To remove the containers: | ||
- | |||
- | <code bash> | ||
- | docker ps -a | grep " | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Stop and remove all containers ===== | ||
- | |||
- | You can review the containers on your system with **docker ps**. | ||
- | |||
- | Adding the **-a** flag will show all containers. | ||
- | |||
- | When you're sure you want to delete them, you can add the **-q** flag to supply the IDs to the **docker stop** and **docker rm** commands: | ||
- | |||
- | To list the containers: | ||
- | |||
- | <code bash> | ||
- | docker ps -a | ||
- | </ | ||
- | |||
- | |||
- | To remove the containers: | ||
- | |||
- | <code bash> | ||
- | docker stop $(docker ps -a -q) | ||
- | docker rm $(docker ps -a -q) | ||
- | </ | ||
- | |||
docker/remove_a_container.1586988779.txt.gz · Last modified: 2020/07/15 09:30 (external edit)