Search code examples
dockerdockerfile

Adding a startup script after a dockerfile is created


I have this dockerfile:

FROM alpine:latest
WORKDIR /app
RUN echo -e "#!/bin/bash\necho test" > /app/test.sh
RUN chmod 744 /app/test.sh
ENTRYPOINT [ "/app/test.sh" ]

when I run it, I get exec /app/test.sh: no such file or directory

I tried adding a line before ENTRYPOINT to see if the file test.sh is present in /app:

FROM alpine:latest
WORKDIR /app
RUN echo -e "#!/bin/bash\necho test" > /app/test.sh
RUN chmod 744 /app/test.sh
RUN ls -al && exit 1
ENTRYPOINT [ "/app/test.sh" ]

And I got this result:

[+] Building 0.6s (8/8) FINISHED                                                                                        docker:default
 => [internal] load build definition from Dockerfile                                                                              0.0s
 => => transferring dockerfile: 200B                                                                                              0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                  0.3s
 => [internal] load .dockerignore                                                                                                 0.0s
 => => transferring context: 2B                                                                                                   0.0s
 => [1/5] FROM docker.io/library/alpine:latest@sha256:77726ef6b57ddf65bb551896826ec38bc3e53f75cdde31354fbffb4f25238ebd            0.0s
 => CACHED [2/5] WORKDIR /app                                                                                                     0.0s
 => CACHED [3/5] RUN echo -e "#!/bin/bash\necho test" > /app/test.sh                                                              0.0s
 => CACHED [4/5] RUN chmod 744 /app/test.sh                                                                                       0.0s
 => ERROR [5/5] RUN ls -al && exit 1                                                                                              0.2s
------                                                                                                                                 
 > [5/5] RUN ls -al && exit 1:
0.171 total 16
0.171 drwxr-xr-x    1 root     root          4096 May 31 05:21 .
0.171 drwxr-xr-x    1 root     root          4096 May 31 05:28 ..
0.171 -rwxr--r--    1 root     root            22 May 31 05:21 test.sh
------
Dockerfile:5
--------------------
   3 |     RUN echo -e "#!/bin/bash\necho test" > /app/test.sh
   4 |     RUN chmod 744 /app/test.sh
   5 | >>> RUN ls -al && exit 1
   6 |     ENTRYPOINT [ "/app/test.sh" ]
--------------------
ERROR: failed to solve: process "/bin/sh -c ls -al && exit 1" did not complete successfully: exit code: 1

Which shows the file test.sh exists with execution permission. What am I missing here?


Solution

  • The Alpine image doesn't have bash installed, so the file that it can't find is bash.

    If you change the 'shebang' in your script to /bin/sh like this, it'll work:

    FROM alpine:latest
    WORKDIR /app
    RUN echo -e "#!/bin/sh\necho test" > /app/test.sh
    RUN chmod 744 /app/test.sh
    ENTRYPOINT [ "/app/test.sh" ]
    

    If you need bash, you can install it yourself in the image using RUN apk add --no-cache bash or you can use an image like debian that has it installed by default.