How do I mount a single file in docker-compose

Viewed 2058

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?

1 Answers

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.

Related