0

I am relatively new to Docker. I am struggling to fetch sensitive data on file which is set a docker volume.

This is how i have setup directory structure in Dockerfile

RUN mkdir /env
RUN touch /env/config.json

This my volume setup in docker-compose.yml

volumes:
      - ./main_app:/main_app
      - ./env/config.json:/env/config.json

I am trying to access data in config.json in Django settings file as shown

with open('/env/config.json') as config_file:
    config = json.load(config_file)

Now when i try to run docker-compose run i get the following error

unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

What could be the issue with my setup?

Muteshi
  • 820
  • 1
  • 10
  • 25
  • Does the configuration file exist on the host, in that exact path? (If it didn't, Docker would have created a directory, and then you'd get that error.) – David Maze Apr 04 '21 at 13:38
  • I am adding the file with this command ```RUN touch /env/config.json``` – Muteshi Apr 04 '21 at 13:46
  • So the image should contain an empty file; and when you start the container, you are trying to mount something on the host over that file. Does the file `./env/config.json` exist on the host? – David Maze Apr 04 '21 at 14:31

1 Answers1

0

You are tyring to mount a file as a volume and need to change

 - ./env/config.json:/env/config.json

to

 - ./env:/env
Kaj Hejer
  • 955
  • 4
  • 18
  • I modified as shown but then Django is throwing the following error ```FileNotFoundError: [Errno 2] No such file or directory: '/env/config.json'``` – Muteshi Apr 04 '21 at 13:45
  • Sorry about that! Please try to run your image interactively to see if the files are where you expect them to be by running the image with ```docker run -it ...```. Please see https://docs.docker.com/engine/reference/commandline/run/ for more details about running images interactively. – Kaj Hejer Apr 04 '21 at 13:49
  • When i run ```ls``` i get ```ls: cannot access '/env': No such file or directory``` – Muteshi Apr 04 '21 at 14:09
  • Strange... should work. See f.x. https://stackoverflow.com/a/46910980/924036. Sorry, dont have any other ideas at the moment. – Kaj Hejer Apr 04 '21 at 16:46