容器数据卷
什么是容器数据卷
将应用 和环境打包成一个镜像!
如果数据都在容器中,容器一删除数据就会丢失。需求:数据持久化
容器之间可以用一个数据共享的技术!Docker 容器中产生的数据,同步到本地。
这就是卷技术。目录的挂载,将我们容器和目录挂载到linux上。
使用数据卷
方式1:使用命令挂载
docker run -it -v 主机目录:容器目录
docker run -it -v /home/ceshi:/home centos
docker inspect 3f63a8295313
“Mounts”: [
{
“Type”: “bind”,
“Source”: “/home/ceshi”,
“Destination”: “/home”,
“Mode”: “”,
“RW”: true,
“Propagation”: “rprivate”
}
双向过程,在主机和容器中操作都会同步
安装MySQL
#安装mysql
docker pull mysql:5.7
#挂载目录并启动
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag#官方启动
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/date:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw --name mysql01 mysql:5.7
[root@localhost home]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/date:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw --name mysql01 mysql:5.7
3280dfb68fa34ab6960f1484915fed2ad7506cddb3c4f942eb8cc9065fe27aef
[root@localhost home]#
#启动成果之后,启动连接工具,测试连接
#sqlyog--连接服务器的3310 --3310 和容器的3306映射
#删除容器后,主机还有相关的容器文件
具名挂载和匿名挂载
#匿名挂载 只写容器路径没有写容器外的路径
-v 容器路径
docker run -d --name nginx02 -v /etc/nginx nginx
#查看所有vlume 情况
[root@localhost home]# docker volume ls
local aff897c9ee4e856037c1533a654216cae1b9546ceaa637bb744c06f78c360e40
#具名挂载 默认情况下挂载到 /var/lib/docker/volumes
[root@localhost home]# docker run -d --name nginx03 -v juming-nginx:/etc/nginx nginx
91cf74d594971245746be1255e2e9866bd4e560dcb0a52c3694417b2a8a27066
#查看卷所在文位置
[root@localhost home]# docker volume inspect juming-nginx
[
{
"CreatedAt": "2022-04-06T06:38:51+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
[root@localhost home]#
#默认
[root@localhost volumes]# ll
total 28
drwx-----x. 3 root root 19 Apr 6 04:10 2c2954005b249d44268cf5464a968fa8096a3a
drwx-----x. 3 root root 19 Apr 6 06:01 3c2990d4db6d29d2e37accc18ce0d9dd4de7d758a46
drwx-----x. 3 root root 19 Apr 6 03:40 9de28c9355839612f26e2d1b1936d3c47388d01e1f
drwx-----x. 3 root root 19 Apr 6 04:35 89b7bcaf848d4cb18baeea0c60bb391d72cab03d3b
drwx-----x. 3 root root 19 Apr 6 06:35 33a654216cae1b9546ceaa637bb744c06f78c360e40
brw-------. 1 root root 253, 0 Apr 6 00:40 backingFsBlockDev
drwx-----x. 3 root root 19 Apr 6 06:38 juming-nginx
-rw-------. 1 root root 32768 Apr 6 06:38 metadata.db
[root@localhost volumes]# pwd
/var/lib/docker/volumes
#如何确定具名、匿名、还是指定路径
-v 容器路径 #匿名挂载
-v 卷名:容器路径 #具名挂载
-v /主机路径:/容器路径 #指定路径挂载
拓展
#ro、rw 容器内部的读写权限
docker run -d --name nginx03 -v juming-nginx:/etc/nginx:ro nginx
docker run -d --name nginx03 -v juming-nginx:/etc/nginx:rw nginx
初识Dockerfile
Dockerfile 是用来构建docker镜像的构建文件!命令脚本!
通过脚本可以生成镜像,镜像是一层一层的,脚本一个一个命令,每个 命令都是一层
#创建一个dockerfile文件 任意名字即可 建议dockerfile
[root@localhost docker-test-volume]# docker build -f dockerfile -t root/centos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 5d0da3dc9764
Step 2/4 : VOLUME ["VOLUME01","VOLUME02"]
---> Running in 213022c22b3c
Removing intermediate container 213022c22b3c
---> 14098f9dbf49
Step 3/4 : CMD echo ".......END........"
---> Running in 3846119080b9
Removing intermediate container 3846119080b9
---> 3e1a6ce67e85
Step 4/4 : CMD /bin/bash
---> Running in 9111e2c7dbad
Removing intermediate container 9111e2c7dbad
---> 711d7a145e27
Successfully built 711d7a145e27
Successfully tagged root/centos:1.0
[root@localhost docker-test-volume]#
#脚本
FROM centos
VOLUME ["VOLUME01","VOLUME02"]
CMD echo ".......END........"
CMD /bin/bash
[root@localhost docker-test-volume]# docker run -it 711d7a145e27 /bin/bash
[root@bc2c72fbd517 /]#
[root@bc2c72fbd517 /]#
[root@bc2c72fbd517 /]# ls -l
total 0
drwxr-xr-x. 2 root root 6 Apr 6 08:31 VOLUME01
drwxr-xr-x. 2 root root 6 Apr 6 08:31 VOLUME02
[root@bc2c72fbd517 /]#
#查看匿名挂载的卷
[root@localhost date]# docker inspect bc2c72fbd517
"Mounts": [
{
"Type": "volume",
"Name": "47be463cd41c38249c32ddcd8f964e7b488728117197db76496be0820572b8f7",
"Source": "/var/lib/docker/volumes/47be463cd41c38249c32ddcd8f964e7b488728117197db76496be0820572b8f7/_data",
"Destination": "VOLUME01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "06b32f7f73386966f2ad3a0a10b75612236f8516e5e40b17747d47fe4d13f961",
"Source": "/var/lib/docker/volumes/06b32f7f73386966f2ad3a0a10b75612236f8516e5e40b17747d47fe4d13f961/_data",
"Destination": "VOLUME02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
数据卷容器
MySQL数据共享
[root@localhost date]# docker run -it --name docker01 711d7a145e27
[root@0261402f3a5c /]# ls
VOLUME01 VOLUME02 bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost date]# docker run -it --name docker03 --volumes-from docker01 711d7a145e27
[root@804c93076511 /]# ls
VOLUME01 VOLUME02 bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@804c93076511 /]#
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/date:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw --name mysql01 mysql:5.7
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/date:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw --name mysql02
--volumes-from mysql01 mysql:5.7
#这个时候两个容器可以实现数据同步
结论:
容器之间配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止。
但是 数据一旦数据持久化 这个时候数据保存在本地,数据不会丢失。
DockerFile
DockerFile介绍
Dockerfile 是用来构建docker镜像的构建文件!命令脚本!
构建步骤
1.编写dockerfile文件
2.docker build 构建镜像
3.docker run 运行镜像
4.docker push 发布镜像
FROM scratch
ADD centos-7-x86_64-docker.tar.xz /
LABEL \
org.label-schema.schema-version="1.0" \
org.label-schema.name="CentOS Base Image" \
org.label-schema.vendor="CentOS" \
org.label-schema.license="GPLv2" \
org.label-schema.build-date="20201113" \
org.opencontainers.image.title="CentOS Base Image" \
org.opencontainers.image.vendor="CentOS" \
org.opencontainers.image.licenses="GPL-2.0-only" \
org.opencontainers.image.created="2020-11-13 00:00:00+00:00"
CMD ["/bin/bash"]
构建过程
基础知识:
1.每个保留关键字都是大写字母
2.从上到下的执行顺序
3.#表示注释
4.每个指令都会创建一层新的镜像层,并提交
dockerfile :构建文件 定义一切的步骤 源代码
dockerimages : 通过dockerfile 构建生成的镜像,最终发布和运行的产品
docker容器:容器就是镜像运行的起来提供服务器
Dockerfile指令
FROM #基础镜像,一切从这里开始构建
MAINTAINER # 镜像的创建者 姓名+邮箱
RUN #镜像构建的时候需要运行的命令
ADD #步骤:tomcat 镜像 这个tomcat 是压缩包!添加内容
WORKDIR #镜像的工作目录
VOLUME #挂载的目录
EXPOSE #暴露端口 和-p 效果一样
CMD #指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT #指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD #当构建一个被继承dockerfile 这个时候就会运行 ONBUILD 的指令 。触发指令
COPY #类似ADD 将我们的文件拷贝到镜像中
ENV #构建的时候设置环境变量
练习
docker hub 中大部分镜像都是从这个基础镜像开始的FROM scratch
#编写dockerfile 文件
[root@localhost dockerfile]# cat dockerfile
FROM centos
MAINTAINER lxf<821271702@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH #默认根目录
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "....end..."
CMD /bin/bash
#通过这个文件构建镜像
docker build -f dockerfile文件路径 -t 镜像名:[tag] .
docker build -f dockerfile -t mycentos:0.1 .
Successfully built a8cb1cb277c4
Successfully tagged mycentos:0.1
我们可以通过docker history images-ID 查看镜像历史的变化记录
[root@localhost dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 a8cb1cb277c4 29 minutes ago 231MB
root/centos 1.0 711d7a145e27 2 hours ago 231MB
tomcat02 1.0 aff84909b34e 13 hours ago 684MB
nginx latest 605c77e624dd 3 months ago 141MB
tomcat latest fb5657adc892 3 months ago 680MB
redis latest 7614ae9453d1 3 months ago 113MB
mysql 5.7 c20987f18b13 3 months ago 448MB
centos latest 5d0da3dc9764 6 months ago 231MB
portainer/portainer latest 580c0e4e98b0 12 months ago 79.1MB
elasticsearch latest 5acf0e8da90b 3 years ago 486MB
[root@localhost dockerfile]# docker histrtoy a8cb1cb277c4
docker: 'histrtoy' is not a docker command.
See 'docker --help'
[root@localhost dockerfile]# docker history a8cb1cb277c4
IMAGE CREATED CREATED BY SIZE COMMENT
a8cb1cb277c4 29 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
d63ed78135e0 29 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
7cf365c93efb 29 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
72ddfce337b6 29 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
60d933eef8d6 About an hour ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
fe31e1bb18fe About an hour ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
df5aaf1889e7 About an hour ago /bin/sh -c #(nop) MAINTAINER lxf<821271702@… 0B
5d0da3dc9764 6 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 6 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 6 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
[root@localhost dockerfile]#
CMD 、ENTRYPOINT区别
CMD #指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT #指定这个容器启动的时候要运行的命令,可以追加命令
测试cmd
#编写dockerfile文件
[root@localhost dockerfile]# cat dockerfile-cmd
FROM centos
CMD ["ls","-a"]
#构建镜像
[root@localhost dockerfile]# docker build -f dockerfile-cmd -t cmdtest .
Sending build context to Docker daemon 4.096kB
Step 1/2 : FROM centos
---> 5d0da3dc9764
Step 2/2 : CMD ["ls","-a"]
---> Using cache
---> 2efb513a6c19
Successfully built 2efb513a6c19
Successfully tagged cmdtest:latest
#run 运行 ls -a 生效
[root@localhost dockerfile]# docker run 2efb513a6c19
.dockerenv
bin
……
usr
var
#追加ls -al 报错
[root@localhost dockerfile]# docker run 2efb513a6c19 -l
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
[root@localhost dockerfile]#
[root@localhost dockerfile]# docker run 2efb513a6c19 ls -al
total 0
drwxr-xr-x. 20 root root 262 Sep 15 2021 var
[root@localhost dockerfile]#
#cmd 的清理下
#
[root@localhost dockerfile]# cat dockerfile-entry
FROM centos
ENTRYPOINT ["ls","-a"]
[root@localhost dockerfile]#
#entrypoint 可以在后面追加命令
[root@localhost dockerfile]# docker run c7b39ad4e057 -l
total 0
drwxr-xr-x. 1 root root 6 Apr 6 19:20 .
drwxr-xr-x. 1 root root 6 Apr 6 19:20 ..
-rwxr-xr-x. 1 root root 0 Apr 6 19:20 .dockerenv
[root@localhost dockerfile]#
练习:tomcat镜像
1、准备镜像文件 tomcat压缩包 、jdk压缩包
2、编写docfile 文件
FROM centos:7
MAINTAINER lxf<821271702@qq.com>
COPY readme.txt /usr/local/reademe.txt
ADD jdk-8u202-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-10.0.21.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_202
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-10.0.21
ENV CATALINA_BASH /usr/local/apache-tomcat-10.0.21
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-10.0.21/bin/startup.sh && tail -F /url/local/apache-tomcat-10.0.21/bin/logs/catalina.out
3、构建镜像
docker build -t diytomcat .
4、运行镜像
docker run -d -p 9090:8080 --name lxftomcat -v /home/lxf/build/tomcat/test:/usr/local/apache-tomcat-10.0.21/webapps/test -v /home/lxf/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-10.0.21/logs diytomcat
5、部署项目
[root@testhost test]# pwd
/home/lxf/build/tomcat/test
[root@testhost test]# tree
.
├── index.jsp
└── WEB-INF
└── web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
Hello World!<br/>
<%
out.println("你的 IP 地址 " + request.getRemoteAddr());
%>
</body>
</html>
验证:
发布镜像
docker hub
[root@testhost tomcatlogs]# docker login --help
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
[root@testhost tomcatlogs]# docker login -u 821271702
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@testhost tomcatlogs]# docker push lxf/diytomcat:1.0
The push refers to repository [docker.io/lxf/diytomcat]
An image does not exist locally with the tag: lxf/diytomcat
[root@testhost tomcatlogs]# docker tag 4de56e7ec301 lxf/diytomcat:1.0
[root@testhost tomcatlogs]# docker push lxf/diytomcat:1.0
The push refers to repository [docker.io/lxf/diytomcat]
9de296bd45b2: Preparing
ecea4b74c9ae: Preparing
be136704343b: Preparing
96cc59e44fa6: Preparing
阿里云镜像服务
1、开通容器镜像服务
2、创建命名空间
3、创建仓库
4、上传、下载镜像
1. 登录阿里云Docker Registry
$ docker login --username=李潇峰personal registry.cn-hangzhou.aliyuncs.com
用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码。
您可以在访问凭证页面修改凭证密码。
2. 从Registry中拉取镜像
$ docker pull registry.cn-hangzhou.aliyuncs.com/lxf_dockerhub/lxf_test:[镜像版本号]
3. 将镜像推送到Registry
$ docker login --username=李潇峰personal registry.cn-hangzhou.aliyuncs.com
$ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/lxf_dockerhub/lxf_test:[镜像版本号]
docker tag 4de56e7ec301 registry.cn-hangzhou.aliyuncs.com/lxf_dockerhub/lxf_test:1.0
$ docker push registry.cn-hangzhou.aliyuncs.com/lxf_dockerhub/lxf_test:[镜像版本号]
请根据实际镜像信息替换示例中的[ImageId]和[镜像版本号]参数。
4. 选择合适的镜像仓库地址
从ECS推送镜像时,可以选择使用镜像仓库内网地址。推送速度将得到提升并且将不会损耗您的公网流量。
如果您使用的机器位于VPC网络,请使用 registry-vpc.cn-hangzhou.aliyuncs.com 作为Registry的域名登录。
5. 示例
使用"docker tag"命令重命名镜像,并将它通过专有网络地址推送至Registry。
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry.aliyuncs.com/acs/agent 0.7-dfb6816 37bb9c63c8b2 7 days ago 37.89 MB
$ docker tag 37bb9c63c8b2 registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816
使用 "docker push" 命令将该镜像推送至远程。
$ docker push registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816w
5、验证
[root@testhost ~]# docker tag 4de56e7ec301 registry.cn-hangzhou.aliyuncs.com/lxf_dockerhub/lxf_test:1.0
[root@testhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/lxf_dockerhub/lxf_test:1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/lxf_dockerhub/lxf_test]
9de296bd45b2: Pushed
ecea4b74c9ae: Pushed
be136704343b: Pushed
96cc59e44fa6: Pushed
174f56854903: Pushed
1.0: digest: sha256:502604cf83e8cead2e98366fe8669ab81e683fffa97cd38e8b34085fc4a2248c size: 1373
小结
[root@testhost ~]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
[root@testhost ~]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output