1. 程式人生 > >centos7 docker 容器的建立/刪除/執行 映象 安裝/刪除

centos7 docker 容器的建立/刪除/執行 映象 安裝/刪除

docker  映象的刪除

映象刪除 : 1.先查出所有的容器2.停止執行的容器3.刪除要刪除的映象 根據映象ID刪除

列出所有執行的ID
docker ps 

列出所有的容器的id
docker ps -a -q




docker  容器刪除

刪除正在執行的容器
docker  rm -f 容器的id
刪除未執行的容器
docker  rm 容器的id

停止容器
docker stop 停止容器ID
啟動容器
docker start
重啟容器
docker restart




1.先停止執行的容器

docker ps 列出所有執行的容器
docker ps -a -q 顯示所有容器包括未執行的 -q靜默模式 只顯示容器編號

docker stop $(docker ps -a -q) 停止所有的container

docker rm $(docker ps -a -q) 刪除所有的container

docker images 檢視有哪些映象

想要刪除未標記的 images,也就是那些id為<None>的image的話可以用

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

要刪除全部image的話

docker rmi $(docker images -q)

2.再刪除映象

docker rmi -f 4ab4c602aa5e#映象的ID 

 

1.docker容器刪除

列出正在執行的容器

docker ps -a 是列出所有的容器 包括未執行的

[[email protected] /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                               NAMES
3684561d2e3d        redis:3.2           "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes                   6379/tcp, 0.0.0.0:6379->63791/tcp   adoring_heyrovsky
fa05e0515c49        redis:3.2           "docker-entrypoint.s…"   6 minutes ago       Created                                                            hardcore_hoover
77ed92a982de        redis:3.2           "docker-entrypoint.s…"   7 minutes ago       Created                                                            practical_mcnulty
be42f394f206        4ab4c602aa5e        "/hello"                 About an hour ago   Exited (0) About an hour ago                                       frosty_benz
9b0f6b466da9        4ab4c602aa5e        "/hello"                 5 weeks ago         Exited (0) 5 weeks ago                                             hungry_borg
3ddc87805976        4ab4c602aa5e        "/hello"                 5 weeks ago         Exited (0) 5 weeks ago                                             reverent_haibt
d37b9e96a5ae        redis:3.2           "docker-entrypoint.s…"   5 weeks ago         Exited (0) 5 weeks ago                                             wpredis

列出執行的容器只顯示 containerID容器id
[
[email protected]
/]# docker ps -q 3684561d2e3d 停止正在執行的容器 docker stop (容器的ID ===containerID) 停止 docker start (容器的ID ====containerID) 執行 docker restart(容器的ID ====containerID) 重啟 [[email protected] /]# docker stop 3684561d2e3d 3684561d2e3d [[email protected] /]# docker ps -q [[email protected]
/]# 刪除容器 docker rm 容器的ID(containerID) docker rm -f 強制刪除正在執行的容器 [[email protected] /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3684561d2e3d redis:3.2 "docker-entrypoint.s…" 19 minutes ago Exited (0) 13 minutes ago adoring_heyrovsky fa05e0515c49 redis:3.2 "docker-entrypoint.s…" 23 minutes ago Created hardcore_hoover 77ed92a982de redis:3.2 "docker-entrypoint.s…" 24 minutes ago Created practical_mcnulty 9b0f6b466da9 4ab4c602aa5e "/hello" 5 weeks ago Exited (0) 5 weeks ago hungry_borg 3ddc87805976 4ab4c602aa5e "/hello" 5 weeks ago Exited (0) 5 weeks ago reverent_haibt d37b9e96a5ae redis:3.2 "docker-entrypoint.s…" 5 weeks ago Exited (0) 5 weeks ago wpredis [[email protected] /]# docker ps -a -q 3684561d2e3d fa05e0515c49 77ed92a982de 9b0f6b466da9 3ddc87805976 d37b9e96a5ae [[email protected] /]# docker rm 3ddc87805976 3ddc87805976 [[email protected] /]# docker ps -a -q 3684561d2e3d fa05e0515c49 77ed92a982de 9b0f6b466da9 d37b9e96a5ae [[email protected] /]#

2.刪除映象

刪除映象
docker rmi -f 映象ID
-f 是強制刪除映象 刪除映象後 所建立的容器也一併刪除




