Mount an empty folder in host to a non-empty folder in Docker

Viewed 9643

The tool I'm using is delivered in a Docker image. Since installing the tool is extremely complicated with a bunch of dependencies, I want to work on the host with my IDE, but run it on the container.

So after download and load the image, I run:

sudo docker run -it -v /home/myself/WIP/thetool:/home/thetool name/label

Without mounting, the tool is located under /home/thetool, but with mounting, this folder is empty (since the folder in the host is empty).

Do I need to copy the tool from the container, then mount it, or there is a way to do it directly.

3 Answers

Docker-compose for yamenk great answer would be:

version: '2'                                                                                                                                                                                                        
services:
  your_service:
    volumes:
      - tool_vol:/home/thetool
    build: .
    command: your_command
volumes:
  tool_vol:
    driver: local
    driver_opts:
      type: none
      device: /home/myself/WIP/thetool
      o: bind

You're going against the grain for this, host volumes are designed to inject files from the host into the container and let you manage the files from outside of the container.

You can copy your tool out of the container as part of your container's entrypoint, but you'll need to mount your volume in a different location from where you have the directory inside the container so that you have both the source and target directories available. A sample entrypoint for this looks like:

#!/bin/sh

if [ ! -d "/data" ]; then
  ln -s /data_save /data
elif [ -z "$(ls -A /data)" ]; then
  cp -a /data_save/. /data/
fi
exec "$@"

And then the Dockerfile for that image would copy in and configure that entrypoint with:

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
Related