第一节 Docker的入门

1 什么是docker

 不必赘述,自己可以去百度下,哈哈
docker必须要有的东西: 镜像 容器以及仓库

2 Docker组件

Docker Client: Docker 的客户端
Docker Server:接收Docker Client发送的请求,并按照相应的路由规则实现路由分发
Docker 镜像:Docker镜像运行之后变成容器
Docker Registry: 这里更多是个仓库管理器

3 docker相对于传统的虚拟机的优势

1 启动秒级
2 资源利用率高,主机上可以运行数以千个容器
3 基本不消耗系统资源,实现运行在docker里面的应用性能很高

4 相对于传统的虚拟化技术,优势有哪些?

a 快速支付和部署
b 高效虚拟化 容器的运行不需要额外的hypervisor支持,docker属于内核级别的虚拟化,所以其性能更高
c 更轻松的钱和扩展。Docker容器可以在任意的平台上运行
d 高效的管理 只需要小小的修改就可以替代以往大量的更新工作

5 Docker 基本概念

1 Image 镜像
2 Container 容器 可以理解成简化版的Linux系统
3 Repository 仓库 存放镜像的地方。仓库注册服务器是用来管理镜像的,里面存放多个仓库,仓库里面有很多镜像,每个镜像都有不同的tag。

6 Docker的安装

1 在centos7中可以直接使用yum install docker
2 设置启动docker服务 systemctl start docker或者systemctl start docker.service
3 设置开机自启动 system enable docker.service

7 基本操作

1 怎样查询镜像 docker search mysql -s 100 查询mysql的镜像,默认查询其starts大于100的
2 拉取镜像 docker pull mysql 
3 怎样运行镜像 docker run -it  centos /bin/bash, 退出是exit, 选项-t 为容器分配一个伪终端 -i 表示交互

8 怎样创建镜像

1 修改已经有的镜像,然后使用commit提交就可以产生一个新的镜像

a) docker run -it centos /bin/bash
b) 修改镜像
c) docker commit -m "This is a description" -a "This is inforation of auther" 1f8b8eb61355 haoshuliang/centos:test  #这里是生成镜像到本地的仓库
实操
[root@VM_0_15_centos docker]# docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                      PORTS                  NAMES
69dff30b6322        nginx                "/bin/bash"         24 hours ago        Exited (0) 25 minutes ago                          boring_brown
8e9313993a0a        kinogmt/centos-ssh   "/bin/bash"         24 hours ago        Up 11 minutes               0.0.0.0:1234->22/tcp   youthful_lichterman
6295c6878dcb        kinogmt/centos-ssh   "/bin/bash"         25 hours ago        Up 10 minutes               22/tcp                 musing_meninsky
6a1d463e4346        4cbe7aa905e7         "/bin/bash"         25 hours ago        Exited (127) 25 hours ago                          cool_curie
[root@VM_0_15_centos docker]# docker commit -m "This is a centos with ssh" -a "haoshuliang" 6295c6878dcb  haoshuliang/centos-ssh-passwd
sha256:0c43616661009e8a72dd08d4bf6dffc05f24d50b3e47824b967be465037c75a7
[root@VM_0_15_centos docker]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
haoshuliang/centos-ssh-passwd   latest              0c4361666100        8 seconds ago       931MB
haoshuliang/centos              test                ee87dab68e9d        33 hours ago        250MB
nginx                           latest              540a289bab6c        3 weeks ago         126MB
centos                          latest              0f3e07c0138f        6 weeks ago         220MB
kinogmt/centos-ssh              latest              dc8713dad282        3 years ago         773MB

2 利用Dockfile 创建镜像

1)每一个命令都会进行封装一层,并且层数有限制,最高127
2)在目录/app/docker/centos/dockfile下编写Dockerfile,如下
# 这里就是备注
FROM centos
MAINTAINER REGAN haoshuliang
RUN yum install mysql -qqy
3)执行命令
执行创建镜像的命令
[root@localhost dockfile]# docker build -t runoob/ubuntu:v1 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM centos
 ---> 0f3e07c0138f
Step 2/3 : MAINTAINER REGAN haoshuliang
 ---> Using cache
 ---> 00974c980554
Step 3/3 : RUN echo "hello"
 ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
 ---> Running in 13d33e2ff867
hello
Removing intermediate container 13d33e2ff867
 ---> 5fe67db864a9
