0

I'm trying to start/run a docker container from a python3 script with the docker-py library. Here's the code.

c = docker.DockerClient()
ctr = c.containers.run('alexwoif/docker-grib2json',
command='grib2json --names --data --fp 2 --fs 103 --fv 10.0 --output /usr/local/{}.json /usr/local/{}'.format(grib_file, grib_file), detach=True)

The python script is on the host, not in the container, so when I run the script it cannot find the target grib_file. The error message is:

b'Cannot find input file: /usr/local/gfs_wind_f000.grib\n'

The python script is located in /var/www/mywebsite/api/data/grib and this is also where the *.grib file is located.

The command string is similar to the example given in the grib2json github project except I am using the --output to write the json to file.

grib2json --names --data --fp 2 --fs 103 --fv 10.0 gfs.t18z.pgrbf00.2p5deg.grib2

For clarification, the *.grib files that should be read and process to json are in the same directory as the python script.

I think the disconnect is the docker container is looking internally for the files instead of on the host file system.

How do I make that connection?

Many thanks for any suggestions you may have. Bryan

Community
  • 1
  • 1
Bryan
  • 81
  • 2
  • 8
  • This sounds like you're looking for something like `subprocess.call()` and not anything Docker-related. If the script isn't in an image, and the inputs and outputs are in the host filesystem...how does Docker come into it? – David Maze Aug 04 '18 at 00:53
  • I'm thinking there has to be a way to push a file processed in a docker container out to the host file system, by some type of mapping or volume sharing. – Bryan Aug 04 '18 at 12:25

1 Answers1

0

Mounting the docker host's /usr/local is possible via the volumes argument:

c = docker.DockerClient()
ctr = c.containers.run(
    'alexwoif/docker-grib2json',
    command='grib2json --names --data --fp 2 --fs 103 --fv 10.0 --output /usr/local/{}.json /usr/local/{}'.format(grib_file, grib_file),
    detach=True,
    volumes={'/usr/local': {'bind': '/usr/local', 'mode': 'ro'}})
naaman
  • 900
  • 10
  • 15