Hello all!

Can you mount any folder a docker image is running in?

So for example, if I have a python script creating a file “./hello.txt”, it would be written in the folder where I launch “docker-compose up” ?

I have figured out how to write the hello.txt to a subfolder like /data/ (by mapping an image-local folder to /data/), but I’d like to use like ./ for the image itself instead. So that the folder I’m launching the docker-compose in on my PC is mapped to ./ in the image, if that makes more sense.

So this works (in the compose.yml):

volumes:

  - ./:/data

but the script must write to “./data/hello.txt”

This doesn’t work:

volumes:

  - ./:./

It pops an error: mount path must be absolute

Any idea if this is even possible?

Cheers and thanks !

  • phorq@lemmy.ml
    link
    fedilink
    Español
    arrow-up
    2
    ·
    edit-2
    3 months ago

    If the script is working like that it implies that your working_dir is / inside the container, hence why your python script can write to ./data instead needing to say of /data with the absolute forward slash. To my knowledge volumes cannot be mounted that way because the working directory inside the container is constantly changing, but you can set the initial working directory to /data. If you don’t want the python script inside the data folder/working directory but still want to be able to call it without specifying a path to the script you’ll need to copy it into a bin folder. So in summary you would have the script in bin, your working directory would be /data which would be associated with your machine’s directory you used when starting the docker-compose and the script would just use the path ./

    Edit: another option if you can’t change the starting working directory is simply to cd first: cd /data && yourscript