Docker架构
Client
docker命令
info子命令查看Docker服务器的信息,如:docker -H 192.168.56.102 info
查看本地镜像:docker images
显示正在运行的容器:docker ps / docker container ls
启动容器:docker run -it centos
镜像构建历史:docker history ubuntu-with-vi-dockfile
删除Host上的镜像:docker rmi debian:latest //只有当最后一个tag被删除时,镜像才被真正删除
搜索Docker Hub中的镜像:docker search httpd
Docker daemon
Image
只读,用来创建Container。
docker commit构建镜像:
docker run -it ubuntu //运行容器,-it 以交互模式进入容器,并打开终端
apt-get install -y vim //安装vi
docker ps //查看容器名称
docker commit silly_goldberg ubuntu-with-vi //保存为镜像
docker images //查看镜像属性
Dockerfile构建镜像:
可以用Dockerfile描述镜像内容和创建步骤。
FROM ubuntu
RUN apt-get update && apt-get install -y vim
构建镜像:docker build <docker-file>
docker build -t ubuntu-with-vi-dockfile . //将新镜像命名为ubuntu-with-vi-dockfile,build context为当前目录
--no-cache不使用缓存
构建过程:
1、将build context中所有文件发送给Docker daemon
2、执行FROM
3、执行RUN:启动临时容器,安装vim,保存为新镜像,删除临时容器
下层镜像改变会导致上层镜像缓存全部失效。
最小镜像
FROM scratch //从0开始构建
COPY hello / //将文件hello复制到镜像根目录
CMD ["/hello"] //执行/hello
base镜像
FROM scratch
ADD centos-7-docker.tar.xz /
CMD ["/bin/bash"]
分层构建镜像
FROM debian
RUN apt-get install emacs
RUN apt-get install apache2
CMD ["/bin/bash"]
调试Dockerfile
1、从base镜像运行一个容器;
2、执行一条指令,对容器做修改;
3、执行类似commit操作,生成一个新的镜像层;
4、再基于新的镜像层,运行一个容器;
5、重复2-4步,直到Dockerfile中所有指令执行完毕。
调试方法:运行最后一次成功构建的镜像层,执行指令。
镜像命名:
REPOSITORY + TAG
docker build -t ubuntu-with-vi:latest
多个 tag 可以对应同一个镜像,如:
docker tag myimage-v1.9.1 myimage:1
docker tag myimage-v1.9.1 myimage:1.9
docker tag myimage-v1.9.1 myimage:1.9.1
docker tag myimage-v1.9.1 myimage:latest
==>
docker tag myimage-v1.9.2 myimage:1
docker tag myimage-v1.9.2 myimage:1.9
docker tag myimage-v1.9.2 myimage:1.9.2
docker tag myimage-v1.9.2 myimage:latest
==>
docker tag myimage-v2.0.0 myimage:2
docker tag myimage-v2.0.0 myimage:2.0
docker tag myimage-v2.0.0 myimage:2.0.0
docker tag myimage-v2.0.0 myimage:latest
Container
可写,镜像的实例。
1、添加文件:文件被添加到容器层;
2、读取文件:从上往下依次在各镜像层中查找文件;
3、修改文件:从上往下依次在各镜像层中查找文件,找到后,复制到容器层修改;
4、删除文件:从上往下依次在各镜像层中查找文件,找到后,在容器层中记录下删除操作;
Registry
下载镜像:docker pull hello-world
下载镜像并启动容器:docker run hello-world
组件协作:
docker run -d -p 80:80 httpd
1、执行run,下载镜像并启动容器;
2、Docker daemon发现本地没有httpd镜像;
3、daemon从Docker Hub下载镜像;
4、下载完成,镜像httpd被保存到本地;
5、daemon启动容器。

浙公网安备 33010602011771号