《Docker技术革命:从虚拟机到容器化,全面解析Docker的原理与应用-上篇》(二)

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云解析 DNS,旗舰版 1个月
简介: 《Docker技术革命:从虚拟机到容器化,全面解析Docker的原理与应用-上篇》(二)

《Docker技术革命:从虚拟机到容器化,全面解析Docker的原理与应用-上篇》(一)+https://developer.aliyun.com/article/1625018

底层原理

Docker是怎么工作的?

Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问!

DockerServer接收到的Docker-Client的指令

Docker为什么比虚拟机VM快?
  1. Docker有着比虚拟机更少的抽象层。
  2. Docker利用的是宿主机的内核,VM需要是Guest OS

总结

新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导,虚拟机是加载Guest OS,分钟级别的,而Docker是利用宿主机的操作系统,省略了这个复杂的过程,属于秒级!

Docker的常用命令

帮助命令

[root@linghuNodeMaster /]# docker version
Client: Docker Engine - Community
 Version:           25.0.1
 API version:       1.44
 Go version:        go1.21.6
 Git commit:        29cf629
 Built:             Tue Jan 23 23:12:51 2024
 OS/Arch:           linux/amd64
 Context:           default
Server: Docker Engine - Community
 Engine:
  Version:          25.0.1
  API version:      1.44 (minimum version 1.24)
  Go version:       go1.21.6
  Git commit:       71fa3ab
  Built:            Tue Jan 23 23:11:50 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.27
  GitCommit:        a1496014c916f9e62104b33d1bb5bd03b0858e59
 runc:
  Version:          1.1.11
  GitCommit:        v1.1.11-0-g4bccb38
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@linghuNodeMaster /]# docker info
Client: Docker Engine - Community
 Version:    25.0.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.12.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.24.2
    Path:     /usr/libexec/docker/cli-plugins/docker-compose
Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 1
 Server Version: 25.0.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: a1496014c916f9e62104b33d1bb5bd03b0858e59
 runc version: v1.1.11-0-g4bccb38
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
 Kernel Version: 3.10.0-957.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.777GiB
 Name: linghuNodeMaster
 ID: 55e66820-5e29-47c4-86f7-f6ebd29be070
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://kjrexsxz.mirror.aliyuncs.com/
 Live Restore Enabled: false
[root@linghuNodeMaster /]# docker --help
Usage:  docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information
Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.12.1)
  compose*    Docker Compose (Docker Inc., v2.24.2)
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes
Swarm Commands:
  swarm       Manage Swarm
Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes
Global Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default
                           context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket to connect to
  -l, --log-level string   Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit
Run 'docker COMMAND --help' for more information on a command.
For more help on how to use Docker, head to https://docs.docker.com/go/guides/
[root@linghuNodeMaster /]#

镜像命令

[root@linghuNodeMaster /]# docker images --help
Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
  docker image ls, docker image list, docker images
Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format output using a custom template:
                        'table':            Print output in table format with column headers (default)
                        'table TEMPLATE':   Print output in table format using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go template.
                        Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with
                        templates
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs
[root@linghuNodeMaster /]#
查看镜像

docker images

