Docker Container Cleanup Tutorial

Preparation

docker pull hello-world:latest
docker pull nginx:mainline-alpine
docker pull redis:alpine
docker pull alpine:3.8
docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
docker run --name nginx -d  nginx:mainline-alpine
docker run --name redis -d redis:alpine
docker run -d hello-world
docker run -d hello-world
docker run -d hello-world
docker stop nginxdocker stop redis
docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED              STATUS                          PORTS               NAMES97f95b11260e        hello-world             "/hello"                 57 seconds ago       Exited (0) 44 seconds ago                           determined_payne
91f69bd08bf8 hello-world "/hello" About a minute ago Exited (0) About a minute ago xenodochial_wiles
6f7b53de6ad7 hello-world "/hello" About a minute ago Exited (0) About a minute ago pedantic_bartik
62919b9e6d84 redis:alpine "docker-entrypoint.s? About a minute ago Exited (0) 3 seconds ago redis
49ca281f8a0c nginx:mainline-alpine "nginx -g 'daemon of? 2 minutes ago Exited (0) 14 seconds ago nginx
The CONTAINER IDs you get when you run these commands on your computer will be different.
docker container prune
docker ps -a | grep "hello-world"
grep is used to search plain-text input for lines that match its search expression.The __ docker ps -a __ lists all containers, the pipe symbol | sends this list to grep.   __  grep "hello-world" __ searches for the text __ hello-word __. grep only outputs the list of containers __ containing hello-word. __Grep stands for: g/re/p (__g__lobally search a __re__gular expression and __p__rint)In __ docker ps -a | grep "hello-world" __ grep searched the list of containers output by docker ps, and prints only the lines containing hello-world.grep searches the input files for lines containing a match to a given pattern list. When it finds a match in a line, it copies the line to standard output (by default), or whatever other sort of output you have requested with options.
CONTAINER ID        IMAGE                   COMMAND                  CREATED              STATUS                          PORTS               NAMES97f95b11260e        hello-world             "/hello"                 About a minute ago   Exited (0) About a minute ago                       determined_payne
91f69bd08bf8 hello-world "/hello" 2 minutes ago Exited (0) About a minute ago xenodochial_wiles
6f7b53de6ad7 hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago pedantic_bartik
docker ps -a | egrep --invert-match "redis|nginx"
docker container __ rm __ = docker container __ remove __ / delete / prune
docker container rm [OPTIONS] CONTAINER [CONTAINER...]
docker ps -a | grep "hello-world" | awk '{print $1}'
97f95b11260e
91f69bd08bf8
6f7b53de6ad7
docker ps -a | grep "hello-world" | awk '{print $1}' | __ xargs docker container rm __
docker ps -a | egrep -v "redis|nginx"  | awk '{print $1}' | __ xargs docker container rm __
97f95b11260e
91f69bd08bf8
6f7b53de6ad7
docker ps -aCONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                     PORTS               NAMES62919b9e6d84        redis:alpine            "docker-entrypoint.s?   4 minutes ago       Exited (0) 2 minutes ago                       redis
49ca281f8a0c nginx:mainline-alpine "nginx -g 'daemon of? 4 minutes ago Exited (0) 2 minutes ago nginx
docker logs redis1:C 03 Oct 06:40:25.554 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 03 Oct 06:40:25.559 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 03 Oct 06:40:25.559 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 03 Oct 06:40:25.613 * Running mode=standalone, port=6379.
1:M 03 Oct 06:40:25.614 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 03 Oct 06:40:25.614 # Server initialized
1:M 03 Oct 06:40:25.614 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 03 Oct 06:40:25.615 * Ready to accept connections
1:signal-handler (1538548914) Received SIGTERM scheduling shutdown...
1:M 03 Oct 06:41:54.159 # User requested shutdown...
1:M 03 Oct 06:41:54.195 * Saving the final RDB snapshot before exiting.
1:M 03 Oct 06:41:54.233 * DB saved on disk
1:M 03 Oct 06:41:54.233 # Redis is now ready to exit, bye bye...
docker logs nginx
docker container prune -fDeleted Containers:
62919b9e6d844d6720301dd8be7d0dfb58defb593fe8e9fa1ea0984b7aa92baa
49ca281f8a0cdc3adaddd222a654c4e61b17861f3a20dd9b584d0073466c722d
Total reclaimed space: 0B
docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                     PORTS               NAMES
docker ps -a | grep "hello-world" | awk '{print $1}' | xargs docker container rm
docker ps -a | egrep -v "redis|nginx"  | awk '{print $1}' | xargs docker container rm

