this post was submitted on 23 Mar 2024
29 points (100.0% liked)

Ask Lemmy

26875 readers
3213 users here now

A Fediverse community for open-ended, thought provoking questions

Please don't post about US Politics. If you need to do this, try [email protected]


Rules: (interactive)


1) Be nice and; have funDoxxing, trolling, sealioning, racism, and toxicity are not welcomed in AskLemmy. Remember what your mother said: if you can't say something nice, don't say anything at all. In addition, the site-wide Lemmy.world terms of service also apply here. Please familiarize yourself with them


2) All posts must end with a '?'This is sort of like Jeopardy. Please phrase all post titles in the form of a proper question ending with ?


3) No spamPlease do not flood the community with nonsense. Actual suspected spammers will be banned on site. No astroturfing.


4) NSFW is okay, within reasonJust remember to tag posts with either a content warning or a [NSFW] tag. Overtly sexual posts are not allowed, please direct them to either [email protected] or [email protected]. NSFW comments should be restricted to posts tagged [NSFW].


5) This is not a support community.
It is not a place for 'how do I?', type questions. If you have any questions regarding the site itself or would like to report a community, please direct them to Lemmy.world Support or email [email protected]. For other questions check our partnered communities list, or use the search function.


Reminder: The terms of service apply here too.

Partnered Communities:

Tech Support

No Stupid Questions

You Should Know

Reddit

Jokes

Ask Ouija


Logo design credit goes to: tubbadu


founded 1 year ago
MODERATORS
 

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 !

top 6 comments
sorted by: hot top controversial new old
[–] [email protected] 20 points 7 months ago* (last edited 7 months ago) (1 children)

You're close.

...
volumes:
  - ./:/data
...
working_dir:  /data

That will mount your CWD to /data inside the container and then set the working directory inside to that.

[–] [email protected] 2 points 7 months ago (1 children)

Thank you !!

One step closer :-D if I have the python file all ready to go in the directory, it works, but I can't seem to use my binaries (or the python script) if I compile them in into the image only.

I have my executables in a "binaries_to_use" folder, is there any way to add them to this local work folder? I tried the thing that worked before:

COPY binaries_to_use/setup /

but then

CMD ["./setup"]

doesn't work, I guess it's no longer a "virtual folder for the image" any more?

Thanks again, I'm getting less dumb about this :-p

[–] [email protected] 1 points 7 months ago

I have my executables in a “binaries_to_use” folder, is there any way to add them to this local work folder? I tried the thing that worked before:

Put them in one of the folders on the $PATH or provide an absolute path to them in your CMD

[–] [email protected] 4 points 7 months ago

Even if ./:./worked you want to be explicit with your container directories. . refers to the current directory and that will change based on what you define as your working directory or whatever the base image uses as a working directory. In other words, it's kind of brittle.

[–] [email protected] 2 points 7 months ago* (last edited 7 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

[–] [email protected] 2 points 7 months ago* (last edited 7 months ago)

You've pretty much answered it yourself in the example you gave: you can mount the host's relative path ./ to an absolute path /data in the compose file, and the container will mount the $CWD at launch as /data.

I think most bind mounts are default read-only, which is why your write is failing. Try adding ,rw to the end of the list entry in volumes and see if that helps.

edit: another poster got it with working_dir which also is more right

Using relative paths is bad practice, by the way. It's fine for development and testing, but in production you would want always to use absolute paths. Think of it as a bad habit that leaves surprises for yourself and othere down the road when moving or replicating projects. If you want it to be more dynamic, consider using compose vars and environment variables

Another thing you might want to consider is not mounting the project root as a RW filesystem in your containers, since this allows the container to modify its own compose file. That's rarely something you'd want to do. Better to make a subdirectory for each container in the project folder, and mount those into the appropriate contains