Successfully built 5fe67db864a9
Successfully tagged runoob/ubuntu:v1 # 这里就是表示成功
查看下所有的镜像
[root@localhost dockfile]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
runoob/ubuntu       v1                  5fe67db864a9        About a minute ago   220MB
centos              latest              0f3e07c0138f        6 weeks ago          220MB
hello-world         latest              fce289e99eb9        10 months ago        1.84kB

9 怎样修改tag

执行命令
docker tag <imgae_id>  haoshuliang/centos:v1
下面是实际操作
# 下面是执行修改tag的操作
[root@localhost dockfile]# docker tag 5fe67db864a9 haoshuliang/centos_with_modiy:v1 
# 下面列出所有的镜像,发现只是copy了一个
[root@localhost dockfile]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
haoshuliang/centos_with_modiy   v1                  5fe67db864a9        2 minutes ago       220MB
runoob/ubuntu                   v1                  5fe67db864a9        2 minutes ago       220MB
centos                          latest              0f3e07c0138f        6 weeks ago         220MB
hello-world                     latest              fce289e99eb9        10 months ago       1.84kB


10 怎样导入镜像

方法1:

下面是规范
 cat alibaba-rocketmq-3.2.6.tar.gz | docker import - rocketmq:3.2.6
实操:
[root@localhost dockfile]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
haoshuliang/centos_with_modiy   v1                  5fe67db864a9        17 minutes ago      220MB
centos                          latest              0f3e07c0138f        6 weeks ago         220MB
hello-world                     latest              fce289e99eb9        10 months ago       1.84kB
[root@localhost dockfile]# docker rmi 5fe67db864a9 
Untagged: haoshuliang/centos_with_modiy:v1
Deleted: sha256:5fe67db864a920c6d142ff4cb1ade6fb919e60ef31b1c906b18ea4a50d553476
[root@localhost dockfile]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0f3e07c0138f        6 weeks ago         220MB
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
[root@localhost dockfile]# cat centos.tar.gz | docker import - haoshuliang/centos_with_modiy:v1
sha256:ee42fdec73fa6b17994a1c46a45e73b7abba6c7d2658358f0bbb332e9e0db606
[root@localhost dockfile]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
haoshuliang/centos_with_modiy   v1                  ee42fdec73fa        9 seconds ago       227MB
centos                          latest              0f3e07c0138f        6 weeks ago         220MB
hello-world                     latest              fce289e99eb9        10 months ago       1.84kB


方法二:

规范
docker load --input <镜像名字.tar.gz>
实操:
[root@localhost dockfile]# docker load --input centos.tar.gz 
Loaded image: haoshuliang/centos_with_modiy:v1
[root@localhost dockfile]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
haoshuliang/centos_with_modiy   v1                  5fe67db864a9        17 minutes ago      220MB
centos                          latest              0f3e07c0138f        6 weeks ago         220MB
hello-world                     latest              fce289e99eb9        10 months ago       1.84kB
note: 镜像必须是tar.gz 结尾的

11 怎样将自己的镜像上传到共享仓库

docker push haoshuliang/centos:v1.1

12 怎样将镜像保存到本地

规范
docker save -o <镜像的名字.tar.gz> <仓库的镜像名称>
实操:
[root@localhost dockfile]# docker save -o centos.tar.gz haoshuliang/centos_with_modiy
[root@localhost dockfile]# ls
centos.tar.gz  Dockerfile


13 怎样删除镜像

规范
docker rmi <镜像的id>
实操
[root@localhost dockfile]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
haoshuliang/centos_with_modiy   v1                  5fe67db864a9        12 minutes ago      220MB
runoob/ubuntu                   v1                  5fe67db864a9        12 minutes ago      220MB
centos                          latest              0f3e07c0138f        6 weeks ago         220MB
hello-world                     latest              fce289e99eb9        10 months ago       1.84kB
[root@localhost dockfile]# docker rmi 5fe67db864a9
Error response from daemon: conflict: unable to delete 5fe67db864a9 (must be forced) - image is referenced in multiple repositories
# 下面是强制删除,因为上面的报错中已经显示该镜像id已经被多个repositories 绑定,所以要想删除的话就必须执行强制执行
[root@localhost dockfile]# docker rmi -f 5fe67db864a9
Untagged: haoshuliang/centos_with_modiy:v1
Untagged: runoob/ubuntu:v1
Deleted: sha256:5fe67db864a920c6d142ff4cb1ade6fb919e60ef31b1c906b18ea4a50d553476

14 docker 容器的操作

启动方式一:docker run centos

