Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

difference between working directory of docker and singularity

I have a very basic question about docker and singularity. I'd like to be able to run a docker container using both docker and singularity, but I'm confused about the difference between the working directory. Here's an example: INSIDE my docker container, I have main.sh and test.sh.

Content of main.sh:

#!/bin/bash

echo "Executing main.sh"
./test.sh

Content of test.sh:

#!/bin/bash

echo "Executing test.sh"

When executing the following: sudo docker run -t myuser/mydocker:1.0 ./main.sh

I get the expected output:

Executing main.sh
Executing test.sh

But when executing singularity exec -e docker://myuser/mydocker:1.0 /main.sh

I get this error:

Executing main.sh
/main.sh: line 4: ./test.sh: No such file or directory

Why is test.sh not accessible? Is there a way to let it work through singularity without changing the content of main.sh? My singularity version is 3.6.3.

Thank you!

question from:https://stackoverflow.com/questions/65642199/difference-between-working-directory-of-docker-and-singularity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is one of the big differences between docker and singularity.

In docker, the working directory is set in the Dockerfile with WORKDIR /some/path or the -w /some/path command line argument. Otherwise, it uses the default of /.

With singularity however, your home directory (or current directory, on older versions) on the host machine is mounted in and used as the working directory inside the container. You can use the --pwd flag to override this.

So given the Dockerfile:

FROM debian:buster-slim
COPY main.sh test.sh /

Running with docker:

$ docker run --rm myuser/mydocker:1.0 ./main.sh
Executing main.sh
Executing test.sh

$ docker run --rm myuser/mydocker:1.0 pwd
/

In singularity:

# note the full path of the file that isn't found
$ singularity exec -e mydocker.sif ./main.sh
FATAL:   stat /home/tsnowlan/main.sh: no such file or directory

# does NOT match the WORKDIR from the Dockerfile
$ singularity exec -e mydocker.sif pwd
/home/tsnowlan

# use full path to the script
# BUT still executing from $HOME so `./test.sh` is resolves to `$HOME/test.sh`
$ singularity exec -e mydocker.sif /main.sh
Executing main.sh
/main.sh: line 4: ./test.sh: No such file or directory

# tell singularity to run from /
$ singularity exec -e --pwd / mydocker.sif ./main.sh
Executing main.sh in
Executing test.sh

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...