Remove Containers Using Docker Container Prune

docker run -d hello-world
docker run hello-world ; docker run hello-world
docker container ls
docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES60cc61444c0b        hello-world         "/hello"            45 seconds ago       Exited (0) 37 seconds ago                           pensive_albattani
e820b8032706 hello-world "/hello" About a minute ago Exited (0) 51 seconds ago silly_lovelace
7e959fd100d0 hello-world "/hello" About a minute ago Exited (0) About a minute ago fervent_kilby
docker container prune -f
Deleted Containers:
60cc61444c0b90c7f2b0ada1546c3913a93676bbb0ba0dfb1c9329268222df6d
e820b803270681be048b511d676af56e28778e92ef783a536a4272f7d9648bd6
7e959fd100d0a72311f25987a0d0b6d1960283f2b164e56c538baddad83e8446

Prune containers using labels

docker run -d --label hello-1 hello-world
docker run -d --label hello-2 hello-world
docker run -d --label hello-3 hello-world
docker ps -a --format 'table {{.Image}}\t{{.Labels}}\t{{.Command}}\t{{.Status}}'
IMAGE               LABELS              COMMAND             STATUS
hello-world hello-3= "/hello" Exited (0) 1 minutes ago
hello-world hello-2= "/hello" Exited (0) 1 minutes ago
hello-world hello-1= "/hello" Exited (0) 1 minutes ago
docker container prune --force --filter "label=hello-1"
Deleted Containers:
1aa4c41c7a5b164edb4bb08cec4ec8c9769fb3e7dc311c825321fc7f87989ea9
Total reclaimed space: 0B
docker ps -a --format 'table {{.Image}}\t{{.Labels}}\t{{.Command}}\t{{.Status}}'IMAGE               LABELS              COMMAND             STATUS
hello-world hello-3= "/hello" Exited (0) 4 minutes ago
hello-world hello-2= "/hello" Exited (0) 4 minutes ago
docker container prune -f
docker ps -a
IMAGE               LABELS              COMMAND             STATUS

Docker ps Filtering Reference

docker container prune --force --filter __ "ancestor=hello-word:latest" __
docker container prune --force --filter __ "network=mytestnetwork" __

Prune Containers Using Exit Status

docker run -d alpine:3.8 /bin/sh -c 'exit 123'
docker run -d alpine:3.8 /bin/sh -c 'exit 9'
docker run -d alpine:3.8
docker run --name nginx -d nginx:mainline-alpine
docker run --name redis -d redis:alpine
docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED              STATUS                            PORTS               NAMES9d4163879e17        redis:alpine            "docker-entrypoint.s?   13 seconds ago       Up 3 seconds                      6379/tcp            redis
2754c6805252 nginx:mainline-alpine "nginx -g 'daemon of? 26 seconds ago Up 15 seconds 80/tcp nginx
ef02229f40da alpine:3.8 "/bin/sh" 42 seconds ago Exited (0) 30 seconds ago epic_nobel
857cf388b70c alpine:3.8 "/bin/sh -c 'exit 9'" 59 seconds ago Exited (9) 46 seconds ago peaceful_torvalds
8b084d95a2fb alpine:3.8 "/bin/sh -c 'exit 12? About a minute ago Exited (123) About a minute ago pedantic_mirzakhani
docker ps -a --filter status=exited
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                            PORTS               NAMESef02229f40da        alpine:3.8          "/bin/sh"                About a minute ago   Exited (0) About a minute ago                         epic_nobel
857cf388b70c alpine:3.8 "/bin/sh -c 'exit 9'" About a minute ago Exited (9) About a minute ago peaceful_torvalds
8b084d95a2fb alpine:3.8 "/bin/sh -c 'exit 12? 2 minutes ago Exited (123) About a minute ago pedantic_mirzakhani
docker ps -a --filter 'exited=0'
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMESef02229f40da        alpine:3.8          "/bin/sh"           About a minute ago   Exited (0) About a minute ago                       epic_nobel
docker ps -a --filter 'exited=123'
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES8b084d95a2fb        alpine:3.8          "/bin/sh -c 'exit 12?   2 minutes ago       Exited (123) 2 minutes ago                       pedantic_mirzakhani
docker ps -a --filter 'exited=0' | awk '{print $1}' | xargs docker container rm
ef02229f40da
Error: No such container: CONTAINER
docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                       PORTS               NAMES9d4163879e17        redis:alpine            "docker-entrypoint.s?   4 minutes ago       Up 3 minutes                 6379/tcp            redis
2754c6805252 nginx:mainline-alpine "nginx -g 'daemon of? 4 minutes ago Up 4 minutes 80/tcp nginx
857cf388b70c alpine:3.8 "/bin/sh -c 'exit 9'" 4 minutes ago Exited (9) 4 minutes ago peaceful_torvalds
8b084d95a2fb alpine:3.8 "/bin/sh -c 'exit 12? 5 minutes ago Exited (123) 4 minutes ago pedantic_mirzakhani
docker ps -a --filter 'exited=9' | awk '{print $1}'
CONTAINER
857cf388b70c
docker ps -a --filter 'exited=9' | awk '{print $1}' | grep -v CONTAINER
857cf388b70c
docker ps -a --filter 'exited=9' | awk '{print $1}' | grep -v CONTAINER
docker ps -a --filter 'exited=9' | grep -v CONTAINER | awk '{print $1}'
docker ps -a --filter 'exited=9' | awk '{print $1}' | grep -v CONTAINER | xargs docker container rm
857cf388b70c
docker ps -a --filter 'exited=123' | awk '{print $1}' | grep -v CONTAINER | xargs docker container rm
8b084d95a2fb
docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS               NAMES9d4163879e17        redis:alpine            "docker-entrypoint.s?   10 minutes ago      Up 10 minutes       6379/tcp            redis
2754c6805252 nginx:mainline-alpine "nginx -g 'daemon of? 10 minutes ago Up 10 minutes 80/tcp nginx

