Removing Docker Containers and Images

In a recent post aboutDocker, we looked into some things that differentiate Docker containers from Virtual Machines. I also gave a brief example of creating your first Docker image. If any of that piqued your interest and you started just trying stuff, you end up like I did with a BUNCH of images now cluttering up your machine and wondering how in the world can I clean this up? Here are a few commands that will get your Docker environment under control.

List Docker Images

  • docker images

Docker provides a number of simple options to allow the admin to manage the environment. Docker images for examples lets us see all the docker images on the machine.

  1. Last login: Fri Apr 14 17:16:30 on ttys001
  2. Danny: > docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. oraclelinux latest 62e4327762a5 2 days ago 225 MB
  5. pythonslim latest 2f61057fee22 3 weeks ago 318 MB
  6. <none> <none> c133f715830e 3 weeks ago 310 MB
  7. sqlslim latest 6cab4fd15c1d 3 weeks ago 348 MB
  8. oels latest 9546896416f7 3 weeks ago 114 MB
  9. sqlcl latest 980f85fc564e 4 weeks ago 463 MB
  10. oraclelinux 7-slim f005b5220b05 7 weeks ago 114 MB

  

Wouldn’t you love to clear some of that space up?

Remove Docker Images

  • docker rmi <image id>

Docker provides rmi option to delete images. Let go ahead and put this one to work and remove the first image

  1. Danny: > docker rmi 62e4327762a5
  2. Error response from daemon: conflict: unable to delete 62e4327762a5 (must be forced)
  3. - image is being used by stopped container be2d81554955

  

Well what happened? Look closely, this image is being used by a stopped container. Alright so now we need to go find that container. On to our next command to get rid of the Docker Container.

Remove Docker Containers

  • docker rm <container id>

Again, Docker provides an option, rm that allows us to delete any docker containers from our local system. Before we can run this in general, we need to find out Container IDs. In the situation above, we actually have it, but let’s user Docker to find these ids for us with the  ps option and the  -a parameter so we can see all the Docker Containers even if they are not running.

  • docker ps -a 
  1. Danny: > docker ps-a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 9f9d34aee084 sqlslim "/bin/bash" 4 minutes ago Exited (0) 4 minutes ago xenodochial_darwin
  4. e27743499f8d sqlslim "/bin/bash" 4 minutes ago Exited (0) 4 minutes ago epic_boyd
  5. 63b123e4f50c oraclelinux "/bin/bash" 24 hours ago Exited (0) 24 hours ago amazing_wilson
  6. be2d81554955 oraclelinux "/bin/bash" 24 hours ago Exited (0) 24 hours ago elated_elion
  7. 9e701197cddc pythonslim "/bin/bash" 3 weeks ago Exited (0) 3 weeks ago keen_ramanujan
  8. 58726602aa40 c133f715830e "/bin/bash" 3 weeks ago Exited (0) 3 weeks ago zealous_spence
  9. 5a04b41413ce sqlslim "sql dbryant/eyeam..." 3 weeks ago Exited (0) 3 weeks ago sharp_lichterman
  10. b7c7569d21fc sqlslim "sql dbryant/eyeam..." 3 weeks ago Exited (1) 3 weeks ago naughty_carson
  11. f852641bf275 sqlslim "sql sys/eyeamd1@1..." 3 weeks ago Exited (1) 3 weeks ago kind_kare
  12. 6e3120482e96 sqlslim "sql sys/eyeamd1@1..." 3 weeks ago Exited (1) 3 weeks ago blissful_austin
  13. 47efed596e8b sqlslim "sql sys@192.168.5..." 3 weeks ago Exited (1) 3 weeks ago serene_hamilton
  14. 5b608a95492b sqlslim "sql dbryant/eyeam..." 3 weeks ago Exited (1) 3 weeks ago friendly_hodgkin
  15. bdabab59992b sqlslim "sql dbryant/eyeam..." 3 weeks ago Exited (1) 3 weeks ago gifted_albattani
  16. 566da1f2d8a3 99ad69730de1 "/bin/sh -c 'unzip..." 3 weeks ago Exited (127) 3 weeks ago loving_jones

  

Now we have everything we need to get rid of those container and images. By the way, did you notice this guy here (be2d81554955) about 4 rows down? That’s your offending Container keeping you from delete that image.

Stop & Remove All Docker Containers

I’m going to nuclear and stop and delete everything with a few “shortcuts.” Using the examples above, you can stop and delete individual container by simply entering the container id after the docker option. Here let’s blow them all away:

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

