0

I need to get a single file from my host machine into a per-existing directory in a docker container using docker-py. The directory shall not be overridden means mount is no option. Moving the directory to allow a shared volume between the host and the container is no option as well.

thinwybk
  • 4,193
  • 2
  • 40
  • 76
  • 1
    Have you tried mounting file, not directory? When you mount with `--volume` full path including filename and extension, it shouldn't overwrite directory. – Alejandro Galera May 16 '18 at 10:40
  • Yeah, but probably I just mixed up the arguments... – thinwybk May 16 '18 at 11:36
  • Seems like it works to mount a folder from the host into the container without overriding the parent directory with `client.containers.run(image='ubuntu', name='ubuntu', volumes={'/tmp/folder': {'bind': '/tmp/folder', 'mode': 'ro'}}, network='host', command='ls /tmp')`... – thinwybk May 16 '18 at 12:00
  • @AlexGalera Thx. Works. – thinwybk May 16 '18 at 12:16

1 Answers1

1

A single file on the host /tmp/hostfile.txt can be mounted into a container /tmp/containerfile.txt e.g. via pseudo code:

import docker

client = docker.from_env()
stdout = client.containers.run(
    image='ubuntu',
    name='ubuntu',
    volumes={
        '/tmp/container.txt': {
            'bind': '/tmp/hostfile.txt',
            'mode': 'ro',
        }
    },
    network='host',
    command='ls /tmp',
)

(The string stdout contains the file.txt.)

Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82
thinwybk
  • 4,193
  • 2
  • 40
  • 76