Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker run with --volume

Tags:

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.

like image 380
planetp Avatar asked Dec 15 '17 11:12

planetp


People also ask

Does docker run create a volume?

If you start a container with a volume that does not yet exist, Docker creates the volume for you. The following example mounts the volume myvol2 into /app/ in the container. The -v and --mount examples below produce the same result.

What is docker run volume?

What are Docker Volumes? Docker volumes are file systems mounted on Docker containers to preserve data generated by the running container. The volumes are stored on the host, independent of the container life cycle. This allows users to back up data and share file systems between containers easily.

Can we attach volume to running container?

Adding a Volume To a Running Docker Container Containers must have their volumes configured on startup, which means to add a new volume, you must restart the container. While there is a hacky solution (more on that below), it's highly recommended that a container restart should be done anyway.


2 Answers

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.

like image 128
Oliver Charlesworth Avatar answered Sep 18 '22 15:09

Oliver Charlesworth


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.

like image 40
Hleb Rubanau Avatar answered Sep 20 '22 15:09

Hleb Rubanau