Restart apache on Docker

43,151

Solution 1

It's because you are (correctly) not starting apache as a service when you docker run the container. The line:

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

Starts apache in the foreground.

I'm guessing you are then using docker exec to execute a shell in the container to edit the file and restart apache? If so this would explain why the second time you start apache it complains about the existing process.

I think if you are using containers in this way then you are really missing out on the benefit of containers which comes when you treat them as immutable and keep the data outside the container (either on your host or in volumes) so that you can easily replace the container.

In your case if you need to modify the .htaccess file I think it would be more normal to mount that file into the container by using a command like:

docker run -d --name apache -v $(pwd)/.htaccess:/path/to/.htaccess -p 80:80 image:tag

Then if you have to change the file and need to restart apache you can use:

docker restart apache

Although it may be worth investigating the suggestion from Charlotte Dunois that you might not even need to restart apache.

Solution 2

Actually you don't need to restart Apache in order to apply changes defined in .htaccess - those are applied during runtime. If you're modifying apache config file (like virtual host definition or something in httpd.conf) you can also reload config without restarting apache using

sudo /etc/init.d/apache2 reload
Share:
43,151
Tyler Hilbert
Author by

Tyler Hilbert

Updated on January 07, 2020

Comments

  • Tyler Hilbert
    Tyler Hilbert over 4 years

    I am trying to update my .htaccess file on a Docker container. After updating the file I need to restart Apache. Whenever I try to restart Apache: with the command service apache2 restart I get the following error:

    (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs Action 'start' failed. The Apache error log may have more information. ...fail!

    When I got to the error log it doesn't have any new errors. This is what my Dockerfile looks like:

        FROM ubuntu:12.04
    
    # Install dependencies
    RUN apt-get update -y
    RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql php5-curl vim
    
    # Install app
    RUN rm -rf /var/www/ *
    ADD src /var/www
    
    # Configure apache
    RUN a2enmod rewrite
    RUN chown -R www-data:www-data /var/www
    ENV APACHE_RUN_USER www-data
    ENV APACHE_RUN_GROUP www-data
    ENV APACHE_LOG_DIR /var/log/apache2
    
    EXPOSE 80
    
    CMD ["/usr/sbin/apache2", "-D",  "FOREGROUND"]