I'm trying to dockerize some services for development on my machine and wondering how docker run --volume=..
works. For example, if I do something like
docker run --volume=/path/to/data:/data [...]
will /path/to/data
be (re)created locally only if it doesn't exist? Is the initial data copied from the container's image?
Links to relevant documentation would be appreciated.
The
--volume
option is described in thedocker 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:
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.
RUN mv /data /original-data
ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ['/entrypoint.sh']
The script
entrypoint.sh
could look like following (simplified example):If there's already some entrypoint script in your image, you can just add appropriate logic to it.