赞
踩
当我们容器启动之后,每个容器是单独隔离运行的,如果数据只保留在当前容器中,那么当容器被删除后,我们操作的数据也会被删除。
为了解决这种容器内数据的持久化保存以及不同容器之间的数据共享问题,docker提供了容器卷技术,可以将容器内的文件目录挂载到容器外(比如Linux系统)的某个目录下,以实现容器的数据操作同步到容器外,并且被挂载目录内的数据共享给容器。
docker run -it -v 主机目录:容器目录 镜像
# 示例
docker run -it -v /home/centosData:/home/centosData centos bash
# 通过挂载目录后,容器和主机操作的centosData.java文件在彼此内都可以看见,即使容器被删除了,本地的文件依然存在,实现数据的共享以及本地的持久化
使用DockerFile进行构建镜像时可以直接在构建镜像的脚本中使用VOLUME构建数据卷。
#构建镜像语法:
docker build -f dockerFile -t volume-learn .
# -f 指定用于构建镜像的文件
# -t 指定目标镜像名称
# . 指定当前目录
示例:
[root@localhost volumn_learn]# pwd /learn/volumn_learn [root@localhost volumn_learn]# vi dockerFile [root@localhost volumn_learn]# cat dockerFile FROM centos VOLUME ["volume01","volume02"] CMD echo "---------end--------" CMD /bin/bash [root@localhost volumn_learn]# docker build -f dockerFile -t volume-learn . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 5d0da3dc9764 Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in cd374350dac1 Removing intermediate container cd374350dac1 ---> 192453b694dd Step 3/4 : CMD echo "---------end--------" ---> Running in c34c9b7b1dd3 Removing intermediate container c34c9b7b1dd3 ---> a3a48975dc22 Step 4/4 : CMD /bin/bash ---> Running in e4fa1aef2e18 Removing intermediate container e4fa1aef2e18 ---> eb085d9bbb22 Successfully built eb085d9bbb22 Successfully tagged volume-learn:latest [root@localhost volumn_learn]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE volume-learn latest eb085d9bbb22 4 minutes ago 231MB
从上图可以看见已正常构建volume-learn镜像,启动镜像后可以观察相关数据卷的挂载信息:
可以使用 --volumes-from 父容器 镜像名
命令来实现多个数据卷容器之间的数据共享。
# volume01 volume02 数据卷在容器内正常存在 [root@localhost volumn_learn]# docker run -it volume-learn /bin/bash [root@a58d88199047 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02 # 通过docker inspect 容器id 查看数据挂载情况,并且如下图展示,已实现数据之间的共享 [root@localhost volumn_learn]# docker inspect a58d88199047 ··· "Mounts": [ { "Type": "volume", "Name": "73b2b24706b792f173efd9b45ddb99641dc13653ed6eb4687a5b3ce7db04d2d2", "Source": "/var/lib/docker/volumes/73b2b24706b792f173efd9b45ddb99641dc13653ed6eb4687a5b3ce7db04d2d2/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "8008dec9795095ee1e37eae3fd9c91f58de424b044187cb92ca0e96bc3d98e2d", "Source": "/var/lib/docker/volumes/8008dec9795095ee1e37eae3fd9c91f58de424b044187cb92ca0e96bc3d98e2d/_data", "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ] ···
通常在我们使用-v命令进行挂载时,可以区分为具名挂载、匿名挂载以及上图展示的绝对路径挂载。
# -v命令格式包括:
-v 容器内路径 # 匿名挂载
-v 指定数据卷名:容器内路径 # 具名挂载
-v /宿主机绝对路径:容器内路径 # 绝对路径挂载
# 容器数据权限,可以在容器路径后面指定ro,rw读写权限
-v 容器内路径:ro # 只读
-v 容器内路径:rw # 读写
# 查询数据卷名命令
docker volume
ls # 列出数据卷
inspect # 列出某个数据卷相信信息
示例:
[root@localhost /]# docker run -d -p 3301:3306 -v mysql-juming:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-juming mysql:5.7 df8a1485ce66c7ce74cd81240a2e87cfaf605c3123b8426e39b6a70fc3817099 [root@localhost /]# docker volume Usage: docker volume COMMAND Manage volumes Commands: create Create a volume inspect Display detailed information on one or more volumes ls List volumes prune Remove all unused local volumes rm Remove one or more volumes Run 'docker volume COMMAND --help' for more information on a command. [root@localhost /]# docker volume ls DRIVER VOLUME NAME local 9f155325f86c5ccbfdd75d1a2fa65af90f0b6aead0d7c02467981be78362b593 local 757afa5fced4bf4899985896b10413d5608e0377467f89c56d141fa8f5889c58 local b9745c8460d39a8973c29ab1f1a7103d77acf296dee3a319790008257fad41c3 local ea4f9e5c2501df7c42ac1bfc58898a00cf5acde2a4f93b39a4880688dcee7913 local mysql-juming [root@localhost /]# docker volume inspect mysql-juming [ { "CreatedAt": "2022-08-09T16:31:37+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/mysql-juming/_data", "Name": "mysql-juming", "Options": null, "Scope": "local" } ]
dockerfile是构建镜像的文件,通过编写脚本文件来构建我们自己的镜像。
FROM # 基础镜像
MAINTAINER # 作者 姓名+邮箱
RUN # 镜像构建是需要运行的命令
ADD # 添加内容,会自动解压压缩包
WORKDIR # 镜像工作目录
VOLUME # 挂载卷
EXPOSE # 暴露端口
CMD # 指定容器启动时需要运行的命令,只有最后一个生效,可被替代
ENTRYPOINT # 指定容器启动时需要运行的命令,可以追加命令
ONBUILD # 触发指令,当以当前镜像为基础镜像,去构建下一级镜像的时候才会被执行
COPY # 将文件copy到镜像中
ENV # 构建时设置环境变量
tomcat压缩包下载地址:https://mirrors.cnnic.cn/apache/tomcat/
jdk8压缩包下载地址: https://www.oracle.com/java/technologies/downloads/
FROM centos MAINTAINER shuirong<ShuiR0129@126.com> COPY readme.txt /usr/local/readme.txt ADD jdk-8u341-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-10.0.23.tar.gz /usr/local/ RUN yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_333 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-10.0.23 ENV CATALINA_BASE /usr/local/apache-tomcat-10.0.23 FROM centos MAINTAINER shuirong<ShuiR0129@126.com> COPY readme.txt /usr/local/readme.txt ADD jdk-8u341-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-10.0.23.tar.gz /usr/local/ RUN yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_333 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-10.0.23 ENV CATALINA_BASE /usr/local/apache-tomcat-10.0.23 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 CMD /usr/local/apache-tomcat-10.0.23/bin/startup.sh && tail -F /usr/local/apache-tomcat-10.0.23/bin/logs/catalina.out
docker build -t mytomcat .
docker run -d -p 8080:8080 -v /learn/volumn_myTomcat/test:/usr/local/apache-tomcat-10.0.23/webapps/test -v /learn/volumn_myTomcat/tomcatLogs/:/usr/local/apache-tomcat-10.0.23/logs mytomcat
# web.xml文件
<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_3_1.xsd"
version="3.1">
</web-app>
# jsp主页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
docker login -u userName
[root@localhost myTomcat]# docker login -u shui0129
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
docker push shui0129/mytomcat:1.0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。