Search code examples
dockerdockerpy

Docker inside docker : volume is mounted, but empty


I am running a docker container with docker mounted inside using :

docker run -v /Path/to/service:/src/service -v /var/run/docker.sock:/var/run/docker.sock --net=host image-name python run.py

This runs a python script that creates a data folder in /src and fills it. When printing os.listdir('/src/data'), I get a list of files.

I then run a container from within this container, mounting the data folder, using docker-py.

volumes = {'/src/data': {'bind': '/src', 'mode': 'rw'}}    
client.containers.run(image, command='ls data', name=container_key, network='host', volumes=volumes)

And it prints :

Starting with UID: 0 and HOME: /src\n0\n'

Which means data is mounted, but empty. What am I doing wrong ?


Solution

  • So- mounting docker inside the container means that containers started from in there are running on your HOST machine.

    The end result is you have two containers on host- one with /Path/to/service:/src/service and one with /src/data:/src

    If you want to share a volume between two containers you should usually use a "named" volume like docker run -v sharedvolume:/src/data and docker run -v sharedvolume:/src