Remove Containers in Status: Created

docker run -d alpine:3.8  <span class=error>  zxgarbagez  </span>
cc00b62dcb5078fed6d9e3fc48a9e5e7a9f3ef11d6722073781c3f5054696889
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \" <span class=error> zxgarbagez </span>\": executable file not found in $PATH": unknown.
docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS               NAMEScc00b62dcb50        alpine:3.8              "zxgarbagez"             28 seconds ago      Created                                 zealous_colden
9d4163879e17 redis:alpine "docker-entrypoint.s? 17 minutes ago Up 17 minutes 6379/tcp redis
2754c6805252 nginx:mainline-alpine "nginx -g 'daemon of? 17 minutes ago Up 17 minutes 80/tcp nginx
docker ps --filter status=created
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS               NAMEScc00b62dcb50        alpine:3.8              "zxgarbagez"             28 seconds ago      Created                                 zealous_colden
docker ps --filter status=created | awk '{print $1}' | grep -v CONTAINER | xargs docker container rm
cc00b62dcb50
docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS               NAMES9d4163879e17        redis:alpine            "docker-entrypoint.s?   17 minutes ago      Up 17 minutes       6379/tcp            redis
2754c6805252 nginx:mainline-alpine "nginx -g 'daemon of? 17 minutes ago Up 17 minutes 80/tcp nginx

Remove Containers in Status: Paused

docker run -d --name testme alpine:3.8 /bin/sh -c 'sleep 10m'
docker pause testme
testmedocker pause showed the name of the container it just paused: testme
docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES637489e9b88d        alpine:3.8          "/bin/sh -c 'sleep 1?   14 seconds ago      Up 6 seconds (Paused)                            testme
docker ps --filter status=paused
docker ps --filter status=paused | awk '{print $1}' | grep -v CONTAINER | xargs docker container rm
Error response from daemon: You cannot remove a paused container 637489e9b88d5704b9e677ff26199a9fec41644d4310732943cab0a2c8b0d4a6. Unpause and then stop the container before attempting removal or force remove
docker stop testme
testme
docker ps --filter status=paused | awk '{print $1}' | grep -v CONTAINER | xargs docker container stop
docker ps --filter status=stopped
Error response from daemon: Invalid filter 'status=stopped'
docker ps --filter status=exited
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES3e8fb9e2c41d        alpine:3.8          "/bin/sh -c 'sleep 1?   3 minutes ago       Exited (137) About a minute ago                       testme
docker ps --filter status=exited | awk '{print $1}' | grep -v CONTAINER | xargs docker container rm
3e8fb9e2c41d

Remove the Container When It Exits

docker run -d  --rm  hello-world
docker run -d --rm hello-world
docker run -d --rm hello-world
docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Tutorial Cleanup

docker image rm hello-world:latest
docker image rm nginx:mainline-alpine
docker image rm redis:alpine
docker image rm alpine:3.8

Overall Docker Cleanup

docker stop $(docker ps -a -q) #stop ALL containersdocker ps -a -q builds a list of all container IDs - running and exited.
docker rm -f $(docker ps -a -q) # remove ALL containers

Summary

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store