[英]How to mount the folder as volume that docker image build creates?
The docker-compose file is as follows: docker-compose文件如下:
version: "3"
services:
backend:
build:
context: .
dockerfile: dockerfile_backend
image: backend:dev1.0.0
entrypoint: ["sh", "-c"]
command: python manage.py runserver
ports:
- "4000:4000"
The docker build creates a folder lets say /docker_container/configs
which has files like config.json
and db.sqlite3
. docker 构建创建一个文件夹,比如说
/docker_container/configs
,其中包含config.json
和db.sqlite3
等文件。 The mounting of this folder as volumes is necessary because during runtime the content of the folder gets modified or updated,these changes should not be lost.必须将此文件夹安装为卷,因为在运行时文件夹的内容会被修改或更新,这些更改不应丢失。
I have tried adding a volumes as follows:我尝试按如下方式添加卷:
volumes:
- /host_path/configs:/docker_container/configs
Here the problem is mount point of the hostpath( /host_path/configs
) is empty initially so the container image folder( /docker_container/configs
) also gets empty.这里的问题是主机路径(
/host_path/configs
)的挂载点最初是空的,因此容器映像文件夹( /docker_container/configs
)也为空。 How could this problem be solved?.这个问题怎么解决?。
You are using a Bind Mount which will "hide" the content already existing in your image as you describe - /host_path/configs
being empty, /docker_container/configs
will be empty as well.您正在使用绑定挂载,它将“隐藏”您描述的图像中已经存在的内容 -
/host_path/configs
为空, /docker_container/configs
也将为空。
You can use named Volumes instead which will automatically populate the volume with content already existing in the image and allow you to perform updates as you described:您可以改用命名卷,它会自动使用映像中已存在的内容填充卷,并允许您按照描述执行更新:
services:
backend:
# ...
#
# content of /docker_container/configs from the image
# will be copied into backend-volume
# and accessible at runtime
volumes:
- backend-volume:/docker_container/configs
volumes:
backend-volume:
As stated in the Volume doc :如卷文档中所述:
If you start a container which creates a new volume [...] and the container has files or directories in the directory to be mounted [...] the directory's contents are copied into the volume
如果您启动一个创建新卷的容器 [...] 并且容器在要挂载的目录中有文件或目录 [...] 目录的内容将被复制到卷中
You can pre-populate the host directory once by copying the content from the image to directory.您可以通过将内容从图像复制到目录来预先填充主机目录。
docker run --rm backend:dev1.0.0 tar -cC /docker_container/config/ . | tar -xC /host_path/configs
Then start your compose project as it is and the host path already has the original content from the image.然后按原样启动您的撰写项目,并且主机路径已经包含图像中的原始内容。
Another approach is to have an entrypoint script that copies content to the mounted volume.另一种方法是使用入口点脚本将内容复制到已安装的卷。
You can mount the host path to a different path(say /docker_container/config_from_host
) and have an entrypoint script which copies content from /docker_container/configs
into /docker_container/config_from_host
if the directory is empty.您可以将主机路径挂载到不同的路径(例如
/docker_container/config_from_host
),并有一个入口点脚本,如果目录为空,则该脚本将/docker_container/configs
中的内容复制到/docker_container/config_from_host
中。
Sample pseudo code:示例伪代码:
$ cat Dockerfile
RUN cp entrypoint.sh /entrypoint.sh
CMD /entrypoint.sh
$ cat entrypoint.sh:
#!/bin/bash
if /docker_container/config_from_host is empty; then
cp -r /docker_container/config/* /docker_container/config_from_host
fi
python manage.py runserver
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.