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

18,259

As per the comments on my question I used docker-compose run backend sh to inspect my container. It turned out that I indeed didn't copy any source code or the start_script.sh to my container. I made changes to my Dockerfile to make it work:

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
Share:
18,259
NG.
Author by

NG.

Software Engineer, hobby programmer and gamer.

Updated on June 04, 2022

Comments

  • NG.
    NG. almost 2 years

    I have the following Dockerfile that we use to start Django servers with:

        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:

        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:

        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...

    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. The copy of requirements.txt also doesn't make sense to me but that is unrelated to this error I think

    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?

    • urfin78
      urfin78 over 3 years
      With WORKDIR you change the the working directory for the following CMD statement. See documentation If start_script.sh is not in /app directory you have to specify a path so CMD /start_script.sh should work. But why you're changing the WORKDIR anyway?
    • David Maze
      David Maze over 3 years
      It doesn't look like you COPY the script or any of your source code into the image, which would produce that error. Have you tried looking with something like docker-compose run backend sh to get an interactive shell in the container to see what's actually there?
    • NG.
      NG. over 3 years
      @urfin78 thanks I will try that. I'm not sure why I'm changing the workdir, I took over this project just before deployment. I am looking at django dockerfiles to see if it's a django specific thing.
    • NG.
      NG. over 3 years
      @DavidMaze thanks! I will try to get an interactive shell to see what's going on. I'm new to docker so I didn't know that was an option! I was having trouble getting information on what's going on but this seems like a good option to try.