[[email protected] /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              1babb1dde7e1        2 weeks ago         94.9MB
redis               3.2                 2fef532eadb3        2 months ago        76MB
[[email protected] /]# docker rmi -f 2fef532eadb3
Untagged: redis:3.2
Untagged: [email protected]:745bdd82bad441a666ee4c23adb7a4c8fac4b564a1c7ac4454aa81e91057d977
Deleted: sha256:2fef532eadb328740479f93b4a1b7595d412b9105ca8face42d3245485c39ddc
[[email protected] /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              1babb1dde7e1        2 weeks ago         94.9MB
[[email protected] /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
3684561d2e3d        2fef532eadb3        "docker-entrypoint.s…"   21 minutes ago      Exited (0) 15 minutes ago                       adoring_heyrovsky
fa05e0515c49        2fef532eadb3        "docker-entrypoint.s…"   25 minutes ago      Created                                         hardcore_hoover
77ed92a982de        2fef532eadb3        "docker-entrypoint.s…"   26 minutes ago      Created                                         practical_mcnulty
9b0f6b466da9        4ab4c602aa5e        "/hello"                 5 weeks ago         Exited (0) 5 weeks ago                          hungry_borg
d37b9e96a5ae        2fef532eadb3        "docker-entrypoint.s…"   5 weeks ago         Exited (0) 5 weeks ago                          wpredis
[[email protected] /]# 

 

3.docker建立容器

建立 一個執行的容器redis
[[email protected] /]# docker run -p 6379:63792 -v $PWD/data:/data  -d redis redis-server --appendonly yes    
f95d7f25584cb0eefd76a1bdb7f1ec14110df3b07d0aebbb7f273dc9a1d4a157
[[email protected] /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                               NAMES
f95d7f25584c        redis               "docker-entrypoint.s…"   16 seconds ago      Up 15 seconds               6379/tcp, 0.0.0.0:6379->63792/tcp   tender_keldysh
3684561d2e3d        2fef532eadb3        "docker-entrypoint.s…"   28 minutes ago      Exited (0) 22 minutes ago                                       adoring_heyrovsky
fa05e0515c49        2fef532eadb3        "docker-entrypoint.s…"   32 minutes ago      Created                                                         hardcore_hoover
77ed92a982de        2fef532eadb3        "docker-entrypoint.s…"   33 minutes ago      Created                                                         practical_mcnulty
9b0f6b466da9        4ab4c602aa5e        "/hello"                 5 weeks ago         Exited (0) 5 weeks ago                                          hungry_borg
d37b9e96a5ae        2fef532eadb3        "docker-entrypoint.s…"   5 weeks ago         Exited (0) 5 weeks ago                                          wpredis
[[email protected] /]# 

4.容器的匯入與匯入

 將執行的容器執行匯出來
[[email protected] /]# docker export f95d7f25584c > redis.tar
[[email protected] /]# ls
bin   data  etc   lib    lost+found  mnt  proc                redis.tar  run   srv  tmp  var
boot  dev   home  lib64  media       opt  redis-5.0.0.tar.gz  root       sbin  sys  usr
[[email protected] /]# 

將本地的容器匯入到docker中成為映象

[[email protected] /]# cat redis.tar | docker import - myredis #(這裡是映象名稱)
sha256:355190f9fe1ad47306cad81afb4933c2b513831a0789c3527b482c24772d60b9
[[email protected] /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                               NAMES
f95d7f25584c        redis               "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes                6379/tcp, 0.0.0.0:6379->63792/tcp   tender_keldysh
3684561d2e3d        2fef532eadb3        "docker-entrypoint.s…"   35 minutes ago      Exited (0) 29 minutes ago                                       adoring_heyrovsky
fa05e0515c49        2fef532eadb3        "docker-entrypoint.s…"   39 minutes ago      Created                                                         hardcore_hoover
77ed92a982de        2fef532eadb3        "docker-entrypoint.s…"   40 minutes ago      Created                                                         practical_mcnulty
9b0f6b466da9        4ab4c602aa5e        "/hello"                 5 weeks ago         Exited (0) 5 weeks ago                                          hungry_borg
d37b9e96a5ae        2fef532eadb3        "docker-entrypoint.s…"   5 weeks ago         Exited (0) 5 weeks ago                                          wpredis
[[email protected] /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myredis             latest              355190f9fe1a        34 seconds ago      91.6MB
redis               latest              1babb1dde7e1        2 weeks ago         94.9MB
[[email protected] /]#