操作过程

  1. a) 检查本地是否存在镜像,如果没有该镜像,则默认需要去远程仓库进行拉取
  2. b) 使用镜像创建并启动容器
  3. c) 内核给该容器分配一个文件系统,并在只读的镜像层挂载一层可读写的层
  4. d) 从宿主机主机配置的网桥接口中桥接一个虚拟接口到容器中去
  5. e) 从地址池分配一个ip地址给容器
  6. f) 执行程序
  7. g) 容器被终止


启动方式二: docker start <容器id>

实操
[root@localhost dockfile]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
9700691b7c7d        centos              "cal"                    15 minutes ago      Exited (0) 15 minutes ago                          epic_banach
3ec1dd341997        centos              "/bin/bash"              40 minutes ago      Up 15 seconds                                      gifted_williamson
6b4199286ff9        centos              "/bin/bash"              41 minutes ago      Exited (127) 41 minutes ago                        cranky_haslett
686d4162c34d        00974c980554        "/bin/sh -c 'yum -qq…"   44 minutes ago      Exited (1) 43 minutes ago                          elastic_solomon
bf04a2613883        00974c980554        "/bin/sh -c 'yum -qq…"   52 minutes ago      Exited (1) 51 minutes ago                          nervous_hypatia
ce534c08afce        00974c980554        "/bin/sh -c 'yum ins…"   59 minutes ago      Exited (1) 58 minutes ago                          unruffled_feynman
5031361bf654        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       adoring_roentgen
0a8bba509332        hello-world         "/hello"                 13 days ago         Exited (0) 13 days ago                             peaceful_kowalevski
[root@localhost dockfile]# docker stop 3ec1dd341997
3ec1dd341997
[root@localhost dockfile]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
9700691b7c7d        centos              "cal"                    16 minutes ago      Exited (0) 16 minutes ago                          epic_banach
3ec1dd341997        centos              "/bin/bash"              41 minutes ago      Exited (0) 6 seconds ago                           gifted_williamson
6b4199286ff9        centos              "/bin/bash"              42 minutes ago      Exited (127) 41 minutes ago                        cranky_haslett
686d4162c34d        00974c980554        "/bin/sh -c 'yum -qq…"   44 minutes ago      Exited (1) 43 minutes ago                          elastic_solomon
bf04a2613883        00974c980554        "/bin/sh -c 'yum -qq…"   53 minutes ago      Exited (1) 52 minutes ago                          nervous_hypatia
ce534c08afce        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) 59 minutes ago                          unruffled_feynman
5031361bf654        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       adoring_roentgen
0a8bba509332        hello-world         "/hello"                 13 days ago         Exited (0) 13 days ago                             peaceful_kowalevski
[root@localhost dockfile]# docker start 3ec1dd341997
3ec1dd341997
[root@localhost dockfile]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
9700691b7c7d        centos              "cal"                    16 minutes ago      Exited (0) 16 minutes ago                          epic_banach
3ec1dd341997        centos              "/bin/bash"              41 minutes ago      Up 4 seconds                                       gifted_williamson
6b4199286ff9        centos              "/bin/bash"              42 minutes ago      Exited (127) 41 minutes ago                        cranky_haslett
686d4162c34d        00974c980554        "/bin/sh -c 'yum -qq…"   45 minutes ago      Exited (1) 44 minutes ago                          elastic_solomon
bf04a2613883        00974c980554        "/bin/sh -c 'yum -qq…"   53 minutes ago      Exited (1) 52 minutes ago                          nervous_hypatia
ce534c08afce        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) 59 minutes ago                          unruffled_feynman
5031361bf654        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       adoring_roentgen
0a8bba509332        hello-world         "/hello"                 13 days ago         Exited (0) 13 days ago                             peaceful_kowalevski
Note: 适用于已经创建的容器,并得到id的名称


启动方式三:保护态运行

