Docker - 从目录到主机 [英] Docker - Mount Directory From Container to Host
问题描述
-v / HOST / PATH:/ CONTAINER / PATH
标志是双向的。如果我们在容器中有文件或目录,它们将被镜像到主机上,这样我们可以在删除docker容器之后保留目录和文件。 官方的MySQL码头图像,这个工作。 $ / code>可以绑定到主机,并在维护主机上的数据的同时重新启动和更换容器。
我为sphinxsearch-2.2.9写了一个docker文件,就像练习一样,为了学习和理解,这里是:
FROM debian
ENV SPHINX_VERSION = 2.2.9-release
RUN apt-get update -qq&& DEBIAN_FRONTEND = noninteractive apt-get install -yqq\
build-essential\
wget\
curl\
mysql-client\
libmysql ++ - dev \
libmysqlclient15-dev \
checkinstall
RUN wget http://sphinxsearch.com/files/sphinx-${SPHINX_VERSION}.tar.gz&& tar xzvf sphinx - $ {SPHINX_VERSION} .tar.gz&& rm sphinx - $ {SPHINX_VERSION} .tar.gz
运行cd sphinx - $ {SPHINX_VERSION}&& ./configure --prefix = / usr / local / sphinx
EXPOSE 9306 9312
运行cd sphinx - $ {SPHINX_VERSION}&& make
RUN cd sphinx - $ {SPHINX_VERSION}&& make install
运行rm -rf sphinx - $ {SPHINX_VERSION}
VOLUME / usr / local / sphinx / etc
VOLUME / usr / local / sphinx / var
学习时,很容易让你的头包裹着。我正在分配/ etc& / var目录从sphinx构建到VOLUME命令,认为它将允许我做一些像 -v〜/ dev / sphinx / etc:/ usr / local / sphinx / etc -v〜/ dev / sphinx / var:/ usr / local / sphinx / var
,但它不是,而是覆盖容器内的目录并将其留空。当我删除-v标志并创建容器时,这些目录具有预期的文件,并且它们不被覆盖。
这是我运行以创建Docker文件之后导航到它所在的目录: docker build -t sphinxsearch。
一旦我创建了,我请执行以下操作以基于该映像创建容器: docker run -it --hostname some-sphinx --name some-sphinx --volume〜/ dev / docker / some-sphinx / etc: usr / local / sphinx / etc -d sphinxsearch
我真的很感激任何帮助和洞察力如何让它上班。我查看了MySQL图像,并没有看到任何神奇的东西,使他们使目录可绑定,他们使用VOLUME。
提前谢谢。
经过无数次的研究,我决定用以下Dockerfile扩展我的形象:
pre>
FROM sphinxsearch
VOLUME / usr / local / sphinx / etc
VOLUME / usr / local / sphinx / var
RUN mkdir -p / sphinx&& cd / sphinx&& cp -avr / usr / local / sphinx / etc。 &安培;&安培; cp -avr / usr / local / sphinx / var。
ADD docker-entrypoint.sh /
运行chmod + x /docker-entrypoint.sh
ENTRYPOINT [/docker-entrypoint.sh]
扩展它有益于我,因为我没有从头开始构建整个图像,因为我正在测试,并且仅构建相关的部分。
我创建了一个ENTRYPOINT来执行一个bash脚本,将文件复制到所需的目标,以使sphinx正常运行,这是代码:
#!/ bin / sh
set -e
target = / usr / local / sphinx / etc
#检查目录是否存在
如果[-d$ target];然后
#检查我们是否有文件
如果找到$ target-mindepth 1 -print -quit | grep -q。然后
#没有文件不做任何事情
#我们可以使用这个,如果条件以后的其他东西
回显不空,不要做任何事情...
else
#我们没有任何文件,我们将
#文件从etc和var复制到正确的位置
cp -avr / sphinx / etc / * / usr / local / sphinx / etc&& cp -avr / sphinx / var / * / usr / local / sphinx / var
fi
else
#目录不存在,我们将在这里做一些事情
echo需要创建目录...
fi
exec$ @
访问/ etc& / var目录在主机允许我调整文件,同时保持在主机之间重新启动之间等等...我还有保存在主机上的数据,应该在重新启动后生存。
我知道这是一个关于数据容器的辩论主题,而不是存储在主机上,此时我倾向于在主机上存储,但是稍后会尝试其他方法。如果任何人有任何提示,建议等,以改善我有什么或更好的方式,请分享。
谢谢@ h3nrik的建议和提供帮助!
I thought I understood the docs, but maybe I didn't. I was under the impression that the -v /HOST/PATH:/CONTAINER/PATH
flag is bi-directional. If we have file or directories in the container, they would be mirrored on the host giving us a way to retain the directories and files even after removing a docker container.
In the official MySQL docker images, this works. The /var/lib/mysql
can be bound to the host and survive restarts and replacement of container while maintaining the data on the host.
I wrote a docker file for sphinxsearch-2.2.9 just as a practice and for the sake of learning and understanding, here it is:
FROM debian
ENV SPHINX_VERSION=2.2.9-release
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -yqq\
build-essential\
wget\
curl\
mysql-client\
libmysql++-dev\
libmysqlclient15-dev\
checkinstall
RUN wget http://sphinxsearch.com/files/sphinx-${SPHINX_VERSION}.tar.gz && tar xzvf sphinx-${SPHINX_VERSION}.tar.gz && rm sphinx-${SPHINX_VERSION}.tar.gz
RUN cd sphinx-${SPHINX_VERSION} && ./configure --prefix=/usr/local/sphinx
EXPOSE 9306 9312
RUN cd sphinx-${SPHINX_VERSION} && make
RUN cd sphinx-${SPHINX_VERSION} && make install
RUN rm -rf sphinx-${SPHINX_VERSION}
VOLUME /usr/local/sphinx/etc
VOLUME /usr/local/sphinx/var
Very simple and easy to get your head wrapped around while learning. I am assigning the /etc & /var directories from the sphinx build to the VOLUME command thinking that it will allow me to do something like -v ~/dev/sphinx/etc:/usr/local/sphinx/etc -v ~/dev/sphinx/var:/usr/local/sphinx/var
, but it's not, instead it's overwriting the directories inside the container and leaving them blank. When i remove the -v flags and create the container, the directories have the expected files and they are not overwritten.
This is what I run to create the docker file after navigating to the directory that it's in: docker build -t sphinxsearch .
And once I have that created, I do the following to create a container based on that image: docker run -it --hostname some-sphinx --name some-sphinx --volume ~/dev/docker/some-sphinx/etc:/usr/local/sphinx/etc -d sphinxsearch
I really would appreciate any help and insight on how to get this to work. I looked at the MySQL images and don't see anything magical that they did to make the directory bindable, they used VOLUME.
Thank you in advance.
After countless hours of research, I decided to extend my image with the following Dockerfile:
FROM sphinxsearch
VOLUME /usr/local/sphinx/etc
VOLUME /usr/local/sphinx/var
RUN mkdir -p /sphinx && cd /sphinx && cp -avr /usr/local/sphinx/etc . && cp -avr /usr/local/sphinx/var .
ADD docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
Extending it benefited it me in that I didn't have to build the entire image from scratch as I was testing, and only building the parts that were relevant.
I created an ENTRYPOINT to execute a bash script that would copy the files back to the required destination for sphinx to run properly, here is that code:
#!/bin/sh
set -e
target=/usr/local/sphinx/etc
# check if directory exists
if [ -d "$target" ]; then
# check if we have files
if find "$target" -mindepth 1 -print -quit | grep -q .; then
# no files don't do anything
# we may use this if condition for something else later
echo not empty, don\'t do anything...
else
# we don't have any files, let's copy the
# files from etc and var to the right locations
cp -avr /sphinx/etc/* /usr/local/sphinx/etc && cp -avr /sphinx/var/* /usr/local/sphinx/var
fi
else
# directory doesn't exist, we will have to do something here
echo need to creates the directory...
fi
exec "$@"
Having access to the /etc & /var directories on the host allows me to adjust the files while keeping them preserved on the host in between restarts and so forth... I also have the data saved on the host which should survive the restarts.
I know it's a debated topic on data containers vs. storing on the host, at this moment I am leaning towards storing on the host, but will try the other method later. If anyone has any tips, advice, etc... to improve what I have or a better way, please share.
Thank you @h3nrik for suggestions and for offering help!
这篇关于Docker - 从目录到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!