187

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?

6
  • Does bootstarp.sh have the executable bit set? Commented Dec 31, 2015 at 18:38
  • Like that RUN chmod +x /bootstarp.sh ? Commented Dec 31, 2015 at 18:54
  • 2
    With "does not work", what exactly is happening? Does it show an error? Is the file present inside the image? If you docker exec -it symfony bash inside the container, can you manually run the script, and check its contents? (cat bootstarp.sh)? Commented Jan 1, 2016 at 2:37
  • try this: docker exec symfony /bin/bash /bootstarp.sh and let me know the output. Commented Jan 1, 2016 at 4:26
  • 33
    Ermahgerd bootstarp! Commented Jul 13, 2017 at 16:27

6 Answers 6

224

RUN and ENTRYPOINT are two different ways to execute a script.

RUN means it creates an intermediate container, runs the script and freeze the new state of that container in a new intermediate image. The script won't be run after that: your final image is supposed to reflect the result of that script.

ENTRYPOINT means your image (which has not executed the script yet) will create a container, and runs that script.

In both cases, the script needs to be added, and a RUN chmod +x /bootstrap.sh is a good idea.

It should also start with a shebang (like #!/bin/sh)

Considering your script (bootstrap.sh: a couple of git config --global commands), it would be best to RUN that script once in your Dockerfile, but making sure to use the right user (the global git config file is %HOME%/.gitconfig, which by default is the /root one)

Add to your Dockerfile:

RUN /bootstrap.sh

Then, when running a container, check the content of /root/.gitconfig to confirm the script was run.

Sign up to request clarification or add additional context in comments.

4 Comments

Also, you might have meant bootstrap.sh instead of bootstarp.sh: see stackoverflow.com/a/1254561/6309
The shebang on the first line did it for me, thanks! :)
Please check out of the box docker image - hub.docker.com/repository/docker/myset/bootstrap
@myset Interesting idea: executing $BOOTSTRAP, with that variable being set at runtime to what you want to execute.
62

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:

  • If you use Notepad++, you need to click "Edit/EOL Conversion/UNIX (LF)"
  • If you use Visual Studio, I would suggest installing "End Of Line" plugin. Then you can make line endings visible by pressing Ctrl-R, Ctrl-W. And to set Linux style endings you can press Ctrl-R, Ctrl-L. For Windows style, press Ctrl-R, Ctrl-C.

4 Comments

My VSCode had automatically set a shell script's lined endings to CRLF in windows. The docker build was failing because of some random characters. I changed the settings in VS Code and it immediately started to work. Thanks! I've upvoted.
on Windows git checks out by default with CR+LF, and this causes the issue for shell scripts when docker image is created, so your answer is really helpful!
Tip: Better give an extension to the scripts you are going to run from docker (e.g. .sh) and add a .gitattributes file to your git repo root with the following content: *.sh text eol=lf. Then you don't need to worry about newlines.
@MarinosAn , good advice, though it will not stop a text editor placing wrong ending when you edit the file. But, I agree, it is good to have that git setting anyway.
28

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

4 Comments

How does docker indentify where script.sh is at first? I mean, the first parameter, script.sh
since you're building container, you also in charge of placing paths
the path and name of the first param script.sh is relative to the Dockerfile
@eja it seems not: "source needs to be within the directory where the docker build command was run."
24

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

Comments

8

For running a bash script when during container creation:

Make script.sh file:

#!/bin/bash
you commands

If you are using windows, you must change script.sh file convention. To do this, in Notepad++, go to Edit -> EOL Conversion -> Change from CRLF to LF, and your bash file will be valid for execution.

If you are using an Alpine image, you must use #!/bin/sh instead of #!/bin/bash in the first line of your bash file.

Then, in your Dockerfile, copy your bash file to your image, and use the ENTRYPOINT instruction for running the file when container is being created:

COPY script.sh /
RUN chmod +x /script.sh
ENTRYPOINT ["/script.sh"]

Notice that the ENTRYPOINT instruction uses the location of the bash file you copied into your image.

now when you create a container, script.sh file will execute.

1 Comment

"notice: if you are using an alpine image, you must use #!/bin/sh`` instead of #!/bin/bash``` in the first line of your bash file.". Nice catch. Thank you
5
WORKDIR /scripts
COPY bootstrap.sh .
RUN ./bootstrap.sh 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.