规范
docker run -d centos /bin/bash -c "while true; do echo "hello"; sleep 1; done"
实际操作:
# 执行ps -a 查看docker容器的运行状态,并获取容器的id
[root@localhost dockfile]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
c86e12b7c04a        centos              "/bin/bash -c 'while…"   2 minutes ago       Up 2 minutes                                       bold_bhabha
9700691b7c7d        centos              "cal"                    21 minutes ago      Exited (0) 21 minutes ago                          epic_banach
3ec1dd341997        centos              "/bin/bash"              46 minutes ago      Up 4 minutes                                       gifted_williamson
6b4199286ff9        centos              "/bin/bash"              47 minutes ago      Exited (127) 46 minutes ago                        cranky_haslett
686d4162c34d        00974c980554        "/bin/sh -c 'yum -qq…"   49 minutes ago      Exited (1) 48 minutes ago                          elastic_solomon
bf04a2613883        00974c980554        "/bin/sh -c 'yum -qq…"   58 minutes ago      Exited (1) 57 minutes ago                          nervous_hypatia
ce534c08afce        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       unruffled_feynman
5031361bf654        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       adoring_roentgen
0a8bba509332        hello-world         "/hello"                 13 days ago         Exited (0) 13 days ago                             peaceful_kowalevski
# 根据前面获取的容器id,查看打印的日志
[root@localhost dockfile]# docker logs c86e12b7c04a
hello
hello
hello
hello
...
# 下面是再执行一次,可以按键这个容器又重新创建了一个
[root@localhost dockfile]# docker run -d centos /bin/bash -c "while true; do echo "hello"; sleep 1; done"
WARNING: IPv4 forwarding is disabled. Networking will not work.
0c7c5e3d4b89996dd1d9f2db3ad8db3445178e71252773bf256de203937394d5
[root@localhost dockfile]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
0c7c5e3d4b89        centos              "/bin/bash -c 'while…"   4 seconds ago       Up 3 seconds                                       tender_mahavira
c86e12b7c04a        centos              "/bin/bash -c 'while…"   3 minutes ago       Up 3 minutes                                       bold_bhabha
9700691b7c7d        centos              "cal"                    23 minutes ago      Exited (0) 23 minutes ago                          epic_banach
3ec1dd341997        centos              "/bin/bash"              48 minutes ago      Up 6 minutes                                       gifted_williamson
6b4199286ff9        centos              "/bin/bash"              48 minutes ago      Exited (127) 48 minutes ago                        cranky_haslett
686d4162c34d        00974c980554        "/bin/sh -c 'yum -qq…"   51 minutes ago      Exited (1) 50 minutes ago                          elastic_solomon
bf04a2613883        00974c980554        "/bin/sh -c 'yum -qq…"   About an hour ago   Exited (1) 58 minutes ago                          nervous_hypatia
ce534c08afce        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       unruffled_feynman
5031361bf654        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       adoring_roentgen
0a8bba509332        hello-world         "/hello"                 13 days ago         Exited (0) 13 days ago                             peaceful_kowalevski


怎样删除容呢?

规范:
docker rm -f <容器id>
实操
[root@localhost dockfile]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
0c7c5e3d4b89        centos              "/bin/bash -c 'while…"   2 minutes ago       Up 2 minutes                                       tender_mahavira
c86e12b7c04a        centos              "/bin/bash -c 'while…"   6 minutes ago       Up 6 minutes                                       bold_bhabha
9700691b7c7d        centos              "cal"                    25 minutes ago      Exited (0) 25 minutes ago                          epic_banach
3ec1dd341997        centos              "/bin/bash"              50 minutes ago      Up 9 minutes                                       gifted_williamson
6b4199286ff9        centos              "/bin/bash"              51 minutes ago      Exited (127) 50 minutes ago                        cranky_haslett
686d4162c34d        00974c980554        "/bin/sh -c 'yum -qq…"   53 minutes ago      Exited (1) 52 minutes ago                          elastic_solomon
bf04a2613883        00974c980554        "/bin/sh -c 'yum -qq…"   About an hour ago   Exited (1) About an hour ago                       nervous_hypatia
ce534c08afce        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       unruffled_feynman
5031361bf654        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       adoring_roentgen
0a8bba509332        hello-world         "/hello"                 13 days ago         Exited (0) 13 days ago                             peaceful_kowalevski
[root@localhost dockfile]# docker rm 0c7c5e3d4b89
Error response from daemon: You cannot remove a running container 0c7c5e3d4b89996dd1d9f2db3ad8db3445178e71252773bf256de203937394d5. Stop the container before attempting removal or force remove
[root@localhost dockfile]# docker stop 0c7c5e3d4b89
0c7c5e3d4b89
[root@localhost dockfile]# docker rm 0c7c5e3d4b89
0c7c5e3d4b89
[root@localhost dockfile]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
c86e12b7c04a        centos              "/bin/bash -c 'while…"   6 minutes ago       Up 6 minutes                                       bold_bhabha
9700691b7c7d        centos              "cal"                    26 minutes ago      Exited (0) 26 minutes ago                          epic_banach
3ec1dd341997        centos              "/bin/bash"              51 minutes ago      Up 9 minutes                                       gifted_williamson
6b4199286ff9        centos              "/bin/bash"              51 minutes ago      Exited (127) 51 minutes ago                        cranky_haslett
686d4162c34d        00974c980554        "/bin/sh -c 'yum -qq…"   54 minutes ago      Exited (1) 53 minutes ago                          elastic_solomon
bf04a2613883        00974c980554        "/bin/sh -c 'yum -qq…"   About an hour ago   Exited (1) About an hour ago                       nervous_hypatia
ce534c08afce        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       unruffled_feynman
5031361bf654        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       adoring_roentgen
0a8bba509332        hello-world         "/hello"                 13 days ago         Exited (0) 13 days ago                             peaceful_kowalevski
[root@localhost dockfile]# docker rm c86e12b7c04a
Error response from daemon: You cannot remove a running container c86e12b7c04ac5829ee48ce978dcccd9557ee0fa216444b8f01501f24e3560d8. Stop the container before attempting removal or force remove
[root@localhost dockfile]# docker rm -f c86e12b7c04a
c86e12b7c04a
[root@localhost dockfile]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
9700691b7c7d        centos              "cal"                    26 minutes ago      Exited (0) 26 minutes ago                          epic_banach
3ec1dd341997        centos              "/bin/bash"              51 minutes ago      Up 10 minutes                                      gifted_williamson
6b4199286ff9        centos              "/bin/bash"              52 minutes ago      Exited (127) 51 minutes ago                        cranky_haslett
686d4162c34d        00974c980554        "/bin/sh -c 'yum -qq…"   55 minutes ago      Exited (1) 54 minutes ago                          elastic_solomon
bf04a2613883        00974c980554        "/bin/sh -c 'yum -qq…"   About an hour ago   Exited (1) About an hour ago                       nervous_hypatia
ce534c08afce        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       unruffled_feynman
5031361bf654        00974c980554        "/bin/sh -c 'yum ins…"   About an hour ago   Exited (1) About an hour ago                       adoring_roentgen
0a8bba509332        hello-world         "/hello"                 13 days ago         Exited (0) 13 days ago                             peaceful_kowalevski