Here I am nesting two commands that you have already seen. The docker stop command with the  docker ps -a -q (the -q is for quiet — only show IDs). This in essence passes a list of Docker Container IDs to the docker stop command resulting in the stoppage of all Docker Containers.

  1. Danny: > docker stop $(docker ps -a -q)
  2. 9f9d34aee084
  3. e27743499f8d
  4. 63b123e4f50c
  5. be2d81554955
  6. 9e701197cddc
  7. 58726602aa40
  8. 5a04b41413ce
  9. b7c7569d21fc
  10. f852641bf275
  11. 6e3120482e96
  12. 47efed596e8b
  13. 5b608a95492b
  14. bdabab59992b
  15. 566da1f2d8a3

  Now that the Containers have been stopped, I’m going to nest another command with the docker ps -a -q command to remove/delete the Container

  1. Danny: > docker rm $(docker ps-a-q)
  2. 9f9d34aee084
  3. e27743499f8d
  4. 63b123e4f50c
  5. be2d81554955
  6. 9e701197cddc
  7. 58726602aa40
  8. 5a04b41413ce
  9. b7c7569d21fc
  10. f852641bf275
  11. 6e3120482e96
  12. 47efed596e8b
  13. 5b608a95492b
  14. bdabab59992b
  15. 566da1f2d8a3
  16.  
  17. Danny: > docker ps-a
  18. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

  

Now you can see that all the Container are gone and move on with removing the Docker Images.

First re-issue the docker images command to get a look at the images that are currently out there,

  1. Danny: > docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. oraclelinux latest 62e4327762a5 2 days ago 225 MB
  4. pythonslim latest 2f61057fee22 3 weeks ago 318 MB
  5. <none> <none> c133f715830e 3 weeks ago 310 MB
  6. sqlslim latest 6cab4fd15c1d 3 weeks ago 348 MB
  7. oels latest 9546896416f7 3 weeks ago 114 MB
  8. sqlcl latest 980f85fc564e 4 weeks ago 463 MB
  9. oraclelinux 7-slim f005b5220b05 7 weeks ago 114 MB

  and make sure that we take care of that initial attempt that failed.

  1. Danny: > dockerrmi 62e4327762a5
  2. Untagged: oraclelinux:latest
  3. Untagged: oraclelinux@sha256:39470a3bde74c099eefe0656635718c31d199e27cdc026742257c0d445f7f7e9
  4. Deleted: sha256:62e4327762a51cacfcca77b032e2e5adb73bdc41836ed1180d0fe8f7cebba932
  5. Deleted: sha256:40c24f62a02f157ab14f45407e9dd284b05299b9d90c1fb899f716e42f2bd912

  

As you can see here, the image was successfully deleted. Now for the nuclear option! Getting rid of them all with, and you guessed it, another nested option.

  • docker rmi $(docker images -q)
  1. Danny: > docker rmi $(docker images-q)
  2. Untagged: pythonslim:latest
  3. Deleted: sha256:2f61057fee22ed529119aba221b61afce076a05b81dd374320044a2d01f932a7
  4. Deleted: sha256:8061aead716665ce0746794932d9f96f489bc5ed73b58789837196e92c082e20
  5. Deleted: sha256:ff49d613d58d807cd87c589498a6eac39d7700f0595c6a1a727278c6a9af1243
  6. Deleted: sha256:c133f715830e6ddf0c861675107015edbffe6f42fab7c23d9a1be5b84c9770dd
  7. Deleted: sha256:68331ccc086054bfb451af10b330d12702b147c3e5628a6c3d8f3ef96a56ad60
  8. Deleted: sha256:87e67f661cf0f66f4c48b4025445ec6ea88d8df5c008930a5fbf0bf43c312e57
  9. Untagged: sqlslim:latest
  10. Deleted: sha256:6cab4fd15c1d5eb9d45d1e5e22df91ec914571eaaf2cc679d6fbdc16610ba246
  11. Deleted: sha256:5d0a16807c54d5eaa8485843efe7c8ac8a01fc107c66bc6d3c845ab1b83f291c
  12. Deleted: sha256:3b73082d9f20f368d1bd62a10318d2300aecba7590aff9d5e0be561bea2bc587
  13. Deleted: sha256:99ad69730de183ba793a685e3684c4f8c7618cf6d5a083fe3146b8fdab03aa8f
  14. Deleted: sha256:ec69f880eb5d70d215196fc0754ee3658886c3ae5b88b5cd0b45dfac10c47770
  15. Untagged: oels:latest
  16. Deleted: sha256:9546896416f76c089644165d018bb0146dd58f2800edced807477093f898e42f
  17. Deleted: sha256:f2af978d2706d37570523170bcfe2c0befee0aafcdf98f7ed71df7419fed8b74
  18. Deleted: sha256:d3ed559d48454ec158117b6e9f907ef87db26071e7c8f19515150db8d8f461a6
  19. Untagged: sqlcl:latest
  20. Deleted: sha256:980f85fc564ee7ae9f224ad3000aef33714cebb6826e54f49ef7efdb87d4be76
  21. Deleted: sha256:bbf0c41ed3632412acd3fa68799b68013069d519ea38376ba174bc753c26db83
  22. Deleted: sha256:26086900ad8f2e62172ef8ee3e5ce2a0e3f6a6ebbcd038d99ff0f65fe6dff285
  23. Deleted: sha256:ef746b0f221637bc6e833331045d3e0c3d5e9bca76dad0ac74dddca48b934ca4
  24. Deleted: sha256:488bdfc5ae796dc06e374d0d95a60ab91f39900bc86bc3343e689e5ea865f119
  25. Deleted: sha256:5a42e075a32bd55aa2131239a235dbaf95426a8333ef8f6ce16ba8a3af1ba3c2
  26. Deleted: sha256:d423508a08c8c0039dd3b4baef9e8964e0925ef57c18647ce30b655a68f8848d
  27. Untagged: oraclelinux:7-slim
  28. Untagged: oraclelinux@sha256:f3a78afd456061bb897b9f2b54b568dec3973efccf2b086d602fabb94069fb6d
  29. Deleted: sha256:f005b5220b05d380d279657c268e4024e72f4141fa070e515f81a9eab5157652
  30. Deleted: sha256:dae53bfc95d17daa53043f78a411d88955bcbaa93f3feeb91e6eac044ad81052
  31.  
  32. Danny: > docker images
  33. REPOSITORY TAG IMAGE ID CREATED SIZE
  34. Danny: >

  

