Auto start apache when running a container

Hi

Can anyone tell me how to get this working with supervisord. Below is my unit file in /etc/supervisor/conf.d/


 [program:apache2]
    command=/usr/sbin/apache2ctl -D FOREGROUND
    autostart=true
    autorestart=true
    user=root

However apache does not start with CMD supervisord -c /etc/supervisor/supervisord.conf

Below is my docker file that starts it but I then have to open a new shell and run

sudo docker exec -it my_container_id /bin/bash to get an active shell

here is my Dockerfile

FROM debian:latest

# Install reauired packages
RUN apt-get update && apt-get install -y mc libmariadb-dev-compat libmariadb-dev apache2 build-essential git curl wget cron vim

# Make sure apache has a failover ServerName
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

# Add config for our VirtualHost and create the webRoot folders (DocumentRoot)

COPY vhost.conf /etc/apache2/sites-enabled/000-default.conf
RUN mkdir -p /var/www/html/TestBlog/admin/
COPY index.pl /var/www/html/TestBlog/
COPY .blogcreds /etc/
# COPY startapache.conf /etc/supervisor/conf.d/apache.conf

# Ensure all the correct apache nodules are loaded and enable apache service

RUN a2enmod cgid rewrite.load
RUN update-rc.d apache2 enable

# Install the needed perl modukes
# ToDo: Do these perl modules really need to be global?
RUN cpan install DBI DBD::MariaDB CGI CGI:Carp Moose Template Scalar:MoreUtils

# Clone the git repo, copy the files and clean up
RUN cd /tmp

# Workaround to force real time data from gitlab without having to install all the perl modules each time
ARG CACHE_DATE=2016-01-01

RUN git clone https://gitlab.com/scothost/PerlBlog.git
RUN mv PerlBlog/*.pm /etc/perl/
RUN cp -r PerlBlog/* /var/www/html/TestBlog/
EXPOSE 80
CMD /usr/sbin/apache2ctl -D FOREGROUND

Why does the comand work in the Docker file but not in supervisord.conf ?

I don’t see the part where you install Supervisord. If I just missed it or it is installed as a dependency of any of the installed packages, I would check the supervisord logs and maybe even the httpdlogs to see if any of those shows something interesting.

It wuld be great if you could share the docker command that you use to run the container when you use supervisord.

I have an example by the way i which I used httpd started by supervisord: LINK

but it seems I almost did the same, except I used the httpd command directly instead of apache2ctl.

I would alos point out that your Dockerfile uses the shell syntax in the CMD instruction which is not recommended usually.

Shell syntax means you define the command as you would run in a shell, so it will start a shell. and run the command in that shell so you will have problems with signals like the stop signal and your processes will be forcefully stopped after 10 seconds without a proper shutdown and cleanup. So this would be better:

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

I’m afraid I don’t understand this part. You would always need to use a command like this to get a shell in the container. Or do you mean you have tu manually start the server in that shell? What does supervisorctl status show in the container? Does it recognize the server process? If not, could be a problem with finding the configuration file. This is how I had to enable laoding files in the conf.d directory:

You probably have a good reason to install Apache HTTPD in your own image, but just in case you didn’t know, or someone in the future reads this topic, I have to share that using the official httpd image could usually be a better choice: https://hub.docker.com/_/httpd

And you can often run other services in separate containers.

yes I was hoping I could run it in the background and keep using the shell I used to start the container

docker build  --build-arg CACHE_DATE="$(date)" -t blog:Release-3 . 
docker  run -it -p 80:80 --add-host blog:192.168.2.4 docker.io/library/blog:Release-3

You can always use the shell when there is one in the image, but a container is not a virtual machine that you start, get a shell and start running processes. In a vitual machine, there is usually systemd with PID 1. In a Docker container, you run your app as PID 1. It could be httpd or supervisord, but there will always be a process in the foreground, so I’m not sure what yoou would win with supervisord here, unless you really want to run multiple services in a single container communicating with eachother

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.