如果一个容器已经切换到后台,该如何才能重新进入容器呢

规范
#可以使用attach的命令
docker attach <容器id>
实操
[root@VM_0_15_centos ~]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                     PORTS               NAMES
5e87fb906732        nginx                     "/bin/bash"              2 hours ago         Exited (0) 2 minutes ago                       admiring_shamir
1d334f409ffd        86e716b77ca5              "/bin/sh -c 'yum ins…"   2 hours ago         Exited (1) 2 hours ago                         amazing_cartwright
c7c9bb281a16        haoshuliang/centos:test   "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                         thirsty_albattani
1f8b8eb61355        centos                    "/bin/bash"              4 hours ago         Exited (127) 4 hours ago                       optimistic_fermi
6585b34bd420        centos                    "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                         charming_margulis
76d49dc677c0        hello-world               "/hello"                 4 hours ago         Exited (0) 4 hours ago                         hopeful_lichterman
a8b5c077336c        centos                    "/bin/bash"              2 days ago          Exited (127) 2 days ago                        loving_joliot
497dc2195148        centos                    "/bin/bash"              2 days ago          Exited (0) 2 days ago                          goofy_bouman
5ed8f689edf9        hello-world               "/hello"                 5 days ago          Exited (0) 5 days ago                          adoring_edison
4ac14a9991c4        hello-world               "/hello"                 5 days ago          Exited (0) 5 days ago                          sweet_chatterjee
[root@VM_0_15_centos ~]# docker start 5e87fb906732
5e87fb906732
[root@VM_0_15_centos ~]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                     PORTS               NAMES
5e87fb906732        nginx                     "/bin/bash"              2 hours ago         Up 3 seconds               80/tcp              admiring_shamir
1d334f409ffd        86e716b77ca5              "/bin/sh -c 'yum ins…"   2 hours ago         Exited (1) 2 hours ago                         amazing_cartwright
c7c9bb281a16        haoshuliang/centos:test   "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                         thirsty_albattani
1f8b8eb61355        centos                    "/bin/bash"              4 hours ago         Exited (127) 4 hours ago                       optimistic_fermi
6585b34bd420        centos                    "/bin/bash"              4 hours ago         Exited (0) 4 hours ago                         charming_margulis
76d49dc677c0        hello-world               "/hello"                 4 hours ago         Exited (0) 4 hours ago                         hopeful_lichterman
a8b5c077336c        centos                    "/bin/bash"              2 days ago          Exited (127) 2 days ago                        loving_joliot
497dc2195148        centos                    "/bin/bash"              2 days ago          Exited (0) 2 days ago                          goofy_bouman
5ed8f689edf9        hello-world               "/hello"                 5 days ago          Exited (0) 5 days ago                          adoring_edison
4ac14a9991c4        hello-world               "/hello"                 5 days ago          Exited (0) 5 days ago                          sweet_chatterjee
[root@VM_0_15_centos ~]# docker attach 5e87fb906732
root@5e87fb906732:/# hostname 
5e87fb906732
root@5e87fb906732:/# exit
exit
[root@VM_0_15_centos ~]# docker attach 5e87fb906732
You cannot attach to a stopped container, start it first
# 只要以attach进入的容器,则exit退出后容器也就会退出
怎样才能避免上述的问题呢?
ctrl + p
ctr + q
实操
[root@VM_0_15_centos app]# docker attach 69dff30b6322
root@69dff30b6322:/# read escape sequence   
[root@VM_0_15_centos app]# docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                     PORTS               NAMES
69dff30b6322        nginx                "/bin/bash"         13 days ago         Up 20 seconds              80/tcp              boring_brown
8e9313993a0a        kinogmt/centos-ssh   "/bin/bash"         13 days ago         Exited (137) 12 days ago                       youthful_lichterman
6295c6878dcb        kinogmt/centos-ssh   "/bin/bash"         13 days ago         Exited (137) 12 days ago                       musing_meninsky
6a1d463e4346        4cbe7aa905e7         "/bin/bash"         13 days ago         Exited (127) 13 days ago                       cool_curie