here we have it Container and Images are now removed from your machine. Simply run your docker build command(s) recreate your images and containers.

Enjoy!

Removing Docker Containers and Images的更多相关文章

  1. Understanding how uid and gid work in Docker containers

    转自:https://medium.com/@mccode/understanding-how-uid-and-gid-work-in-docker-containers-c37a01d01cf Un ...

  2. Running Elixir in Docker Containers

    转自:https://www.poeticoding.com/running-elixir-in-docker-containers/ One of the wonderful things abou ...

  3. docker log 批量删除报错: find: `/var/lib/docker/containers/': 没有那个文件或目录

    问题描述: 服务器上面docker log太多,打算用之前写的批量清理shell脚本清理掉,但是发现报错. find: `/var/lib/docker/containers/': 没有那个文件或目录 ...

  4. [Docker] Run Short-Lived Docker Containers

    Learn the benefits of running one-off, short-lived Docker containers. Short-Lived containers are use ...

  5. [Docker] Run, Stop and Remove Docker Containers

    In this lesson, we'll find out the basics of running Docker containers. We'll go over how to downloa ...

  6. [Docker] Prune Old Unused Docker Containers and Images

    In this lesson, we will look at docker container prune to remove old docker containers. We can also ...

  7. Configuring and Running Django + Celery in Docker Containers

    Configuring and Running Django + Celery in Docker Containers  Justyna Ilczuk  Oct 25, 2016  0 Commen ...

  8. Docker目录/var/lib/docker/containers文件太大

    Docker在不重建容器的情况下,日志文件默认会一直追加,时间一长会逐渐占满服务器的硬盘的空间,内存消耗也会一直增加,本篇来了解一些控制日志文件的方法. 查出占用磁盘较大的文件 Docker 的日志文 ...

  9. A workaround to change shared memory size for Docker containers in AWS ECS

    Issue Because of not supporting to specify the following docker run parameter, containers in ECS can ...

随机推荐

  1. git代理设置

    git config --global http.proxy http://127.0.0.1:1080git config --global https.proxy https://127.0.0. ...

  2. 新装Windows Server 2008 R2的设置

    原文链接:https://www.kafan.cn/edu/2898054.html 1.2008设置自动登录 具体方法:开始→运行→输入“rundll32 netplwiz.dll,UsersRun ...

  3. Apache- DBUtils框架学习

    一.DBUtils DBUtils 的介绍 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,,DBUtils封装了对JDBC的操作,简 ...

  4. poi excel超出65536行数限制自动扩展Invalid row number (65536) outside allow

    1.xls一个sheet只能装65536行,多余则报错 poi包导出或写入excel超出65536报错: java.lang.IllegalArgumentException: Invalid row ...

  5. linux 任务调度

    crontab 定时任务 -e  编辑 -l  查看 -r  删除 参数 * * * * * 分钟  小时  天数   月   星期几

  6. BBS--后台管理页面,编辑文章,xss攻击

    1 1.对文章进行增删改查 # 后台管理url re_path(r'^cn_backend/$', views.cn_backend, name='cn_backend'), re_path(r'^c ...

  7. vue:图片切换动态显示

    <img :src="切换条件 ? require('xxx.png') : require('xxx.png')" />

  8. World Cup 996B(排队模拟)

    题意:有n个通道,按顺序每一次站一个通道,直到所站的通道没有人 分析:模拟这个过程 #include<cstdio> int main() { ]; while(~scanf(" ...

  9. swarm on ubuntu

    转自:https://www.cnblogs.com/jsonhc/p/7862518.html 由于在centos7系统上使用docker-machine安装的swarm实现服务的负载均衡一直失败, ...

  10. (Java)怎么去掉字符串数组中重复的值?

    String fdbs = "WXB,WXA,FDA,WXB"; String[] str = fdbs.split(","); Set set = new H ...