Run a script in Dockerfile

Kevin picture Kevin · Dec 31, 2015 · Viewed 240.6k times · Source

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 bu executing my running container:

docker exec symfony /bin/bash -c "/bootstrap.sh"

Nothing seems to work.

Do you know how to do it?

Answer

VonC picture VonC · Jan 1, 2016

RUN and ENTRYPOINT are two different way 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 (bootstarp.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 /bootstart.sh

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