对于互联网公司的产品研发,通常有两个环境,一个是开发环境,一个是上线环境。
产品从研发到上线,两个环境的应用配置要对齐,这个时候容易出现这样的问题:
这段代码在我的电脑上能运行,在你的电脑上不能运行了?这个时候开发和运维就会因为这个问题陷入焦灼。
后端程序员需要掌握:
这个时候开发运维一体化了,所以要学习Docker了,要去把运维的工作也干了!运维的工作难度,复杂度主要体现在环境配置这个方面,那我们这帮程序员要去卷运维该怎么卷?
环境的配置十分繁琐,每一个机器都要部署环境(集群Redis、ES、Hadoop)耗时费力。
过去程序员发布一个jar包出来,运维工程师就去配置环境,把这个jar包跑在服务器上。现在的情况变成了:程序员发布jar包的时候,把环境也带上了,把项目+环境一起打包。
传统:开发jar包,运维来做。
现在:开发打包部署上线,一套流程做完。
Docker 的思想来源于集装箱!Docker的核心思想是: 隔离
打包箱子,每个箱子是互相隔离的!
Docker可以利用隔离机制将服务器利用到极致。
Docker出现之前还是虚拟机的天下,虚拟机技术就是我们在VM中用Linux编程搞环境那套东西。但是后来出现了容器技术,这个容器技术就是我们今天谈的Docker!
Docker 的出现是为了解决虚拟机技术带来的笨重!
虚拟机用的是虚拟化技术,虚拟软件、硬件需要的环境。Docker用的是容器技术,但这种容器技术也是虚拟化技术,只是一种轻量级的虚拟化技术罢了。
传统的虚拟机搞隔离要开很多个虚拟机才能实现分布式集群。这是一种非常笨重的方法。
Docker用了一个叫镜像的东西,把应用需要的环境,比如jdk+mysql等等环境安装打包到镜像里面,你直接运行镜像即可,非常快,非常小巧方便。
之前的虚拟机技术:
现在的容器化技术:
容器化技术不是模拟一个完整的操作系统。
比较docker和虚拟机技术的不同
DevOps(开发、运维)
应用更快速的交付和部署
传统:
Docker:
更便捷的升级和扩容
使用了Docker之后,我们部署应用就像搭积木一样!
项目打包为一个镜像,扩展服务器A!B!
更简单的系统运维
在容器简化之后,我们的开发,测试环境都是高度一致的。
更高效的计算资源利用
Docker是内核级别的虚拟化,可以再一个物理机上可以运行很多容器实例!服务器的性能可以被利用到极致!
Docker镜像好比是一个模板,可以通过这个模板来创建容器服务,Tomcat镜像===>run===>Tomcat01容器(提供服务),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。
Docker利用容器技术,独立运行一个或者一组应用,通过镜像来创建的。
启动、停止、删除、基本命令!
目前就可以把容器理解成一个简易的Linux系统
仓库就是存放镜像的地方!仓库分为公有仓库和私有仓库!
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
sudo yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
$ sudo systemctl start docker
docker version
docker run hello-world
上面的执行Helloworld
流程如下:
docker images
用自己的手机号进行注册即可。
这里选择CentOS!
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://kjrexsxz.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问!
DockerServer接收到的Docker-Client的指令
新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导,虚拟机是加载Guest OS,分钟级别的,而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
安装过程:
[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配置文件,都要进入容器内部?十分的麻烦,我要是可以在容器外部提供一个映射路径,达到在容器修改文件名,容器内部可以自动修改?
# 下载 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镜像,就可以直接跑起来
当我们下载一个镜像的时候,观察输出日志,可以看到在一层层的下载:
最大的好处,我觉得莫过于资源共享了!比如有多个镜像都从相同的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 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