简体   繁体   English

Docker 容器以退出代码 2 退出“sh 无法打开‘start_script.sh’:没有这样的文件或目录”

[英]Docker container exited with exit code 2 "sh can't open 'start_script.sh': No such file or directory"

I have the following Dockerfile that we use to start Django servers with:我有以下 Dockerfile 用于启动 Django 服务器:

    FROM python:3.7.4-alpine3.10
    LABEL maintainer = ******
    
    ENV PYTHONUNBUFFERED 1S
    ENV RUNNING_IN_DOCKER True
    
    RUN apk add --update --no-cache build-base postgresql-client exiftool jpeg-dev zlib-dev gettext git openssl
    RUN apk add --update --no-cache gcc libc-dev linux-headers postgresql-dev file-dev py-magic libffi-dev libxml2-dev
    
    COPY ./requirements.txt /requirements.txt
    RUN pip install -r /requirements.txt
    
    RUN mkdir /app
    
    WORKDIR /app
    
    CMD sh start_script.sh

and the following docker-compose.yml:以及以下 docker-compose.yml:

    version: '3'
    
    services:
      backend:
        build: .
        restart: always
        ports:
          - 127.0.0.1:****:****
        env_file:
          - .env
        environment: &app-env
          - POSTGRES_HOST=db
          - POSTGRES_PORT=${POSTGRES_PORT}
          - POSTGRES_DB=${POSTGRES_DB}
          - POSTGRES_USER=${POSTGRES_USER}
          - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
          - REDIS_HOST=redis
          - REDIS_PORT=${REDIS_PORT}
        depends_on: &app-dep
          - db
          - redis
        volumes: &app-vol
          - ./app
      db:
        image: postgres:10-alpine
        restart: always
        ports:
          - ${POSTGRES_PORT}:****
        environment:
          - POSTGRES_DB=${POSTGRES_DB}
          - POSTGRES_USER=${POSTGRES_USER}
          - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
        volumes:
          - ${LOCAL_POSTGRES_DB_DATA}:/var/lib/postgresql/data
      redis:
        image: redis:5-alpine
        command: ["redis-server", "--appendonly", "yes"]
        restart: unless-stopped
        ports:
          - ${REDIS_PORT}:****
        volumes:
          - ${LOCAL_REDIS_DATA}:/data

When I try to run it I get the following output in my docker-compose logs:当我尝试运行它时,我在 docker-compose 日志中得到以下输出:

    Attaching to api_backend_1, api_redis_1, api_db_1
    backend_1  | sh: can't open 'start_script.sh': No such file or directory
    backend_1  | sh: can't open 'start_script.sh': No such file or directory
    backend_1  | sh: can't open 'start_script.sh': No such file or directory
    backend_1  | sh: can't open 'start_script.sh': No such file or directory
    backend_1  | sh: can't open 'start_script.sh': No such file or directory
    backend_1  | sh: can't open 'start_script.sh': No such file or directory
    db_1       | 
    db_1       | PostgreSQL Database directory appears to contain a database; Skipping initialization
    db_1       | 
    db_1       | 2020-09-25 09:14:20.936 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port ****
    db_1       | 2020-09-25 09:14:20.936 UTC [1] LOG:  listening on IPv6 address "::", port ****
    db_1       | 2020-09-25 09:14:20.945 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    db_1       | 2020-09-25 09:14:20.965 UTC [20] LOG:  database system was shut down at 2020-09-25 09:14:10 UTC
    db_1       | 2020-09-25 09:14:20.970 UTC [1] LOG:  database system is ready to accept connections
    redis_1    | 1:C 25 Sep 2020 09:14:20.818 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    redis_1    | 1:C 25 Sep 2020 09:14:20.818 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
    redis_1    | 1:C 25 Sep 2020 09:14:20.818 # Configuration loaded
    redis_1    | 1:M 25 Sep 2020 09:14:20.819 * Running mode=standalone, port=****.
    redis_1    | 1:M 25 Sep 2020 09:14:20.819 # Server initialized
    redis_1    | 1:M 25 Sep 2020 09:14:20.819 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    redis_1    | 1:M 25 Sep 2020 09:14:20.819 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    redis_1    | 1:M 25 Sep 2020 09:14:20.819 * Ready to accept connections
    api_backend_1 exited with code 2
    api_backend_1 exited with code 2

This is the file structure:这是文件结构:

/ git_repository
| - /app
| - requirements.txt
| - Dockerfile
| - docker-compose.yml
| - start_script.sh PERMISSIONS: -rwxrwxr-x 1 ubuntu ubuntu   208 Sep 25 08:11

The weird thing is that we have another service with the exact same Dockerfile and structure that does work correctly...奇怪的是,我们有另一个服务,其 Dockerfile 和结构完全相同,但可以正常工作......

The only thing I could find that might be wrong is in the Dockerfile the workingdir is changed to /app before running start_script.sh , but changing the run command to CMD sh ../start_script.sh didn't change the error.我唯一能发现的可能是错误的是在 Dockerfile 中,在运行start_script.sh之前将workingdir 更改为/app ,但是将运行命令更改为CMD sh ../start_script.sh并没有改变错误。 The copy of requirements.txt also doesn't make sense to me but that is unrelated to this error I think requirements.txt 的副本对我来说也没有意义,但我认为这与此错误无关

While searching I came across this post about line endings but that fix also didn't work for me.在搜索时,我遇到了这篇关于行尾的帖子,但该修复对我也不起作用。

I'm not sure how to proceed from here, or how I can debug this further, does anyone see what's wrong or have tips that I can try?我不知道如何从这里开始,或者我如何进一步调试,有没有人看到有什么问题或有我可以尝试的提示?

As per the comments on my question I used docker-compose run backend sh to inspect my container.根据对我的问题的评论,我使用docker-compose run backend sh来检查我的容器。 It turned out that I indeed didn't copy any source code or the start_script.sh to my container.事实证明,我确实没有将任何源代码或 start_script.sh 复制到我的容器中。 I made changes to my Dockerfile to make it work:我对 Dockerfile 进行了更改以使其正常工作:

FROM python:3.7.4-alpine3.10
LABEL maintainer = ******

ENV PYTHONUNBUFFERED 1S
ENV RUNNING_IN_DOCKER True

RUN apk add --update --no-cache build-base postgresql-client exiftool jpeg-dev zlib-dev gettext git openssl
RUN apk add --update --no-cache gcc libc-dev linux-headers postgresql-dev file-dev py-magic libffi-dev libxml2-dev

RUN mkdir /app
COPY ./start_script.sh /app/start_script.sh.      --------> Copy the start_script.sh
COPY ./requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt

COPY . /app                                       --------> Copy the source code
WORKDIR /app

CMD sh start_script.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM