【问题标题】:Docker container fails on Windows Powershell succeeds on WSL2 with identical Dockerfile and docker-composeDocker 容器在 Windows Powershell 上失败,在 WSL2 上成功,具有相同的 Dockerfile 和 docker-compose
【发布时间】:2022-12-02 21:31:31
【问题描述】:

问题描述

我有一个使用 docker-compose 构建和运行的 docker 镜像。通常我在 WSL2 上开发,当运行 docker-compose up --build 时,图像构建并成功运行。在另一台机器上,使用 Windows powershell,使用相同的代码克隆,执行相同的命令成功构建图像,但在运行时出错。

错误

    [+] Running 1/1
     - Container fastapi-service  Created                                                                              0.0s
    Attaching to fastapi-service
    fastapi-service  | exec /start_reload.sh: no such file or directory
    fastapi-service exited with code 1

我在使用 Docker 方面相当有经验,但对于 PowerShell 和更普遍的 Windows 开发来说,我是一个完全的新手。在这种情况下,Dockerfile 构造是否存在差异,或者 COPY 和 RUN 语句的执行存在差异?

代码sn-ps

包括复制错误所需的所有代码部分。

文件

    FROM tiangolo/uvicorn-gunicorn:python3.7

    COPY requirements.txt requirements.txt
    RUN pip install --no-cache-dir -r requirements.txt

    COPY ./start.sh /start.sh
    RUN chmod +x /start.sh

    COPY ./start_reload.sh /start_reload.sh
    RUN chmod +x /start_reload.sh

    COPY ./data /data
    COPY ./app /app

    EXPOSE 8000 

    CMD ["/start.sh"]

docker-compose.yml

    services:
      web:
        build: .
        container_name: "fastapi-service"

        ports:
          - "8000:8000"

        volumes:
          - ./app:/app
        command: /start_reload.sh

启动-reload.sh

这是一个小的 shell 脚本,它运行 prestart.sh(如果存在),然后以“重新加载模式”启动 gunicorn/uvicorn:

    #!/bin/sh
    # If there's a prestart.sh script in the /app directory, run it before starting
    PRE_START_PATH=/app/prestart.sh

    HOST=${HOST:-0.0.0.0}
    PORT=${PORT:-8000}
    LOG_LEVEL=${LOG_LEVEL:-info}

    echo "Checking for script in $PRE_START_PATH"
    if [ -f $PRE_START_PATH ] ; then
        echo "Running script $PRE_START_PATH"
        . "$PRE_START_PATH"
    else 
        echo "There is no script $PRE_START_PATH"
    fi

    # Start Uvicorn with live reload
    exec uvicorn --host $HOST --port $PORT --log-level $LOG_LEVEL main:app --reload 

【问题讨论】:

    标签: docker powershell docker-compose wsl-2 line-endings


    【解决方案1】:

    解决方案在于 UNIX 和 Windows 系统之间的差异,以及它们结束行的方式。可以在 [此处] 找到有关该主题的讨论。 (Difference between CR LF, LF and CR line break types?)

    文件中这些字符的存在/不存在以及运行命令的 shell 配置会导致错误,其中正在运行的文件是 Dockerfile start-reload.sh(CR-LF) 但存在的文件只是 start-reload.sh,因此是 no such file or directory出现错误。

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      相关资源
      最近更新 更多