补充1 查看指定关键词的镜像

规范: 
docker images <关键词>
实操:
[root@VM_0_15_centos app]# docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              540a289bab6c        5 weeks ago         126MB

补充2 怎样实现在启动容器的时候,当该容器退出的时候需要把这个容器也删除

规范:
docker run -it --rm centos /bin/bash
实操
# 下面是启动一个容器,该容器在退出后也会默认删除
[root@VM_0_15_centos appdeploy]# docker run -it --rm centos /bin/bash 
# 下面默认进入容器的bash 交互界面,得到其容器的id是578ad1ecf59
[root@578ad1ecf594 /]# 
# 执行ctrl+p 和ctrl+q下的后,查看所有的运行的容器状态
[root@VM_0_15_centos appdeploy]# docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                     PORTS                  NAMES
578ad1ecf594        centos               "/bin/bash"         17 seconds ago      Up 16 seconds                                     romantic_dewdney
69dff30b6322        nginx                "/bin/bash"         13 days ago         Up About an hour           80/tcp                 boring_brown
8e9313993a0a        kinogmt/centos-ssh   "/bin/bash"         13 days ago         Up About an hour           0.0.0.0:1234->22/tcp   youthful_lichterman
6295c6878dcb        kinogmt/centos-ssh   "/bin/bash"         13 days ago         Exited (137) 12 days ago                          musing_meninsky
6a1d463e4346        4cbe7aa905e7         "/bin/bash"         13 days ago         Exited (127) 13 days ago                          cool_curie
# 过滤容器id是578ad1ecf59的容器,发现该容器还在,并且状态是运行中
[root@VM_0_15_centos appdeploy]# docker ps -a | grep 578ad1ecf594
578ad1ecf594        centos               "/bin/bash"         27 seconds ago      Up 25 seconds                                     romantic_dewdney
# 然后进入到容器中,并执行exit退出容器
[root@VM_0_15_centos appdeploy]# docker attach 578ad1ecf594
[root@578ad1ecf594 /]# exit
exit
# 查看容器id为578ad1ecf59的容器已经没有了,这就是默认被删除了
[root@VM_0_15_centos appdeploy]# docker ps -a | grep 578ad1ecf594
[root@0f08da061098 /]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

补充3 与docker images 命令一样的命令

规范
docker image ls
实操
[root@VM_0_15_centos appdeploy]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
hsl_centos_with_ssh   v1.0.1              0c4361666100        12 days ago         931MB
haoshuliang/centos    test                ee87dab68e9d        2 weeks ago         250MB
nginx                 latest              540a289bab6c        5 weeks ago         126MB
centos                latest              0f3e07c0138f        2 months ago        220MB
kinogmt/centos-ssh    latest              dc8713dad282        3 years ago         773MB
[root@VM_0_15_centos appdeploy]# docker image ls
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
hsl_centos_with_ssh   v1.0.1              0c4361666100        12 days ago         931MB
haoshuliang/centos    test                ee87dab68e9d        2 weeks ago         250MB
nginx                 latest              540a289bab6c        5 weeks ago         126MB
centos                latest              0f3e07c0138f        2 months ago        220MB
kinogmt/centos-ssh    latest              dc8713dad282        3 years ago         773MB

