EDIT:
I've learned a lot about Docker since originally posting this answer. "Starting services automatically in Docker containers" is not a good usage pattern for Docker. Instead, use something like fleet, Kubernetes, or even Monit/SystemD/Upstart/Init.d/Cron to automatically start services that execute inside Docker containers.
ORIGINAL ANSWER:
If you are starting the container with the command /bin/bash
, then you can accomplish this in the manner outlined here: https://mcmap.net/q/245729/-run-a-service-automatically-in-a-docker-container
So, if you are starting the container with docker run -i -t IMAGE /bin/bash
and if you want to automatically start apache2 when the container is started, edit /etc/bash.bashrc
in the container and add /usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf
(or whatever your apache2 start command is) to a newline at the end of the file.
Save the changes to your image and restart it with docker run -i -t IMAGE /bin/bash
and you will find apache2 running when you attach.
/etc/bash.bashrc
will get executed on every bash startup which might cause problems if you later want to interact with the container (e.g. executingdocker exec -ti ID bash
) ... I think this is not the right place to put startup scripts for services like apache2 – Lavabo