this post was submitted on 17 Apr 2024
16 points (86.4% liked)

Asklemmy

43989 readers
645 users here now

A loosely moderated place to ask open-ended questions

Search asklemmy ๐Ÿ”

If your post meets the following criteria, it's welcome here!

  1. Open-ended question
  2. Not offensive: at this point, we do not have the bandwidth to moderate overtly political discussions. Assume best intent and be excellent to each other.
  3. Not regarding using or support for Lemmy: context, see the list of support communities and tools for finding communities below
  4. Not ad nauseam inducing: please make sure it is a question that would be new to most members
  5. An actual topic of discussion

Looking for support?

Looking for a community?

~Icon~ ~by~ ~@Double_[email protected]~

founded 5 years ago
MODERATORS
 

Hello all !

I have a docker image that you can run with:

docker compose -f compose_10f.yml up

The compose_10f.yml looks like this:

services:
  setup:
    image: tenfingers_10f:v1
    volumes:
      - ./:/data
    working_dir: /data/

Which makes the image believe it runs in ./ so if it saves "./hello" it will be saved in the folder where it's launched (it works).

The thing is, it's a command line program (named 10f.py), not a server or such, so I'd like to run it like this:

docker run -v ./:/data -w /data/ tenfingers_10f:v1 10f.py

And it works with the exception it doesn't get to run in the mounted ./ folder.

It confuses the "mount" (or I'm just lucky the compose file works?) and it believes it lives in /data/, not in ./

python3: can't open file '/data/10f.py': [Errno 2] No such file or directory

I did struggle to set this up in the compose file, but I'd like to make the images run in a specific directory thinking they are in ./

Any ideas how I can figure this out?

Cheers and thank you so much!

Valmond

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 3 points 7 months ago* (last edited 7 months ago) (1 children)

It works in docker compose because compose handles relative paths for the volumes, the docker CLI doesn't.

You can achieve this by doing something like

docker run -v $(pwd):/data ...

pwd is a command that returns the current path as an absolute path, you can just run it by itself to see this. $() syntax is to execute the inner command separately before the shell runs the rest of it. (Same as backticks, just better practice)

I imagine that wouldn't work on windows, but it would on either osx, Linux or wsl.

Generally speaking, if you need the file system access and your CLI requires some setup, I'd recommend either writing it in a statically compiled language (e.g. golang, rust) or researching how to compile a python script into an executable.

If you're just mounting your script in the container - you're better off adding it directly at build time.

[โ€“] [email protected] 1 points 7 months ago (1 children)

Thanks for the thorough answer, really appreciate it.

You sure had my hopes up a bit too ๐Ÿ˜ but it doesn't work. I also tried with the absolute path (from pwd) and others to no avail, docker run has an "inner" working dir of /data/ still. Guess it can't be ./

Funny you are talking about compiling and such, the project is in python and I have frozen executables (not the most elegant solution, but its fire up and forget for the user), I just thought it simpler in a docker image.

So back to the drawing board to figure out the best way to tackle it all. As I do have one image running a server, I could make it take commandline arguments and dispatch, but it's not exactly a beautiful solution. Or make the softs work in a designated directory.

Thanks anyways!

[โ€“] [email protected] 3 points 7 months ago* (last edited 7 months ago) (2 children)

I think I misunderstood your problem, I assumed the issue was the volume mounts and after testing it I was indeed wrong - the docker cli now accepts relative paths so your original command does the same as what I suggested. After re-reading your issue I have a different idea of what's wrong, but would have to see your dockerfile (or for you to confirm) to be sure.

Do you add 10f.py to the docker image when you build it and do you specify the command/entrypoint in the Dockerfile? There are possibly to issues I can think of with how you do that (although considering the docker compose works it's probably the 2nd):

  1. You do add it and you add it to /data in the image - when you mount a volume over it would make the script no longer exist in the container.
  2. You do add it and it's not in /data - in this case the issue with running docker run -v ./:/data -w /workdir tenfigers_10f:v1 10f.py is the last bit - you override the command which makes it try to look for it at /data/10f.py, if you omit it the last part (10f.py) it should run whatever the original command was and assuming you set the cmd/entrypoint correctly in the Dockerfile it should see /data as ./ in python.

(Also when you run it with the CLI you might want to add -it --rm as well to the docker command otherwise it won't really behave similarly to a regular command)

[โ€“] [email protected] 1 points 7 months ago

Thanks again, I think I have to bite the bullet and rewrite my programs so they can work in a folder, but I'll check out your suggestions first ๐Ÿ˜Š !