我正在尝试创建一个具有外部卷的docker容器,该容器应包含多个文件夹,因此我的Dockerfile的简化版本如下所示:
FROM ubuntu:12.04
# Create a volume for externally stored data that will persist across containers.
VOLUME ["/uploads"]
# Add the subfolders we need if they dont already exist
# however this never works.
RUN mkdir /uploads/folder1
RUN mkdir /uploads/folder2
每当我启动容器时
sudo docker run -i -t -v /uploads:/uploads [IMAGE ID] /bin/bash
/ uploads文件夹不包含folder1或folder2.但是,如果我用RUN mkdir / uploads替换VOLUME上传行,它可以使用此命令
sudo docker run -i -t [IMAGE ID] /bin/bash
但没有使用此命令(文件夹再次丢失):
sudo docker run -i -t -v /uploads:/uploads [IMAGE ID] /bin/bash
How can I set up the dockerfile so that files/folders will
automatically get added to the hosts mounted directory upon running
the container?
你没有. Dockerfile用于创建图像,以设置图像的内容.您可以直接在shell中设置已安装目录的内容:
# create folders:
mkdir /uploads123
mkdir /uploads123/folder1
mkdir /uploads123/folder2
# run container
docker run -i -t -v /uploads123:/uploads [IMAGE ID] /bin/bash
# for this trivial case,you can use directly ubuntu image,# it works,no need for Dockerfile
或者,您可以在启动主进程之前将一些安装脚本设置为在容器中运行.此安装脚本可以使用nescessary文件夹填充已装入的卷.
要解释行为 – 您的命令RUN mkdir / uploads / folder1确实在图像中创建了文件夹,但是您隐藏了已装入卷的文件夹,因此您看不到此文件夹(文件夹位于图像中,而不是位于主机文件夹中) .您无法在Dockerfile中的卷上创建文件夹,因为稍后将在运行时装入卷(容器).