dockershelldocker-composedockerfileline-endings

Running a shell script in a Docker container results in a "not found" error, even the script being located in the PATH environment


I have a Django application configured to run in a Docker container. In the Dockerfile, I copy a directory called scripts which contains a script commands.sh to the container, which is then added to the PATH environment variable. The script is intended to run every time the Docker container starts, and it worked correctly in my local environment.

However, when attempting to run the Docker container in another environment, I encountered the following error message:

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

Resuming of the 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"]

Despite confirming that the commands.sh script exists in the /scripts directory and is included in the PATH, the script fails to execute. 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. I've verified that the commands.sh script exists in the /scripts directory.
  2. I've confirmed that the /scripts directory is included in the PATH environment variable.
  3. I've ensured that the script has execute permissions by running chmod -R +x /scripts.
  4. I've attempted to execute the script manually using ./scripts/commands.sh which resulted in the same error.

Additional Information: The project is available on GitHub, in case if you want to try it: https://github.com/dayvison2b/ze-delivery-partner-service

Any insights on what could be causing this issue and how to resolve it would be greatly appreciated. Thank you!


Solution

  • As pointed out by @David_Maze, the root cause of the issue was related to the line endings used in the commands.sh script.

    Based on the output of the hexdump command, I found that the script had a mix of Unix-style (\n) and Windows-style (\r\n) line endings. After I fix it, the CMD command was able to find and execute the script.

    I also found out that the script on my first environment, which ran correctly, had the correct line endings. But somehow when I uploaded it to github it was converted.