I'm trying to run a script during my building process in my Dockerfile
.
But it doesn't seems to work.
I tried that way:
FROM php:7-fpm
ADD bootstrap.sh /
ENTRYPOINT ["/bin/bash", "/bootstrap.sh"]
Also this way:
FROM php:7-fpm
ADD bootstrap.sh /
RUN bash -c "/bootstrap.sh"
And also by executing my running container:
docker exec symfony /bin/bash -c "/bootstrap.sh"
Nothing seems to work.
Do you know how to do it?
This question is related to
docker
dockerfile
Try to create script with ADD
command and specification of working directory
Like this("script" is the name of script and /root/script.sh
is where you want it in the container, it can be different path:
ADD script.sh /root/script.sh
In this case ADD
has to come before CMD
, if you have one
BTW it's cool way to import scripts to any location in container from host machine
In CMD
place [./script]
It should automatically execute your script
You can also specify WORKDIR
as /root
, then you'l be automatically placed in root, upon starting a container
In addition to the answers above:
If you created/edited your .sh script file in Windows, make sure it was saved with line ending in Unix format. By default many editors in Windows will convert Unix line endings to Windows format and Linux will not recognize shebang (#!/bin/sh) at the beginning of the file. So Linux will produce the error message like if there is no shebang.
Tips:
It's best practice to use COPY
instead of ADD
when you're copying from the local file system to the image. Also, I'd recommend creating a sub-folder to place your content into. If nothing else, it keeps things tidy. Make sure you mark the script as executable using chmod
.
Here, I am creating a scripts
sub-folder to place my script into and run it from:
RUN mkdir -p /scripts
COPY script.sh /scripts
WORKDIR /scripts
RUN chmod +x script.sh
RUN script.sh
WORKDIR /scripts
COPY bootstrap.sh .
RUN ./bootstrap.sh
Source: Stackoverflow.com