补充查看镜像、 容器、 数据卷所占用的空间 

规范
# 查看镜像和容器的使用情况
docker system df
# 查看具体的镜像和容器的使用情况
docker sysetm df -v
实操
# 下面是查看镜像和容器的使用情况
[root@VM_0_15_centos appdeploy]# docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              5                   2                   1.307GB             1.181GB (90%)
Containers          4                   2                   315.8MB             157.9MB (50%)
Local Volumes       0                   0                   0B                  0B
Build Cache         0                   0                   0B                  0B
# 下面是查看具体的镜像和容器的使用情况
[root@VM_0_15_centos appdeploy]# docker system df -v
Images space usage:

REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE                SHARED SIZE         UNIQUE SIZE         CONTAINERS
hsl_centos_with_ssh   v1.0.1              0c4361666100        12 days ago         931MB               773.1MB             157.9MB             0
haoshuliang/centos    test                ee87dab68e9d        2 weeks ago         249.5MB             219.6MB             29.93MB             0
nginx                 latest              540a289bab6c        5 weeks ago         126.2MB             0B                  126.2MB             1
centos                latest              0f3e07c0138f        2 months ago        219.6MB             219.6MB             0B                  0
kinogmt/centos-ssh    latest              dc8713dad282        3 years ago         773.1MB             773.1MB             0B                  2

Containers space usage:

CONTAINER ID        IMAGE                COMMAND             LOCAL VOLUMES       SIZE                CREATED             STATUS                     NAMES
69dff30b6322        nginx                "/bin/bash"         0                   377B                13 days ago         Up 2 hours                 boring_brown
8e9313993a0a        kinogmt/centos-ssh   "/bin/bash"         0                   158MB               13 days ago         Up 2 hours                 youthful_lichterman
6295c6878dcb        kinogmt/centos-ssh   "/bin/bash"         0                   158MB               13 days ago         Exited (137) 12 days ago   musing_meninsky
6a1d463e4346        4cbe7aa905e7         "/bin/bash"         0                   2.55kB              13 days ago         Exited (127) 13 days ago   cool_curie

Local Volumes space usage:

VOLUME NAME         LINKS               SIZE

Build cache usage: 0B

CACHE ID            CACHE TYPE          SIZE                CREATED             LAST USED           USAGE               SHARED

补充5 怎样清理docker 的空间

规范:
docker system prune
实操
#下面是清理之前空间的使用情况
[root@VM_0_15_centos appdeploy]# docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              5                   2                   1.307GB             1.181GB (90%)
Containers          4                   2                   315.8MB             157.9MB (50%)
Local Volumes       0                   0                   0B                  0B
Build Cache         0                   0                   0B                  0B
# 下面是清理空间的命令
[root@VM_0_15_centos appdeploy]# docker system prune
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y
Deleted Containers:
6295c6878dcb1f39ff16accd7e9de4ef7603b6648a3831234c015ed758079eab
6a1d463e4346156667f8bcea2a9acee9d61e2204e0b5118f27719860d88cd18c

Total reclaimed space: 157.9MB
#下面是清理后的空间显示情况,相比于清理之前发现还是有变化的
[root@VM_0_15_centos appdeploy]# docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              5                   2                   1.307GB             1.181GB (90%)
Containers          2                   2                   157.9MB             0B (0%)
Local Volumes       0                   0                   0B                  0B
Build Cache         0                   0                   0B                  0B
note:该指令默认会清除所有如下资源:
  • 已停止的容器(container)
  • 未被任何容器所使用的卷(volume)
  • 未被任何容器所关联的网络(network)
  • 所有悬空镜像(image)。

补充6 怎样清理没有用的镜像

规范
docker image prune
实操
[root@VM_0_15_centos appdeploy]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
# 这里是需要清理没有用的镜像,但是由于我的机器上没有这种镜像的,所以在这里的演示没有任何的效果
note: 这里prune的意思是修剪的意思,顾名思义就是要把没用的数据进行“修剪”掉

补充7 虚悬镜像(dangling image)

概念: 在我们在使用docker images 或者dokcer image ls 查看镜像的时候会有显示仓库名和标签都有<None>的,我们把这样的镜像称之为虚悬镜像,英文是dangling image,这里dangling就是这个意思了

Q1: 怎样查看虚悬镜像呢?

