What is docker –rm option
On Docker, --rm
option means automatically remove the container when it exits.
$ docker run --rm <container_id>
Done 🙂
Please read on to understand more about --rm
.
1. Remove a container
On Docker, the container is an instance of an image, and we can create multiple containers from an image. If the container is exited or stopped, it still exists at the machine, and we need to delete the stopped container manually to free up the disk space.
This command will remove a docker container.
$ docker rm <container_id>
With --rm
, the container will automatically remove when it exit.
2. docker –rm
Review a docker --rm
example.
2.1 List all images, and then we use the Debian image to test the --rm
option.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 78096d0a5478 9 days ago 1.22MB
markdownhtml 1.1 18e69a3a2aae 12 days ago 144MB
debian 0.1 ae0872208331 7 weeks ago 114MB
2.2 List all containers, running, and stopped containers. In the below example, we have one stopped container.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d362659da5fc markdownhtml:1.1 "java -jar app.jar" 11 days ago Exited (255) 10 days ago 0.0.0.0:80->8080/tcp xenodochial_volhard
2.3 We start a container from the Debian image and run a ls -la
command. The container finished the command job and exit.
$ docker run debian ls -la
total 72
drwxr-xr-x 1 root root 4096 May 23 04:04 .
drwxr-xr-x 1 root root 4096 May 23 04:04 ..
-rwxr-xr-x 1 root root 0 May 23 04:04 .dockerenv
drwxr-xr-x 2 root root 4096 Mar 27 00:00 bin
drwxr-xr-x 2 root root 4096 Feb 1 17:09 boot
drwxr-xr-x 5 root root 340 May 23 04:04 dev
drwxr-xr-x 1 root root 4096 May 23 04:04 etc
drwxr-xr-x 2 root root 4096 Feb 1 17:09 home
drwxr-xr-x 7 root root 4096 Mar 27 00:00 lib
drwxr-xr-x 2 root root 4096 Mar 27 00:00 lib64
drwxr-xr-x 2 root root 4096 Mar 27 00:00 media
#...
2.4 List all the containers again, the stopped Debian container is listed here, and the stopped containers may take disk space. In the below example, the new stopped crazy_swirles
container takes 10MB disk space.
$ ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf381d99f454 debian "ls -la" About a minute ago Exited (0) About a minute ago crazy_swirles (NEW Container)
d362659da5fc markdownhtml:1.1 "java -jar app.jar" 11 days ago Exited (255) 10 days ago 0.0.0.0:80->8080/tcp xenodochial_volhard
# show file size with -s
# display short output with --format
$ docker ps -as --format "{{.ID}}: {{.Image}}: {{.Status}}: {{.Size}}"
cf381d99f454: debian: Exited (0) 9 minutes ago: 10MB (virtual 114MB)
d362659da5fc: markdownhtml:1.1: Exited (255) 10 days ago: 39.4MB (virtual 184MB)
If we test a lot of containers and this will take up the disk space very fast. We can delete the stopped container with a single command, but we have a better option --rm
, it will remove the container when it exit.
2.5 Run the container from the Debian image again, and send an echo
command and exit the container, this time with --rm
option.
$ docker run --rm debian echo 'hello'
'hello'
2.6 List the containers again; this time, there is no new stopped container in the list. With docker --rm
, the above-stopped container is removed when it exit.
$ ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf381d99f454 debian "ls -la" About a minute ago Exited (0) About a minute ago crazy_swirles
d362659da5fc markdownhtml:1.1 "java -jar app.jar" 11 days ago Exited (255) 10 days ago 0.0.0.0:80->8080/tcp xenodochial_volhard
Done. 🙂