running apache in docker

2019-07-23 18:41发布

Ok, I have exhausted pretty much all threads and articles, but still cant get my apache webserver to run in standalone mode on Centos Docker Container.

Here is my simplified Dockerfile

# install apache
RUN yum -y install httpd

# start the webserver
ADD startservice /startservice
RUN chmod 775 /startservice

EXPOSE 80

CMD ["/startservice"]

My starservice script just has

#!/usr/bin/sh
service httpd start

I can build fine, but, cant seem to run the container in daemon/standalone mode. How do I do that?

I am using this to run the container in standalone mode

docker run -p 80:80 -d -t webserver

I have to log onto the container and start the service for the webserver to run.

docker run -p 80:80 -i -t webserver bash
service httpd start

1条回答
Evening l夕情丶
2楼-- · 2019-07-23 18:53

This is a classic docker issue. The process you start must execute in the foreground, otherwise the container simply stops.

So, to be able to do so the following can be used in your startservice script:

#!/usr/bin/sh
service httpd start

# Tail the log file
tail -f /var/log/httpd/access_log 

# Alternatively, you can tail any file or even /dev/null
#tail -f /dev/null

Note that there are also other ways of fixing this. One way is to use supervisord that keeps your processes alive. The supervisord-approach is cleaner and les hackish than the tail -f-approach and I would personally prefer that alternative.

Another alternative is simply that you do not start httpd as a service but instead provide the -DFOREGROUND parameter. This will make httpd be attached to the shell (and not fork off to a background process).

/usr/sbin/httpd -DFOREGROUND

For more info on http in foreground mode, check this question.

查看更多
登录 后发表回答