为实现持久化,docker引入了挂载数据卷的方法,以实现数据的可持久化。具体是以下三种:
一.通过docker run命令
命令:
docker run -dit --name gerrit -p 9001:8080 -p 29419:29418 -v /home/docker/test.txt:/var/spool/cron/crontabs/root openfrontier/gerrit:2.15.3
此时将宿主机的/data/mysql1挂载到容器的/var/lib/mysql下,这里需要注意的还分以下几种情况:
A.挂载本地不存在的文件,到容器中已存在的文件,无法启动
docker run -dit --name gerrit -p 9001:8080 -p 29419:29418 -v /home/docker/review_site_test/root:/var/spool/cron/crontabs/root openfrontier/gerrit:2.15.3
结果无法启动
B.挂载本地不存在的文件夹,到容器内已存在的文件夹,可以启动,但会清空容器内的指定目录/var/spool/cron/crontabs下的所有文件
docker run -dit --name gerrit -p 9001:8080 -p 29419:29418 -v /home/docker/review_site_test:/var/spool/cron/crontabs openfrontier/gerrit:2.15.3
结果可以启动
但需要注意的是挂载本地不存在的文件夹过去,会清空当前容器的指定文件夹。如果容器中对该文件夹有启动依赖,例如上面的gerrit镜像:
使用下面的命令就无法启动:
docker run -dit --name gerrit -p 9001:8080 -p 29419:29418 -v /home/docker/review-site21a:/usr/share/gitweb openfrontier/gerrit:2.15.3
原因:gerrit依赖于gitweb
C.挂载本地存在的文件,到容器内指定文件,可以启动,该文件会覆盖你指定的文件
[root@zzw docker]# cat test.txt
askdkjajksdk
[root@zzw docker]# docker run -dit --name gerrit -p 9001:8080 -p 29419:29418 -v /home/docker/test.txt:/var/spool/cron/crontabs/root openfrontier/gerrit:2.15.3
c86aac5a73d551163c262bdf4db8cc4253322ac4c41d2d2418675c5b4f63e62a
bash-4.4# cd /var/spool/cron/crontabs
bash-4.4# ls
root
bash-4.4# vim root
bash: vim: command not found
bash-4.4# cat root
askdkjajksdk
D.挂载本地存在的文件夹test,到容器里已存在的文件夹,发现test目录中的文件,直接到容器内指定目录下
[root@zzw docker]# docker run -dit --name gerrit -p 9001:8080 -p 29419:29418 -v /home/docker/test:/var/spool/cron/crontabs openfrontier/gerrit:2.15.3
9c32db2cff1d8d7ed3636a812302470d9408f4c60786067a57da770b930ae01f
[root@zzw docker]# docker exec -it gerrit /bin/bash
bash-4.4# cd /var/spool/cron/
bash-4.4# ls
crontabs
bash-4.4# cd crontabs
bash-4.4# ls
test.txt
bash-4.4# vim test.txt
bash: vim: command not found
bash-4.4# cat test.txt
askdkjajksdk
F.挂载本地存在的文件夹test,到容器里不存在的文件夹,会在容器内创建该test文件夹
E的变种
[root@zzw docker]# docker run -dit --name gerrit -p 9001:8080 -p 29419:29418 -v /home/docker/test:/var/spool/cron/test openfrontier/gerrit:2.15.3
53fb24583eb65278599975ca515000080267e8348e945f1fcbd4fc4f8e6cb165
[root@zzw docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53fb24583eb6 openfrontier/gerrit:2.15.3 "/gerrit-entrypoin..." 4 seconds ago Up 2 seconds 0.0.0.0:9001->8080/tcp, 0.0.0.0:29419->29418/tcp gerrit
d114a167bbcc nginx:1.15.2-alpine "nginx -g ‘daemon ..." 41 hours ago Exited (0) 35 minutes ago test_nginx_1
641508207b44 mysql:5.7 "docker-entrypoint..." 41 hours ago Exited (0) 35 minutes ago test_db_1
[root@zzw docker]# docker exec -it gerrit /bin/bash
bash-4.4# cd /var/spool/cron/
bash-4.4# ls
crontabs test
bash-4.4# cd test/
bash-4.4# ls
test.txt
G.挂载本地是文件test.txt,到容器内指定的目录crontabs,报错
[root@zzw docker]# docker run -dit --name gerrit -p 9001:8080 -p 29419:29418 -v /home/docker/test/test.txt:/var/spool/cron/crontabs openfrontier/gerrit:2.15.3
e63487c08abce10c8bdbcf834714ad0994a93118abdd5126969856f862ca7f0b
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "process_linux.go:364: container init caused \"rootfs_linux.go:54: mounting \"/home/docker/test/test.txt\" to rootfs \"/var/lib/docker/overlay2/5b0428b09a0727e114f06d312106e4fb28bc31d728abd27dab2f30328497c3c7/merged\" at \"/var/lib/docker/overlay2/5b0428b09a0727e114f06d312106e4fb28bc31d728abd27dab2f30328497c3c7/merged/etc/crontabs\" caused \"not a directory\"\""
: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
看过第一种肯定有一些疑惑,因为有些镜像是违背第一种原则,即宿主机不存在某个目录,-v参数指定的宿主机目录:容器目录,容器目录里的东西立马都到宿主机指定的目录下了,不是因为宿主机的空目录覆盖掉容器的指定目录吗?怎么容器目录里的东西全都过来呢?
例如你用这条命令试试:
docker run -dit --name gerrit -p 9001:8080 -p 29419:29418 -v /home/docker/test1:/var/gerrit/review_site openfrontier/gerrit:2.15.3
别着急等会儿解释。
二.通过dockerfile中的volume创建挂载点
下面我们看下mysql的dockerfile
FROM debian:stretch-slim
add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r mysql && useradd -r -g mysql mysql
RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/*
add gosu for easy step-down from root
ENV GOSU_VERSION 1.7
RUN set -x \
&& apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \
&& wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \
&& wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \
&& export GNUPGHOME="$(mktemp -d)" \
&& gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \
&& gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \
&& gpgconf --kill all \
&& rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc \
&& chmod +x /usr/local/bin/gosu \
&& gosu nobody true \
&& apt-get purge -y --auto-remove ca-certificates wget
RUN mkdir /docker-entrypoint-initdb.d
RUN apt-get update && apt-get install -y --no-install-recommends \
for MYSQL_RANDOM_ROOT_PASSWORD
pwgen \
for mysql_ssl_rsa_setup
openssl \
FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:
File::Basename
File::Copy
Sys::Hostname
Data::Dumper
perl && rm -rf /var/lib/apt/lists/*
RUN set -ex; \
gpg: key 5072E1F5: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported
key=‘A4A9406876FCBD3C456770C88C718D3B5072E1F5‘; export GNUPGHOME="$(mktemp -d)"; gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; gpg --batch --export "$key" > /etc/apt/trusted.gpg.d/mysql.gpg; gpgconf --kill all; rm -rf "$GNUPGHOME"; apt-key list > /dev/null
ENV MYSQL_MAJOR 5.7
ENV MYSQL_VERSION 5.7.25-1debian9
RUN echo "deb http://repo.mysql.com/apt/debian/ stretch mysql-${MYSQL_MAJOR}" > /etc/apt/sources.list.d/mysql.list
the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn‘t have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql)
also, we set debconf keys to make APT a little quieter
RUN { \
echo mysql-community-server mysql-community-server/data-dir select ‘‘; \
echo mysql-community-server mysql-community-server/root-pass password ‘‘; \
echo mysql-community-server mysql-community-server/re-root-pass password ‘‘; \
echo mysql-community-server mysql-community-server/remove-test-db select false; \
} | debconf-set-selections \
&& apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}" && rm -rf /var/lib/apt/lists/* \
&& rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld \
&& chown -R mysql:mysql /var/lib/mysql /var/run/mysqld \
ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime
&& chmod 777 /var/run/mysqld \
comment out a few problematic configuration values
&& find /etc/mysql/ -name ‘*.cnf‘ -print0 | xargs -0 grep -lZE ‘^(bind-address|log)‘ | xargs -rt -0 sed -Ei ‘s/^(bind-address|log)/#&/‘ \
don‘t reverse lookup hostnames, they are usually another container
&& echo ‘[mysqld]\nskip-host-cache\nskip-name-resolve‘ > /etc/mysql/conf.d/docker.cnf
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 3306 33060
CMD ["mysqld"]
很明显有个标签:
VOLUME
这个标签我理解是将容器目录暴露出去,简单理解就是容器中的目录可以挂载到宿主机的某个匿名目录,一般是在docker的主程序目录下,一般没做修改的话就是/var/lib/docker下,我这里是/data/docker
来看下具体命令:
docker中数据卷探究与总结
那么我们到标红的那段里面看看,是否和/var/lib/mysql一样呢?结果是一样的
docker中数据卷探究与总结
那老是匿名不是个事,我们能指定吗?
第一种方法-v不就行了吗?
下面试试:
docker中数据卷探究与总结
结果发现是可以了,/var/lib/mysql里的东西全部到/data/mysql1里面了,这里其实是容器的目录复制到指定目录,宿主机的指定目录再挂载到容器的指定目录中,这理解起来可能有些困难,但是确实是这样。
那么我们就很好解决了上面gerrit数据卷的问题了,因为它的dockerfile里面也是有VOLUME标签的。
三.使用--volumes-from参数指定挂载的容器的宿主机目录
上图,不多bb
docker中数据卷探究与总结
可以发现mysql1和mysql用的是用一个宿主机目录/data/mysql1
好了,数据卷东西就写到这了
原文地址:http://blog.51cto.com/14204961/2351091