在 Docker 容器中运行 shell 脚本会导致“未找到”错误,即使该脚本位于 PATH 环境中

问题描述 投票:0回答:1

我有一个配置为在 Docker 容器中运行的 Django 应用程序。在 Dockerfile 中,我将一个名为

scripts
的目录复制到容器中,其中包含脚本
commands.sh
,然后将其添加到
PATH
环境变量中。该脚本旨在每次 Docker 容器启动时运行,并且 它在我的本地环境中正常工作

但是,当尝试在其他环境中运行Docker容器时,我遇到了以下错误消息:

djangoapp | exec /scripts/commands.sh: no such file or directory

恢复 Dockerfile:

FROM python:3.12-alpine3.19

# Copy the "djangoapp" and "scripts" folders into the container.
COPY ./djangoapp /djangoapp
COPY ./scripts /scripts

# Allowing permissions
RUN apk update && \
    apk add --no-cache binutils gdal geos proj-dev && \
  python -m venv /venv && \
  /venv/bin/pip install --upgrade pip && \
  /venv/bin/pip install -r /djangoapp/requirements.txt && \
  adduser --disabled-password --no-create-home duser && \
  chown -R duser:duser /venv && \
  chmod -R +x /scripts

# Setting to the path
ENV PATH="/scripts:/venv/bin:$PATH"

USER duser

# Executing
CMD ["commands.sh"]

尽管确认

commands.sh
脚本存在于
/scripts
目录中并且包含在
PATH
中,但该脚本仍无法执行。 container directory

[+] Running 2/2
 ✔ Container postgis    Recreated                                                        0.2s 
 ✔ Container djangoapp  Recreated                                                        0.1s 
Attaching to djangoapp, postgis
djangoapp  | exec /scripts/commands.sh: no such file or directory
djangoapp exited with code 1
  1. 我已验证commands.sh脚本存在于/scripts目录中。
  2. 我已经确认 /scripts 目录包含在 PATH 环境变量中。
  3. 我通过运行 chmod -R +x /scripts 确保脚本具有执行权限。
  4. 我尝试使用 ./scripts/commands.sh 手动执行脚本,这导致了同样的错误。

附加信息: 该项目可在 GitHub 上找到,如果您想尝试一下:https://github.com/dayvison2b/ze-delivery-partner-service

任何有关可能导致此问题的原因以及如何解决此问题的见解将不胜感激。谢谢!

docker docker-compose dockerfile
1个回答
0
投票

正如@David_Maze所指出的,问题的根本原因与

commands.sh
脚本中使用的行结尾有关。

根据

hexdump
命令的输出,我发现该脚本混合了 Unix 风格( ) 和 Windows 风格 ( ) 行结尾。修复后,CMD 命令能够找到并执行脚本。

我还发现我的第一个环境中的脚本运行正确,具有正确的行结尾。但不知何故,当我将其上传到 github 时,它被转换了。

© www.soinside.com 2019 - 2024. All rights reserved.