How To Have The Apache Service Automatically Start When Its Docker Container Is Started

Goal:  To have Apache start when a Docker container starts running do the following (for Docker containers based on RHEL/CentOS/Fedora). 

Prerequisite:  The Docker container was created with the "-p 80:80" flag.  Other port numbers besides 80 will work.  The port numbers do not have to be the same.  They can be any number between 1 and 65535.  If there is no binding of the Docker host's TCP ports to the container, the Apache service will not automatically start even if the steps below are followed.

Method:
1. cd /etc/profile.d/
2.  vi custom.sh
3.  Enter this text and save the changes:
#!/bin/bash
apachectl start

4.  If you create an image off a container with the four steps above, subsequent containers based on the image will not have Apache start the first time after the container is created.  You have to do one of two things for Apache to automatically start thereafter:
     i) login into the container (e.g., docker exec -it <containerID> bash)
     ii) stop and start the container (but not with a semicolon).  Use the docker stop <containerID> and wait. Then use the docker start <containerID>.

Leave a comment

Your email address will not be published. Required fields are marked *