docker - Docker-compose:exec用户进程导致 “no such file or directory”但文件存在

标签 docker docker-compose

使用以下docker-compose.yml文件,我创建一个包含两个服务的堆栈:一个是Web应用程序(称为Broker),另一个是Web应用程序使用的MySQL数据库(称为broker_db)。

我发现我必须告诉代理服务等待MySQL初始化,并且我可以使用一个Shell脚本来延迟代理真正启动Web应用程序的时间,直到它可以开始使用数据库为止。但是,在运行docker-compose up时出现以下错误:

 broker_1     | standard_init_linux.go:190: exec user process caused "no such file or directory"

我的docker-compose.yml文件是:
version: '3'

services:
  broker-db:
    image: mysql:5.7 # https://store.docker.com/images/mysql
    restart: always
    environment:
      MYSQL_DATABASE: broker # Create a database with name broker during container initialization.
      MYSQL_ROOT_PASSWORD: root # Set root/root as user/password credentials in the container.
    ports:
      - "3302:3306"
    volumes:
      - ./broker-db-volume:/var/lib/mysql
    networks:
      - shared-network

  broker:
    build: .
    command: ["/code/wait-for-mysql-init.sh", "broker-db", "python3", "manage.py", "runserver", "0.0.0.0:8080"]
    volumes:
      - .:/code
    ports:
      - "8080:8080"
    depends_on:
      - broker-db
    restart: always
    networks:
      - shared-network

networks:
  shared-network:

代理的Dockerfile将其根项目文件夹中的所有代码添加到Docker容器中的/code中,所以我不明白为什么docker-compose up表示找不到wait-for-mysql-init.sh:
# Based on https://docs.docker.com/compose/django
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
RUN chmod +x wait-for-mysql-init.sh

Shell脚本如下:
#!/bin/bash
# wait-for-mysql-init.sh

# Taken from https://stackoverflow.com/a/29793382/5433896.
# Argument handling based on https://unix.stackexchange.com/questions/31414.
# Delayed command execution based on https://docs.docker.com/compose/startup-order/.

set -e # Exit immediately if a command exits with a non-zero status (https://stackoverflow.com/a/19622569/5433896).

DB_HOST="$1" # First argument passed to the script
shift # Forget the first argument, $2 becomes $1, $3 becomes $2 and so on.
command="$@" # All remaining arguments form the command to execute after MySQL is initialized.

while ! mysqladmin ping -h"$DB_HOST" --silent; do
    echo "The initialization of MySQL on $DB_HOST is still ongoing."
    sleep 1
done

echo "The initialization of MySQL on $DB_HOST has finished. It is ready to accept incoming queries."
echo "MySQL is up and running. Executing command $command."
exec $command

另外,我的经纪人项目内容的简要概述:
C:\*somepath*\broker
├───broker-application-code
├───broker-db-volume
├───broker
├───.dockerignore
├───docker-compose.yml
├───Dockerfile
├───manage.py
├───requirements.txt
└───wait-for-mysql-init.sh

因此,肯定存在用于添加到Docker镜像的Shell脚本,ADD指令位于Dockerfile中,为什么在启动代理容器时仍找不到Shell脚本?

最佳答案

对于 shell 程序脚本,找不到可执行文件意味着脚本本身不存在,或者脚本在第一行启动的 shell 程序不存在。要寻找的东西:

  • 图片中是否存在/ bin / bash?因为python 3.6是基于Debian的,所以应该这样。
  • 是容器内的卷装载覆盖目录吗? .:/code卷挂载意味着您​​在该位置的镜像中执行的所有操作都将被替换,但是您似乎正在同一目录中运行。
  • 是该脚本确实试图运行/bin/bash,还是该文件中有Windows换行符? Linux不了解Windows的换行符,因此它将查找要执行的/bin/bash^M,该代码不存在。
  • 关于docker - Docker-compose:exec用户进程导致 “no such file or directory”但文件存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50757368/

    相关文章:

    docker - 安装 scikit-image 时没有名为 'numpy' 的模块

    docker - Docker-compose无法解析DNS来纠正服务

    bash - super 账本结构 - docker 容器错误

    spring-boot - 当通过 Dockerfile 上的 gradle 图像触发时,Jooq 无法找到数据库

    mysql - 具有多个 Mysql 容器的 Docker 无法正常工作

    while-loop - 从 docker-compose breaking while 循环中退出代码

    docker - 为什么docker build后node_modules为空?

    linux - 如何使 postgresql 数据库可从 postgres docker 容器外部连接?

    docker - package.json文件不会在Docker容器中保留

    docker - 无法连接到 keycloak 管理面板