[root@linghuNodeMaster /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d2c94e258dcb   8 months ago   13.3kB
[root@linghuNodeMaster /]#

下载镜像

docker pull mysql

安装mysql的镜像:

删除镜像

docker rmi -f d2c94e258dcb

容器命令

我们有了镜像才可以创建容器,Linux,下载一个centos镜像来测试学习。

docker pull centos

[root@linghuNodeMaster /]# docker run -it centos /bin/bash
[root@0d8907f0b945 /]# 
#从容器中退回主机,容器停止运行
[root@0d8907f0b945 /]# exit
exit
[root@linghuNodeMaster /]#
退出容器

#从容器中退回主机,容器停止运行

[root@0d8907f0b945 /]# exit

exi

删除容器

docker rm 容器id #不能删除正在运行的容器

docker rm $(docker ps -aq) #可以删除正在运行的容器

启动和停止容器
docker start 容器id  # 启动客器
docker restart 容器id # 重启容器
docker stop 容器id ##停止当前正在运行的容器
docker ki11 容器id #强制停止当前容器
[root@linghuNodeMaster /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
0d8907f0b945   centos         "/bin/bash"   19 minutes ago   Exited (0) 16 minutes ago             priceless_jackson
8ff48633ed72   d2c94e258dcb   "/hello"      18 hours ago     Exited (0) 18 hours ago               heuristic_mestorf
[root@linghuNodeMaster /]# docker start 0d8907f0b945 
0d8907f0b945
[root@linghuNodeMaster /]# docker stop 0d8907f0b945 
0d8907f0b945
[root@linghuNodeMaster /]#
常用其他命令
后台启动容器
# 命令 docker run -d 镜像名
[root@iZ2zeg4ytp0whqtmxbsqiiZ /]# docker run -d centos
 
# 问题 docker ps, 发现centos停止了
 
# 常见的坑, docker 容器使用后台运行, 就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx, 容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了
查看容器中的进程信息
# 命令 docker top 容器id
 docker top 3218b38490ce
查看镜像中的元数据
# 命令
docker inspect 容器id
[root@linghuNodeMaster /]# docker inspect 5d0da3dc9764
[
    {
        "Id": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "RepoTags": [
            "centos:latest"
        ],
        "RepoDigests": [
            "centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-15T18:20:05.184694267Z",
        "Container": "9bf8a9e2ddff4c0d76a587c40239679f29c863a967f23abf7a5babb6c2121bf1",
        "ContainerConfig": {
            "Hostname": "9bf8a9e2ddff",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 231268856,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/d2748d432c8cb6a180520aaa48050020a595787b70b7f3d8792051603590c611/merged",
                "UpperDir": "/var/lib/docker/overlay2/d2748d432c8cb6a180520aaa48050020a595787b70b7f3d8792051603590c611/diff",
                "WorkDir": "/var/lib/docker/overlay2/d2748d432c8cb6a180520aaa48050020a595787b70b7f3d8792051603590c611/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
[root@linghuNodeMaster /]#
进入当前正在运行的容器
#我们通常容器都是后台方式进行的,需要进入容器,修改一些配置
   
#命令
docker exec -it 容器id /bin/bash
# 方式二
docker attach 容器id
 
# docker exec       # 进入容器后开启一个新的终端,可以在里面操作
# docker attach     # 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝到主机上
#1.命令
docker cp 容器id:容器内路径(文件名)  主机目的路径
#2.测试
docker cp 0694e2e1032c:/hello.java /home 
#将容器内部的/hello.java移动到主机的/home目录下,即使容器关闭了,也可以拷贝出来
#拷贝是一个手动过程,未来我们使用-v卷的技术,可以实现自动同步
总结

attach      Attach local standard input, output, and error streams to a running container
#当前shell下 attach连接指定运行的镜像
build       Build an image from a Dockerfile # 通过Dockerfile定制镜像
commit      Create a new image from a container's changes     #提交当前容器为新的镜像
cp          Copy files/folders between a container and the local filesystem #拷贝文件
create      Create a new container #创建一个新的容器
diff        Inspect changes to files or directories on a container's filesystem 
#查看docker容器的变化
events      Get real time events from the server # 从服务获取容器实时时间
exec        Run a command in a running container # 在运行中的容器上运行命令
export      Export a container's filesystem as a tar archive 
#导出容器文件系统作为一个tar归档文件[对应import]
history     Show the history of an image # 展示一个镜像形成历史
images      List images #列出系统当前的镜像
import      Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
info        Display system-wide information # 显示全系统信息
inspect     Return low-level information on Docker objects #查看容器详细信息
kill        Kill one or more running containers # kill指定docker容器
load        Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
login       Log in to a Docker registry #
logout      Log out from a Docker registry
logs        Fetch the logs of a container
pause       Pause all processes within one or more containers
port        List port mappings or a specific mapping for the container
ps          List containers
pull        Pull an image or a repository from a registry
push        Push an image or a repository to a registry
rename      Rename a container
restart     Restart one or more containers
rm          Remove one or more containers
rmi         Remove one or more images
run         Run a command in a new container
save        Save one or more images to a tar archive (streamed to STDOUT by default)
search      Search the Docker Hub for images
start       Start one or more stopped containers
stats       Display a live stream of container(s) resource usage statistics
stop        Stop one or more running containers
tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top         Display the running processes of a container
unpause     Unpause all processes within one or more containers
update      Update configuration of one or more containers
version     Show the Docker version information
wait        Block until one or more containers stop, then print their exit codes
Docker安装Nginx

安装过程:

[root@linghuNodeMaster /]# docker pull nginx #下载拉取nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
root@linghuNodeMaster /]# docker images 查看docker的镜像文件
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    605c77e624dd   2 years ago   141MB
mysql        latest    3218b38490ce   2 years ago   516MB
centos       latest    5d0da3dc9764   2 years ago   231MB
[root@linghuNodeMaster /]# 
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口:
[root@linghuNodeMaster /]# docker run -d --name nginx01 -p 3344:80 nginx
e8868b054eb4b0fff97adb6b19dc9b6fb83d513b87176290e71d1199e01ded2d
[root@linghuNodeMaster /]# docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED          STATUS          PORTS                                   NAMES
e8868b054eb4   nginx     "/docker-entrypoint.…"   57 seconds ago   Up 50 seconds   0.0.0.0:3344->80/tcp, :::3344->80/tcp   nginx01
[root@linghuNodeMaster /]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@linghuNodeMaster /]#

端口暴露的概念:

外网也可以访问Nginx:

思考问题:我们每次改动nginx配置文件,都要进入容器内部?十分的麻烦,我要是可以在容器外部提供一个映射路径,达到在容器修改文件名,容器内部可以自动修改?

Docker安装Tomcat
# 下载 tomcat9.0
# 命令:docker run -it --rm 镜像名,注意看命令中带有了--rm,表示用完就删除
# 之前的启动都是后台,停止了容器,容器还是可以查到还存在,一般是用来测试,用完就删除
#1. 下载tomcat最新版
docker pull tomcat
#2. 查看下载的镜像
docker images
#3. 以后台方式,暴露端口方式,启动运行
docker run -d -p 3355:8080 --name tomcat02 tomcat
#4. 测试访问有没有问题
curl localhost:3355 #测试没问题,但是外网访问的话是404,这个问题需要下面的第七步解决
#5. 根据容器id进入tomcat容器
docker exec -it 1c9d3fc1dac5 /bin/bash
#6. 其实这个tomcat是阉割版
# 发现问题:1、linux命令少了。 2.webapps目录为空 
# 原因:阿里云镜像的原因,阿里云默认是最小的镜像,所以不必要的都剔除掉
# 保证最小可运行的环境!
# 解决方案:
# 将webapps.dist下的文件都拷贝到webapps下即可
#7. 将webapps.dist下的文件都拷贝到webapps下
#进入容器
[root@iZ2vc28obhvfham8wewhh0Z ~]# docker exec -it 1c9d3fc1dac5 /bin/bash
#查看文件目录,含有webapps.dist
root@1c9d3fc1dac5:/usr/local/tomcat# ls
BUILDING.txt   LICENSE  README.md  RUNNING.txt  conf  logs      temp     webapps.dist
CONTRIBUTING.md  NOTICE   RELEASE-NOTES  bin        lib   native-jni-lib  webapps  work
#将webapps.dist所有东西复制到webapps文件夹中
root@1c9d3fc1dac5:/usr/local/tomcat# cp -r webapps.dist/* webapps

Docker镜像原理讲解

镜像是什么

  • 镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码、运行时库、环境变量和配置文件

所有应用,直接打包docker镜像,就可以直接跑起来

如何得到镜像

  • 从远程仓库下载
  • 别人拷贝给你
  • 自己制作一个镜像 DockerFile

Docker镜像加载原理

UnionFs (联合文件系统)

Docker镜像加载原理

  • 第一阶段:只有bootfs和rootfs
  • 第二阶段:bootfs和rootfs部分加上了images
  • 第三阶段:bootfs和rootfs部分加上了images,再加上images…无限加层,千层饼

分层理解

查看分层

当我们下载一个镜像的时候,观察输出日志,可以看到在一层层的下载:

为什么Docker要采用这种分层结构?

最大的好处,我觉得莫过于资源共享了!比如有多个镜像都从相同的Base镜像构建而来,那么宿主机只需在磁盘上保留一份base镜像,同时内存中也只需要加载一份base镜像,这样就可以为所有的容器服务了,而且镜像的每一层都可以被共享。

使用docker image inspect tomcat,查看一下分层,主要看一下layers:

[root@iZ2vc28obhvfham8wewhh0Z ~]# docker image inspect tomcat
[...
        "RootFS": {
            "Type": "layers",
            "Layers": [#以下的就相当于是一个个的记录,有10条,对应上方的10条pull complete
                "sha256:688e187d6c79c46e8261890f0010fd5d178b8faa178959b0b46b2635aa1eeff3",
                "sha256:00bcea93703b384ab61d2344d6e721c75b0e47198b7912136a86759d8d885711",
                "sha256:ccb9b68523fdb47816f2a15d7590d06ff3d897dfdb3e0733e9c06a2bb79ebfc7",
                "sha256:685934357c8993799eda4a6d71c2bb2d2c54b1138d6e037f675eeeeffc719f2d",
                "sha256:3fc095fab4a2bec54616a8f5c20c43a9fe197ad06c2cf81385578dfe79aed238",
                "sha256:6f770cdc9ebf757c2f2f6395c3cdb6f4298f5860727de340890a9a855e168e6e",
                "sha256:15786a1cf1cbf50fea509d04227d327416c7c0c8b42b9488b56416095ba2f434",
                "sha256:8f8b5acac684a1fb664c6301413fd28d50ac0f28c7fb8a01c24eee6cd4799739",
                "sha256:3307ffa538c1bbc8f7d1cf8a5f0fbcd08634a7001dbf92c619e7720fb334df70",
                "sha256:daf63ef0ddbb5fd50852b4bfc2f5f9fd0be4923819608d4f6051fc23809985c9"
            ]
        }...
]
深入理解

所有Docker镜像都起始于一个基础镜像层,当进行修改或加新的内容时,就会在当前镜像层之上创建新的镜像层。

假如基于 Ubuntu Linux16.04创建一个新的镜像,这就是新镜像的第一层;如果在该镜像中添加 Python包,就会在基础镜像层之上创建第二个镜像层;如果继续添加一个安全补丁,就会创健第三个镜像层该像当前已经包含3个镜像层,如下图所示(这只是一个用于演示的很简单的例子)。

在添加额外的镜像层的同时,镜像始终保持是当前所有镜像的组合,理解这一点

添加额外的镜像层的同时,镜像始终保持是当前所有镜像的组合,理解这一点非常重要。下图中举了一个简单的例子,每个镜像层包含3个文件,而将这两个层进行打包之后,镜像包含了来自两个镜像层的6个文件。

这种情況下,上层镜像层中的文件覆盖了底层镜像层中的文件。这样就使得文件的更新版本作为一个新镜像层添加到镜像当中。

  • Docker通过存储引擎(新版本采用快照机制)的方式来实现镜像层堆栈,并保证多镜像层对外展示为统一的文件系统。
  • Linux上可用的存储引撃有AUFS、 Overlay2、 Device Mapper、Btrfs以及ZFS。顾名思义,每种存储引擎都基于 Linux中对应的文件系统或者块设备技术,井且每种存储引擎都有其独有的性能特点。
  • Docker在 Windows上仅支持 windowsfilter 一种存储引擎,该引擎基于NTFS文件系统之上实现了分层和CoW 。

下图展示了与系统显示相同的三层镜像。所有镜像层堆并合井,对外提供统一的视图。

提交自己的镜像-commit

语法
docker commit 提交容器成为一个新的副本
# 命令和git原理类似
docker commit -m="描述信息" -a="作者名字" 容器id 目标镜像名:[版本TAG]
#1. 启动一个默认的tomcat
[root@iZ2vc28obhvfham8wewhh0Z ~]# docker run -it -p 8080:8080 tomcat
#2. 发现这个默认的tomcat 是没有webapps应用,官方的镜像默认webapps下面是没有文件的!
[root@iZ2vc28obhvfham8wewhh0Z ~]# docker exec -it 079f8174730f /bin/bash
#3. 将webapps.dist里的所有东西拷贝文件进webapps,并查看
root@079f8174730f:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@079f8174730f:/usr/local/tomcat# cd webapps
root@079f8174730f:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager
#4. 操作过的容器通过commit调教为一个镜像!我们以后就使用我们修改过的镜像即可,而不需要每次都重新拷贝webapps.dist下的文件到webapps了,这就是我们自己的一个修改的镜像。
docker commit -m="描述信息" -a="作者" 容器id 目标镜像名:[TAG]
docker commit -a="peng" -m="add webapps app" 容器id tomcat02:1.0
[root@iZ2vc28obhvfham8wewhh0Z ~]# docker commit -a="haha" -m="add webapps app" 079f8174730f tomcat02:1.0
sha256:b0a602f7e277d044ec71dbc36450609a0652f316e06c51fdcc82338de792793e
[root@iZ2vc28obhvfham8wewhh0Z ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED              SIZE
#看下方这个tomcat02.1.0就是我们创建的镜像,发布之后再说
tomcat02.1.0          1       b0a602f7e277   About a minute ago   672MB
tomcat                9         6654503f1940   7 hours ago          667MB
nginx                 latest    d1a364dc548d   4 weeks ago          133MB
tomcat                latest    c43a65faae57   5 weeks ago          667MB
mysql                 latest    c0cdc95609f1   6 weeks ago          556MB
portainer/portainer   latest    580c0e4e98b0   3 months ago         79.1MB
hello-world           latest    d1165f221234   3 months ago         13.3kB
centos                latest    300e315adb2f   6 months ago         209MB
elasticsearch         7.6.2     f29a1ee41030   15 months ago        791MB

=以上为基础部分,Docker高级应用内容后续更======

目录
相关文章
|
1天前
|
运维 Java 程序员
《Docker技术革命:从虚拟机到容器化,全面解析Docker的原理与应用-上篇》(一)
《Docker技术革命:从虚拟机到容器化,全面解析Docker的原理与应用-上篇》(一)
7 0
|
20天前
|
Kubernetes Linux 开发者
深入探索Docker容器化技术的奥秘
深入探索Docker容器化技术的奥秘
18 1
|
5月前
|
Cloud Native Linux 开发者
【Docker】Docker:解析容器化技术的利器与在Linux中的关键作用
【Docker】Docker:解析容器化技术的利器与在Linux中的关键作用
|
2月前
|
Linux iOS开发 Docker
Docker技术概论(2):Docker环境的搭建
Docker技术概论(2):Docker环境的搭建
78 17
|
5月前
|
存储 Linux 调度
Ubantu docker学习笔记(五)容器底层技术
Ubantu docker学习笔记(五)容器底层技术
|
存储 持续交付 虚拟化
Docker 基础知识解析:容器与虚拟化的区别与优势
Docker 基础知识解析:容器与虚拟化的区别与优势
578 0
|
5月前
|
运维 Java 云计算
深入浅出:使用Docker容器化改进Java应用部署
在当前快速演变的软件开发领域,Docker作为一种开源的容器化技术,已经成为优化应用部署、实现快速交付和高效率运维的关键工具。本文将探讨如何利用Docker容器化技术来改进Java应用的部署流程。我们不仅会介绍Docker的基础知识,还会通过一个实际的Java应用示例,详细展示从创建Dockerfile到构建镜像,再到运行容器的整个过程。此外,文章还将探讨容器化带来的好处,如环境一致性、便捷的版本控制和简化的部署流程等,力求为读者提供一个清晰、易懂的指南,帮助他们在自己的项目中实现Docker容器化,从而提升开发和部署效率。
207 1
|
5月前
|
存储 运维 Linux
探索容器化技术:Docker的原理与应用
在当今快节奏的软件开发环境中,容器化技术成为了一种重要的工具,其中最为流行和广泛使用的是Docker。本文将深入探讨Docker的原理和使用,包括容器化概念、镜像管理、容器编排等内容,帮助读者更好地理解和应用这项技术。
307 0
|
5月前
|
存储 Linux 开发者
探索容器化技术:揭秘Docker的原理与应用
在当今快节奏的软件开发环境中,容器化技术成为了关键的工具。本文将深入探索Docker作为一种常见的容器化解决方案的原理与使用方法,并讨论其在现代应用开发中的重要性与优势。
|
5月前
|
运维 持续交付 虚拟化
一文详解Docker与虚拟机的不同
一文详解Docker与虚拟机的不同