从 Dockerfile 写入 docker 卷不起作用 [英] Writing to docker volume from Dockerfile does not work

查看:36
本文介绍了从 Dockerfile 写入 docker 卷不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下 Dockerfile:

Please consider the following Dockerfile:

FROM phusion/baseimage
VOLUME ["/data"]
RUN touch /data/HELLO
RUN ls -ls /data

问题:/data"目录不包含HELLO"文件.此外,任何其他写入卷目录的尝试(通过 echo、mv、cp、...)都不会成功 - 该目录始终为空.没有显示错误消息.

Problem: "/data" directory does not contain "HELLO" file. Moreover, any other attempts to write to volume directory (via echo, mv, cp, ...) are unsuccessful - the directory is always empty. No error messages shown.

我在文档或 stackoverflow 中找不到关于此问题的任何内容.

I could not find anything in documentation or on stackoverflow regarding this problem.

这是众所周知的还是新的?

Is this something well-known or new?

docker version 返回:

Client version: 1.2.0
Client API version: 1.14
Go version (client): go1.3.1
Git commit (client): fa7b24f
OS/Arch (client): linux/amd64
Server version: 1.2.0
Server API version: 1.14
Go version (server): go1.3.1
Git commit (server): fa7b24f

推荐答案

Dockerfile 的每一步都在它自己的容器中运行,该容器在该步骤完成时被丢弃,而卷在最后一步时被丢弃(仅在这种情况下)使用它们的容器在命令完成后被删除.这使得卷不适合在 Dockerfiles 中使用,因为它们在中途丢失了内容.Docker 文件旨在能够在任何地方运行,如果它们使用持久化的 Volumes,这将使这变得更加困难.另一方面,如果你真的想要这个,只需在主机上使用一个目录备份卷.

Each step of the Dockerfile is run in it's own container that is discarded when that step is done, and volumes are discarded when the last (in this case only) container that uses them is deleted after it's command finishes. This makes volumes poorly suited to use in Dockerfiles because they loose their contents half way through. Docker files are intended to be able to be run anywhere, and if they used Volumes that persisted it would make this harder. On the other hand if you really want this, just back the volume with a directory on the host.

PS:初始化主机的数据目录最好在 Docker 文件之外完成.

PS: Initializing the host's data directory is best done outside of the Docker file.

上次我需要这个时,我把这一步从 docker 文件中省略了,因为这一步的想法是准备 host 来运行这个 Dockerfile 生成的图像.然后我用 docker run 创建了一个容器,在该容器中我运行了通常的数据库设置.

Last time I needed this I left this step out of the docker file because the idea of this step is to prepare the host to run the Image produced by this Dockerfile. Then I made a container with docker run and within that container I ran the usual DB setup stuff.

docker run -v /var/lib/mysql:/raid/.../mysql ...
/usr/bin/mysql_install_db 
mysql_secure_installation

现在,当这个容器被移动到一个新的主机上时,那个数据目录可以随它一起携带,也可以在该主机上使用相同的进程创建.或者,在我的示例中,如果您想要其他应用程序的另一个 mysql 数据库,则不必重复创建容器.

Now when this container is moved to a new host, that Data dir can either be brought with it, or created using the same process on that host. Or if, as in my example, you wanted another mysql db for some other application you don't have to repeat the container creation.

重要的想法是将容器创建和主机设置分开.

The important idea is to keep the container creation and host setup seperate.

这篇关于从 Dockerfile 写入 docker 卷不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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