MaxC MaxC - 1 month ago 9
Apache Configuration Question

How to execute script after container is just running

I'm using a docker container of apache.
I have to execute a script after the container is started.
How can I do this?

First of all, I have a dockerfile:

FROM:ubunty:trusty
... necessary installs

ADD test.sh /my-folder/

EXPOSE 80
EXPOSE 443

CMD ["/run.sh"]


In
run.sh
I'm executing as the last command:
exec apache2 -D FOREGROUND
, and this works.
Now I want an additional script
test.sh
to be be executed after the container is started.

In details, what I want is, after the apache container is started (this works), an additional script to be executed. That script has to connect to port 80 of my server (the port has been mapped at the run command).

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
61c66c17d5f1 my-apache "/run.sh" 28 minutes ago Up 28 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp suspicious_shockley

Answer

If you want manually run the script use:

docker exec 61c66c17d5f1 /myfolder/test.sh 

If you want your script be executed automatically after apache starts modify your run.sh:

exec apache2 &
sleep 2
/myfolder/test.sh
wait

Regards