Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker CentOS image does not auto start httpd

I'm trying to run a simple Docker image with Apache and a PHP program. It works fine if I run

docker run -t -i -p 80:80 my/httpd /bin/bash

then manually start Apache

service httpd start

however I cant get httpd to start automatically when running

docker run -d -p 80:80 my/httpd

Apache will startup then container exists. I have tried a bunch of different CMDs in my docker file

CMD /etc/init.d/httpd start
CMD ["service" "httpd" "start"]
CMD ["/bin/bash", "/etc/init.d/httpd start"]
ENTRYPOINT /etc/init.d/httpd CMD start
CMD ./start.sh

start.sh is

#!/bin/bash
/etc/init.d/httpd start

However every-time docker instance will exist after apache starts

Am I missing something really obvious?

like image 503
dwitz Avatar asked Jan 22 '14 10:01

dwitz


People also ask

How do I start a service in a docker container?

Run Docker Container as a Service Docker team recommends to use cross-platform built-in restart policy for running container as a service. For this, configure your docker service to start on system boot and simply add parameter --restart unless-stopped to the docker run command that starts YouTrack.

How do you make a container always run?

Dockerfile Command to Keep the Container Running Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.


2 Answers

You need to run apache (httpd) directly - you should not use init.d script.

Two options:

  1. you have to run apache in foreground: /usr/sbin/apache2 -DFOREGROUND ... (or /usr/sbin/httpd in CentOS)
  2. you have to start all services (including apache configured as auto-run) by executing /sbin/init as entrypoint.
like image 102
Jiri Avatar answered Oct 17 '22 19:10

Jiri


Add this line in the bottom of your Dockerfile to run Apache in the foreground on CentOS

ENTRYPOINT ["/usr/sbin/httpd", "-D", "FOREGROUND"]
like image 33
Hemerson Varela Avatar answered Oct 17 '22 17:10

Hemerson Varela