在Container中挂载目录并与主机共享 [英] Mount directory in Container and share with Host

查看:184
本文介绍了在Container中挂载目录并与主机共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我了解文档,但也许我听不懂。我的印象是 -v / HOST / PATH:/ CONTAINER / PATH 标志是双向的。如果我们在容器中有文件或目录,它们将被镜像到主机上,从而为我们提供了即使删除docker容器后仍保留目录和文件的方法。

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.

官方的MySQL docker镜像,可以正常工作。 / var / lib / mysql 可以绑定到主机,并且可以在重新启动和替换容器的同时保留主机上的数据。

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.

我为sphinxsearch-2.2.9编写了一个docker文件,作为一种实践,为了学习和理解,这里是:

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

学习时非常简单容易。我正在分配/ etc& / sp目录(从sphinx构建到VOLUME命令)认为这将允许我执行 -v〜/ dev / sphinx / etc:/ usr / local / sphinx / etc -v〜/ dev / sphinx / var:/ usr / local / sphinx / var ,但不是,而是覆盖了容器内的目录,并将它们留空。当我删除-v标志并创建容器时,目录具有预期的文件并且它们不会被覆盖。

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.

这是我运行后用于创建docker文件的内容导航到它所在的目录: docker build -t sphinxsearch。

This is what I run to create the docker file after navigating to the directory that it's in: 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

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

我真的很感激任何帮助和深刻见解。我看着MySQL映像,没有看到它们使目录可绑定而做出的任何神奇的事情,他们使用了VOLUME。

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.

请先谢谢您。

推荐答案

经过无数小时的研究,我决定使用以下Dockerfile扩展映像:

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.

我创建了一个ENTRYPOINT来执行bash脚本,该脚本会将文件复制回所需的目标,以便狮身人面像正常运行,这是该代码:

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 "$@"

可以访问/ etc&主机上的/ var目录使我能够调整文件,同时在两次重启之间将它们保留在主机上,等等。。。我还将数据保存在主机上,这些数据应该在重启后仍然存在。

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.

感谢@ h3nrik的建议和帮助!

Thank you @h3nrik for suggestions and for offering help!

这篇关于在Container中挂载目录并与主机共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