docker run with --volume

…衆ロ難τιáo~ 提交于 2019-12-05 07:15:41

The --volume option is described in the docker run reference docs, which forwards you on to the dedicated Managed data in containers docs, which then forwards you on to the Bind mounts docs.

There, it says:

If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v will create the endpoint for you. It is always created as a directory.

Yes, the directory on the host FS will be created only if it does not already exist.

The same time, Docker will not copy anything from the image into bind-mounted volume, so the mount path will appear as empty directory inside the container. Whatever was in the image will be hidden.

If you need original data to be copied over, you need to implement this functionality yourself. Fortunately, it is pretty easy thing to do.

  1. Among the last steps in Dockerfile, move or copy the original directory elsewhere. E.g. RUN mv /data /original-data
  2. Add a custom script to the image, which would serve as entrypoint, and will copy the data needed into the mounted volume (see code example below). Dockerfile directive: ADD entrypoint.sh /entrypoint.sh
  3. Add ENTRYPOINT directive, to instruct Docker to invoke your script as a part of container initialization: ENTRYPOINT ['/entrypoint.sh']

The script entrypoint.sh could look like following (simplified example):

#!/bin/bash
set -e
SOURCE_DIR=/original-data 
TARGET_DIR=/data
if [ $(find $TARGET_DIR -maxdepth 0 -type d -empty) 2>/dev/null) ]; then
   cp -r --preserve-all $SOURCE_DIR/* $TARGET_DIR/
fi

# continue Docker container initialization, execute CMD
exec $@

If there's already some entrypoint script in your image, you can just add appropriate logic to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!