5

I have a project that need to mount a single directory into the docker container, and I mount it in a similar way

agent:
    image: agent:latest
    container_name: agent
    volumes:
      - $PWD/status.txt:/status.txt

Is A Directory error occurs when I modify status.txt in open mode.

with open('status.txt','a') as f:
    ...
    ...

docker-compose seems to recognize files as directories.

I would appreciate it if you could tell me how to solve it?

moluzhui
  • 1,003
  • 14
  • 34
  • You `docker-compose.yml` works just fine for me. If you get a directory instead of a file, that means that from Docker's perspective the source file didn't exist. Can you tell us more about your environment? Are you running Docker under Linux? MacOS? Windows? – larsks Nov 27 '20 at 14:41
  • @larsks I run docker under linux with Ubuntu – moluzhui Nov 28 '20 at 15:04
  • 1
    Don't use `$PWD` in the volume mapping and instead use `./status.txt:/status.txt`! `./status.txt` will always refer to the file `status.txt` right next to the `docker-compose.yml` while using `$PWD` might yield unexpected results when you run `docker-compose` form another directory then the one containing the `docker-compose.yml`. – acran Nov 28 '20 at 17:28

1 Answers1

0

I can mount files just fine using the same syntax, although I use a relative path, e.g.:

volumes:
 - ./sourcedir/file.txt:/target/dir/mountedfile.txt

Where mountedfile.txt is essentially file.txt. There must be something else in your environment causing this issue. To troubleshoot, there are a couple of things you could do, including:

  1. Get a shell into the container and run stat on the target file, e.g. stat mountedfile.txt. That should tell you if it's a file or directory.

  2. Test your configuration manually with plain docker using -v to mount the volumes, e.g.:

docker run --rm -ti -v /sourcedir/file.txt:/target/mountedfile.txt ubuntu:latest bash

Also, there may be some useful information in a (somewhat) unrelated answer.

Nagev
  • 10,835
  • 4
  • 58
  • 69