I know that mounting/using a volume when running an image will overwrite the directory in the container with the directory on the host.
I'm wondering if this can be done the other way around.
I have data in my-image which was created during docker build.
docker run -dit --volume ~/data:/usr/data my-image
From the command above, the host ~/data directory will map inside of the Docker container to the /usr/data directory. I want the opposite behavior, I want the container /usr/data directory to map to the host ~/data (essentially overwriting anything inside the host directory ~/data).
Is this possible?
Here are some ideas:
Turn a folder inside the container into a volume (e.g. by marking it as a VOLUME inside the Dockerfile)
On the host, link or mount the volume folder into the desired location
(depending on what works and what you need).
This is possible because volumes live inside /var/lib/docker/volumes/
--you just need to find the relevant folder by inspecting the container.
The benefit here is that you can access the data even if the container is not running.
sshfsA more "generic" solution would be to use something like sshfs.
Obviously it requires your container to run ssh and to establish
the connection before the data from the container can be accessed.
As one solution is suggested by @Tomasz which is also possible, But before posting my answer I will share our use case why we doing this.
we map /data/ directory of the host at creation time with an empty directory of the container /root/data/ and then copy a remote code from /home/tempdata/data/ to /root/data of the container which overrides anything in the mount directory i.e ~/data of the host during boot time.
So as a result, the user will get the latest code from the container to host the directory which means a form of overriding a bit tricky way to achieve the opposite case.
docker build -t override .
docker run -it --rm --privileged -p 8081:8080 --name tt -v ~/data/:/root/data override
Here is the Dockerfile based on alpine
FROM python:3.6-alpine
RUN mkdir -p /etc/supervisord.d
RUN apk add --no-cache supervisor git npm
# general config for supervisord
RUN echo $'[supervisord] \n\
[unix_http_server] \n\
file = /tmp/supervisor.sock \n\
chmod = 0777 \n\
chown= nobody:nogroup \n\
[supervisord] \n\
logfile = /tmp/supervisord.log \n\
logfile_maxbytes = 50MB \n\
logfile_backups=10 \n\
loglevel = info \n\
pidfile = /tmp/supervisord.pid \n\
nodaemon = true \n\
umask = 022 \n\
identifier = supervisor \n\
[supervisorctl] \n\
serverurl = unix:///tmp/supervisor.sock \n\
[rpcinterface:supervisor] \n\
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface \n\
[include] \n\
files = /etc/supervisord
# clone remote code so that can override local code of the user specified directory
RUN git clone https://github.com/IBM-Bluemix/bluemix-hello-node /home/tempdata/data/
# copy code from container directory to host mount directory
RUN echo $'#!/bin/ash \n\
echo "********************************8" \n\
mkdir -p /root/data/ \n\
mkdir -p /home/tempdata/ \n\
echo "*******Overiding /data/ of host machine ****" \n\
# this will override host machine files like if server.js modified on host this will overide with remote server.js
cp -r /home/tempdata/data/ /root/ \n\
cd /root/data/; npm i; npm start' >> /root/override.sh
RUN echo $'[supervisord] \n\
nodaemon=true \n\
[program:override] \n\
command=/root/override.sh \n\
stdout_logfile=/dev/fd/1 \n\
stdout_logfile_maxbytes=0MB \n\
stderr_logfile_maxbytes = 0 \n\
stderr_logfile=/dev/fd/2 \n\
redirect_stderr=true \n\
autorestart=false \n\
startretries=0 \n\
exitcodes=0 ' >> /etc/supervisord.d/override.conf
RUN chmod +x /root/override.sh
EXPOSE 8080
ENTRYPOINT ["supervisord", "--nodaemon", "--configuration", "/etc/supervisord.conf"]