Docker容器无法从容器中找到文件

发布于 2025-01-31 04:18:15 字数 1979 浏览 6 评论 0原文

我正在使用Docker在主机卷上查找一些文件并直接修改它们。 Docker容器运行时间无法在主机卷中找到文件{s},通过-V< path>:< path>:z

但是,当我使用Docker运行 - IT ... myimage/bin/bash模式时,这将毫无任何问题找到文件。

所描述的整个设置都是通过詹金斯(Jenkins)作业运行的。

我的dockerfile

FROM centos:7
SHELL ["/bin/bash", "-c"]

COPY ./run-find.sh /
RUN ["chmod", "+x", "/run-find.sh"]

ENTRYPOINT ["/bin/bash", "/run-find.sh"]
CMD ["${FIND_DIR}"]

我的run-find.sh脚本:

#!/bin/bash
set -x
echo "Find dir: $FIND_DIR"
set -e
mapfile -t files_list < <(find "$FIND_DIR" -type f ! -name "*.asc" \
    ! -name "*.sha1" \
    ! -name "*.sh" \
    ! -name "_remote.repositories" \
    ! -name "*.lastUpdated")
# Can list the file unsing ls
ls ${SIGN_DIR} -la

if [ "${#files_list[@]}" -eq 0 ]; then
    echo "ERROR: No files in map. Quitting..."
    exit 1
else
   echo "processing file list"
fi

构建图像:

docker build -f /w/Dockerfile \
      --build-arg FIND_DIR=${FIND_DIR} \
      -t myimage ${WORKSPACE}

运行容器如下:

docker run -e KEY1="${KEY1}" -e PASS="${PASS}" \
          -u root:root \
          -e MY_CONFIG=${MY_CONFIG} \
          -v /w:/w:z \
          myimage /run-find.sh "${FIND_DIR}"

我缺少什么?我将获得以下输出,发现无法在DIR中找到文件,但是,我可以在测试运行时使用LS列出或列出文件。

输出:

+ docker run -e KEY=test-key -e PASS=/w/pass -u root:root -e CONFIG=/w/my-config.conf -v /w:/w:z -v /home/jenkins:/home/jenkins:z myimage /run-find.sh /w/repo
FIND_DIR: /w/repo
+ echo 'FIND_DIR: /w/repo'
+ set -e
+ pwd
+ ls /w/repo -la
/
total 12
drwxrwxr-x. 2 1000 1000 4096 May 20 08:01 .
drwxr-xr-x. 3 1000 1000 4096 May 20 08:03 ..
-rw-rw-r--. 1 1000 1000    8 May 20 08:01 test.pom
+ id
uid=0(root) gid=0(root) groups=0(root)
+ cd /w/repo
+ ls
test.pom
+ mapfile -t files_list
++ find . -type f
ERROR: No files in map. Quitting...
+ '[' 0 -eq 0 ']'
+ echo 'ERROR: No files to sign. Quitting...'
+ exit 1

I'm using docker to find a few files on the host volume and modify them directly.
The docker container run time is unable to find file{s} in the host volume, shared through -v <path>:<path>:z.

However, when I run the container using docker run -it ... myimage /bin/bash mode this finds the files without any issues.

The entire setup described is being run through a Jenkins job.

My Dockerfile

FROM centos:7
SHELL ["/bin/bash", "-c"]

COPY ./run-find.sh /
RUN ["chmod", "+x", "/run-find.sh"]

ENTRYPOINT ["/bin/bash", "/run-find.sh"]
CMD ["${FIND_DIR}"]

My run-find.sh script:

#!/bin/bash
set -x
echo "Find dir: $FIND_DIR"
set -e
mapfile -t files_list < <(find "$FIND_DIR" -type f ! -name "*.asc" \
    ! -name "*.sha1" \
    ! -name "*.sh" \
    ! -name "_remote.repositories" \
    ! -name "*.lastUpdated")
# Can list the file unsing ls
ls ${SIGN_DIR} -la

if [ "${#files_list[@]}" -eq 0 ]; then
    echo "ERROR: No files in map. Quitting..."
    exit 1
else
   echo "processing file list"
fi

Build the image:

docker build -f /w/Dockerfile \
      --build-arg FIND_DIR=${FIND_DIR} \
      -t myimage ${WORKSPACE}

Run the container as follows:

docker run -e KEY1="${KEY1}" -e PASS="${PASS}" \
          -u root:root \
          -e MY_CONFIG=${MY_CONFIG} \
          -v /w:/w:z \
          myimage /run-find.sh "${FIND_DIR}"

What am I missing? I get the following output and find is unable to find the files in the dir, however, I can list it with an ls or list the files when I do a test run.

output:

+ docker run -e KEY=test-key -e PASS=/w/pass -u root:root -e CONFIG=/w/my-config.conf -v /w:/w:z -v /home/jenkins:/home/jenkins:z myimage /run-find.sh /w/repo
FIND_DIR: /w/repo
+ echo 'FIND_DIR: /w/repo'
+ set -e
+ pwd
+ ls /w/repo -la
/
total 12
drwxrwxr-x. 2 1000 1000 4096 May 20 08:01 .
drwxr-xr-x. 3 1000 1000 4096 May 20 08:03 ..
-rw-rw-r--. 1 1000 1000    8 May 20 08:01 test.pom
+ id
uid=0(root) gid=0(root) groups=0(root)
+ cd /w/repo
+ ls
test.pom
+ mapfile -t files_list
++ find . -type f
ERROR: No files in map. Quitting...
+ '[' 0 -eq 0 ']'
+ echo 'ERROR: No files to sign. Quitting...'
+ exit 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2025-02-07 04:18:15

所讨论的问题与Dockerfile或Bach脚本本身无关,但是,这是因为脚本需要额外的卷发括号。当将参数传递给Shell脚本时,您需要逃脱卷曲括号,否则它们只会在脚本中被0替换,而 在作业控制台上不容易调试。

更详细的解释可以在在这里

The issue in question is not related to the dockerfile or bach script itself, however, it's because scripts required an extra set of curly braces. When passing the parameters to shell scripts, you would need to escape curly braces or they would simply get replaced with a 0 in the script which is not easy to debug through the job console.

More detailed explanation can be found in here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文