docker视频总结:启动,删除,复制文件,保存

发布于 2019-12-25  642 次阅读


来自的视频地址:https://www.imooc.com/video/15646

命令 用途
docker  pull 获取image
docker build 创建image
docker images 列出image
docker run 运行container
docker ps 列出container
docker rm 删除container
docker rmi 删除image
docker cp 在host和container之间拷贝文件
docker commit 保存改动为新的image

 

[root@localhost ~]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
friendlyhello            latest              9267bd4853d6        3 months ago        148MB
w11930879/get-started    part2               9267bd4853d6        3 months ago        148MB
python                   2.7-slim            f462855313cd        3 months ago        137MB
hello-world              latest              fce289e99eb9        11 months ago       1.84kB
twang2218/gitlab-ce-zh   11.1                a570ada5159a        16 months ago       1.61GB
[root@localhost ~]# 
[root@localhost ~]# docker run -p 8080:80 -d nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
000eee12ec04: Pull complete 
eb22865337de: Pull complete 
bee5d581ef8b: Pull complete 
Digest: sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566
Status: Downloaded newer image for nginx:latest
20621967cd8fee29c13aca24afc7ad03988bb888e69043341e14bcb03bf88c8e
[root@localhost ~]# echo $?
0
[root@localhost ~]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
nginx                    latest              231d40e811cd        4 weeks ago         126MB
friendlyhello            latest              9267bd4853d6        3 months ago        148MB
w11930879/get-started    part2               9267bd4853d6        3 months ago        148MB
python                   2.7-slim            f462855313cd        3 months ago        137MB
hello-world              latest              fce289e99eb9        11 months ago       1.84kB
twang2218/gitlab-ce-zh   11.1                a570ada5159a        16 months ago       1.61GB
[root@localhost ~]# cd /Dockerfile/
[root@localhost Dockerfile]# ls
app.py              Dockerfile      git.nowtest.top.crt  git.nowtest.top.key  VirtualBox-6.0-6.0.14_133895_el7-1.x86_64.rpm
docker-compose.yml  docker-machine  git.nowtest.top.csr  requirements.txt
[root@localhost Dockerfile]# mkdir nginx
[root@localhost Dockerfile]# cd nginx/
[root@localhost nginx]# ls
[root@localhost nginx]# echo "你好,世界" > index.html
[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
20621967cd8f        nginx               "nginx -g 'daemon of…"   7 minutes ago       Up 7 minutes        0.0.0.0:8080->80/tcp   competent_khayyam
[root@localhost nginx]# 
[root@localhost nginx]# cat index.html 
你好,世界
[root@localhost nginx]# docker cp index.html 20621967cd8f://usr/share/nginx/html
[root@localhost nginx]# 
[root@localhost nginx]# #上面的配置复制过去,是临时的,如果关掉进程,会丢失index.html,恢复初始配置
[root@localhost nginx]# docker commit -m 'fun' 20621967cd8f nginx-fun
sha256:6b3b08a104c9d7df55203fc92b6e3af55bb6091238a3aa1f7ae01d4dd8b43879
[root@localhost nginx]# 
[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
20621967cd8f        nginx               "nginx -g 'daemon of…"   19 minutes ago      Up 19 minutes       0.0.0.0:8080->80/tcp   competent_khayyam
[root@localhost nginx]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
nginx-fun                latest              6b3b08a104c9        21 seconds ago      126MB
nginx                    latest              231d40e811cd        4 weeks ago         126MB
friendlyhello            latest              9267bd4853d6        3 months ago        148MB
w11930879/get-started    part2               9267bd4853d6        3 months ago        148MB
python                   2.7-slim            f462855313cd        3 months ago        137MB
hello-world              latest              fce289e99eb9        11 months ago       1.84kB
twang2218/gitlab-ce-zh   11.1                a570ada5159a        16 months ago       1.61GB
[root@localhost nginx]# 
[root@localhost nginx]# docker stop 20621967cd8f
20621967cd8f
[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost nginx]# docker run -p 8080:80 -d nginx-fun
62b70c5f20083882bfef86a33a158ed5ff7c4523bd70ad6aa7fad5a52cbf4b72
[root@localhost nginx]# 
[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
62b70c5f2008        nginx-fun           "nginx -g 'daemon of…"   5 seconds ago       Up 4 seconds        0.0.0.0:8080->80/tcp   friendly_kirch
[root@localhost nginx]# 
[root@localhost nginx]# 
[root@localhost nginx]# #删除镜像
[root@localhost nginx]# 
[root@localhost nginx]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
nginx-fun                latest              6b3b08a104c9        6 minutes ago       126MB
nginx                    latest              231d40e811cd        4 weeks ago         126MB
friendlyhello            latest              9267bd4853d6        3 months ago        148MB
w11930879/get-started    part2               9267bd4853d6        3 months ago        148MB
python                   2.7-slim            f462855313cd        3 months ago        137MB
hello-world              latest              fce289e99eb9        11 months ago       1.84kB
twang2218/gitlab-ce-zh   11.1                a570ada5159a        16 months ago       1.61GB
[root@localhost nginx]# docker rmi 231d40e811cd
Error response from daemon: conflict: unable to delete 231d40e811cd (cannot be forced) - image has dependent child images
[root@localhost nginx]# 
[root@localhost nginx]# 
[root@localhost nginx]# docker images          
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
nginx-fun                latest              6b3b08a104c9        7 minutes ago       126MB
nginx                    latest              231d40e811cd        4 weeks ago         126MB
friendlyhello            latest              9267bd4853d6        3 months ago        148MB
w11930879/get-started    part2               9267bd4853d6        3 months ago        148MB
python                   2.7-slim            f462855313cd        3 months ago        137MB
hello-world              latest              fce289e99eb9        11 months ago       1.84kB
twang2218/gitlab-ce-zh   11.1                a570ada5159a        16 months ago       1.61GB
[root@localhost nginx]# docker rmi 231d40e811cd
Error response from daemon: conflict: unable to delete 231d40e811cd (cannot be forced) - image has dependent child images
[root@localhost nginx]# 
[root@localhost nginx]# #删除docker images 中的镜像是用rmi ,上面报错是因为,nginx作为了nginx-fun的母镜像,无法强制删除
[root@localhost nginx]# 
[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
62b70c5f2008        nginx-fun           "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes        0.0.0.0:8080->80/tcp   friendly_kirch
[root@localhost nginx]# docker ps -a
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                        PORTS                                                               NAMES
62b70c5f2008        nginx-fun                     "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes                  0.0.0.0:8080->80/tcp                                                friendly_kirch
20621967cd8f        nginx                         "nginx -g 'daemon of…"   30 minutes ago      Exited (0) 6 minutes ago                                                                          competent_khayyam
68e6c7ec711f        twang2218/gitlab-ce-zh:11.1   "/assets/wrapper"        2 weeks ago         Exited (255) 34 minutes ago   0.0.0.0:2222->22/tcp, 0.0.0.0:8888->80/tcp, 0.0.0.0:8443->443/tcp   gitlab
444b7439c994        hello-world                   "/hello"                 2 weeks ago         Exited (0) 2 weeks ago                                                                            eager_lovelace
452c639ba611        w11930879/get-started:part2   "python app.py"          3 weeks ago         Exited (0) 3 weeks ago                                                                            nifty_mclean
c957edbc46ee        friendlyhello                 "python app.py"          3 weeks ago         Exited (137) 3 weeks ago                                                                          heuristic_clarke
3221ff467c40        friendlyhello                 "python app.py"          3 weeks ago         Exited (0) 3 weeks ago                                                                            infallible_sammet
463f34e5f68d        hello-world                   "/hello"                 3 weeks ago         Exited (0) 3 weeks ago                                                                            gifted_feynman
035e7dcfa0e8        a405b9a10b98                  "/bin/sh -c 'pip ins…"   3 months ago        Exited (2) 3 months ago                                                                           practical_pare
e7b585f4a4d6        hello-world                   "/hello"                 3 months ago        Exited (0) 3 months ago                                                                           busy_mirzakhani
d325ab909064        hello-world                   "/hello"                 3 months ago        Exited (0) 3 months ago                                                                           pensive_chatelet
[root@localhost nginx]# 
[root@localhost nginx]# docker rm 20621967cd8f d325ab909064 e7b585f4a4d6
20621967cd8f
d325ab909064
e7b585f4a4d6
[root@localhost nginx]# 
[root@localhost nginx]# 
[root@localhost nginx]# docker ps -a
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                        PORTS                                                               NAMES
62b70c5f2008        nginx-fun                     "nginx -g 'daemon of…"   7 minutes ago       Up 7 minutes                  0.0.0.0:8080->80/tcp                                                friendly_kirch
68e6c7ec711f        twang2218/gitlab-ce-zh:11.1   "/assets/wrapper"        2 weeks ago         Exited (255) 35 minutes ago   0.0.0.0:2222->22/tcp, 0.0.0.0:8888->80/tcp, 0.0.0.0:8443->443/tcp   gitlab
444b7439c994        hello-world                   "/hello"                 2 weeks ago         Exited (0) 2 weeks ago                                                                            eager_lovelace
452c639ba611        w11930879/get-started:part2   "python app.py"          3 weeks ago         Exited (0) 3 weeks ago                                                                            nifty_mclean
c957edbc46ee        friendlyhello                 "python app.py"          3 weeks ago         Exited (137) 3 weeks ago                                                                          heuristic_clarke
3221ff467c40        friendlyhello                 "python app.py"          3 weeks ago         Exited (0) 3 weeks ago                                                                            infallible_sammet
463f34e5f68d        hello-world                   "/hello"                 3 weeks ago         Exited (0) 3 weeks ago                                                                            gifted_feynman
035e7dcfa0e8        a405b9a10b98                  "/bin/sh -c 'pip ins…"   3 months ago        Exited (2) 3 months ago                                                                           practical_pare

 

删除镜像报错

使用IMAGE ID删除镜像报错Error response from daemon: conflict: unable to delete d0957ffdf8a2 (must be forced) - image is referenced in multiple repositories是因为有多个镜像的IMAGE ID是相同的,可以使用REPOSITORY+TAG的方式去删除;如下:

[root@localhost php-nginx-compose]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
nginx_ghost_mysql_ghost-app   latest              0dead1e98a63        3 weeks ago         403MB
ghost                         latest              a3fced6c343c        4 weeks ago         403MB
w11930879/get-started         part2               9267bd4853d6        3 months ago        148MB
friendlyhello                 latest              9267bd4853d6        3 months ago        148MB
python                        2.7-slim            f462855313cd        4 months ago        137MB

--这里我centos:6和centos:centos6的IMAGE ID是相同的,删除就会报错。

解决办法可以用REPOSITORY+TAG的方式删除

[root@localhost php-nginx-compose]# docker rmi w11930879/get-started:part2
Untagged: w11930879/get-started:part2
Untagged: w11930879/get-started@sha256:6c231674410c369793bb65663b5057016566b6f724f656caabe6fbedbd233a10

再次查看验证是成功的