实操,ps:但是由于我的本地环境没有虚悬镜像,所以下面的演示也就没有什么参考价值,如果遇见虚悬镜像的话,我再进行补充
[root@VM_0_15_centos appdeploy]# docker images dangling=true
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@VM_0_15_centos appdeploy]# docker image ls dangling=true
REPOSITORY          TAG                 IMAGE ID            CREATED 

Q2: 虚悬镜像是什么原因造成的呢?

原因1:曾经执行过docker pull的命令,这个命令会对镜像进行更新,新的镜像名字和tag与旧的镜像名字和tag是一样的,新的镜像肯定会把旧的名字和tag占用,这就不得不造成旧的image的名字和tage为<None>
原因 2:曾经执行过docker build 命令,在build的过程中,可能会存在对旧镜像的更新,这样会和docker pull一样的效果
结果:这样会造成docker 的旧的镜像也就是虚悬镜像没有任何价值,可以使用docker image prune进行删除

补充8 怎样查看中间层的镜像

规范
docker image ls -a
docker images -a
实操
[root@VM_0_15_centos appdeploy]# docker images -a
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
hsl_centos_with_ssh   v1.0.1              0c4361666100        12 days ago         931MB
haoshuliang/centos    test                ee87dab68e9d        2 weeks ago         250MB
nginx                 latest              540a289bab6c        5 weeks ago         126MB
centos                latest              0f3e07c0138f        2 months ago        220MB
kinogmt/centos-ssh    latest              dc8713dad282        3 years ago         773MB
[root@VM_0_15_centos appdeploy]# docker image ls -a
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
hsl_centos_with_ssh   v1.0.1              0c4361666100        12 days ago         931MB
haoshuliang/centos    test                ee87dab68e9d        2 weeks ago         250MB
nginx                 latest              540a289bab6c        5 weeks ago         126MB
centos                latest              0f3e07c0138f        2 months ago        220MB
kinogmt/centos-ssh    latest              dc8713dad282        3 years ago         773MB

补充9 docker ps -a 与docker container ls --all的区别


补充10 已经有个镜像在以保护态度进行运行,怎样进入终端输入命令呢?

思路一: 可以首先进入到容器中,然后在进行输入命令

实操
[root@VM_0_15_centos dockerfile]# docker ps -a 
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                  NAMES
69dff30b6322        nginx                "/bin/bash"         13 days ago         Up 3 hours          80/tcp                 boring_brown
8e9313993a0a        kinogmt/centos-ssh   "/bin/bash"         13 days ago         Up 2 hours          0.0.0.0:1234->22/tcp   youthful_lichterman
[root@VM_0_15_centos dockerfile]# docker attach 69df
root@69dff30b6322:/# cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

思路二:执行docker exec的命令

规范 
docker exec -it <container-id | contain-name> /bin/bash
实操
[root@VM_0_15_centos appdeploy]# docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                  NAMES
aa05dabe6c67        ubuntu               "/bin/bash"         43 minutes ago      Up 43 minutes                              romantic_matsumoto
69dff30b6322        nginx                "/bin/bash"         13 days ago         Up 3 hours          80/tcp                 boring_brown
8e9313993a0a        kinogmt/centos-ssh   "/bin/bash"         13 days ago         Up 3 hours          0.0.0.0:1234->22/tcp   youthful_lichterman
[root@VM_0_15_centos appdeploy]# docker exec -it aa05 /bin/bash
root@aa05dabe6c67:/# cat /etc/os-release 
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

补充11 仓库的相关操作

[root@VM_0_15_centos appdeploy]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: jasonhaoshuliang
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@VM_0_15_centos appdeploy]# docker logout
Removing login credentials for https://index.docker.io/v1/
[root@VM_0_15_centos appdeploy]# echo $?
0

补充12 怎样解决docker机器和宿主机时间不同步的问题


方法1:复制主机的localtime
 docker cp /etc/localtime <container-id>:/etc/
实操:
[jasonhao@cvm-172_16_30_7 ~]$ docker cp /etc/localtime 7142e502f1ae:/etc/
[jasonhao@cvm-172_16_30_7 ~]$ docker attach 714
[root@7142e502f1ae tapdbase]# date  
Tue Dec 10 10:42:23 CST 2019
方法2:在创建container时就应该共享主机的时间
docker run -ti -d --name my-nginx -v /etc/localtime:/etc/localtime:ro  docker.io/nginx  /bin/bash
待实操
方法3:创建镜像的时候就应该设置时区
Dockerfile的文件如下:
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone






猜你喜欢

转载自www.cnblogs.com/jasonhao/p/12361686.html