Apache not running automatically on docker compose up

11,021

Judging from the output of your logs the image which is running when you do docker-compose up is not using the CMD you have specified. This is probably because it was built the first time you ran docker-compose up and was not subsequently rebuilt. To get around this try running with docker-compose up --build.

When I built & ran your image (with the docker-compose.yml you provided) I did get apache starting - my output was like:

Successfully built d65dabcc2595
Creating apachenotrunningautomaticallyondockercomposeup38280007_frontend_1
Attaching to apachenotrunningautomaticallyondockercomposeup38280007_frontend_1
frontend_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.25.0.2. Set the 'ServerName' directive globally to suppress this message

I think that starting apache in this way is not best practice given the image you are using though - the recommended way would be with a service file as phusion/baseimage uses a custom init system to get around some potential issues with Docker.

You can follow their recommended way by creating a service file for apache. If you create (in your frontend folder) a script called apache.sh (make sure it is chmod +x) with these contents:

#! /bin/sh

exec /usr/sbin/apache2ctl -D FOREGROUND

And change your Dockerfile to look like this:

FROM phusion/baseimage:0.9.16
MAINTAINER Raheel <[email protected]>

# Apache
RUN apt-get update
RUN apt-get -y install apache2

# PHP
RUN apt-get -y install python-software-properties
RUN add-apt-repository ppa:ondrej/php
RUN apt-get update
RUN apt-get -y --force-yes install php7.0
RUN apt-get -y --force-yes install libapache2-mod-php7.0 php7.0-curl php7.0-json

RUN mkdir /etc/service/apache
ADD apache.sh /etc/service/apache/run
RUN chmod +x /etc/service/apache/run

Then it will use the init system provided.

In answer to your second question I think you have 2 main options:

  1. If you must keep them in a VOLUME & be accessible to both your user on the host (which is presumably the one with uid & gid 1000) then you could ensure that the user which apache runs your app as has the same uid & gid as your user on the host (create another user & tell apache to use that user in your apache config). This will work but will be far less portable to systems where the user on the host is different.

  2. Add this to your Dockerfile and drop the volume options from docker-compose.yml:

    RUN mkdir -p /var/www/html/frontend/
    COPY . /var/www/html/frontend/
    RUN chown -R www-data:www-data /var/www/html/frontend
    

If it were me I would choose option 1 for a development environment (as portability is likely less of an issue) but option 2 for any kind of production deployment (as a large benefit of docker is the immutability of containers).

Share:
11,021
Raheel
Author by

Raheel

A results-driven, user-focused, articulate and analytical Senior Software Engineer who can think “out of the box.” Strong in design and integration problem-solving skills. Skilled in developing business plans, requirements specifications, user documentation, and architectural systems research. Strong written and verbal communications. Interested in a challenging technical track career in an application development environment.

Updated on June 21, 2022

Comments

  • Raheel
    Raheel almost 2 years

    Dockerfile

    FROM phusion/baseimage:0.9.16
    MAINTAINER Raheel <[email protected]>
    
    # Apache
    RUN apt-get update
    RUN apt-get -y install apache2
    
    # PHP
    RUN apt-get -y install python-software-properties
    RUN add-apt-repository ppa:ondrej/php
    RUN apt-get update
    RUN apt-get -y install php7.0
    RUN apt-get -y install libapache2-mod-php7.0 php7.0-curl php7.0-json
    
    CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
    

    docker-composer.yaml

    version: '2'
    services:
      frontend:
        build: ./frontend
        ports:
         - "80:80"
        volumes:
         - ./frontend:/var/www/html/frontend
    

    I am trying to run my laravel app inside docker. I am facing two problems

    1 - When i run docker-compose up, the apache server inside the container doesnot start automatically. Everytime i have to login into the container and do service apache2 start. As per my searching i found the CMD command i wrote in the Dockerfile is how we start apache but not working in my case

    2 - When i login to container and go to my app folder /var/www/html/frontend its user are 1000:1000 something. I want them to be under www-data:www-data. But i do not want this to be added in my Dockerfile since i want to keep my dockerfile only for installing apache and php. How can i achieve this by using docker-compose.yaml or any other way.

    Updated : Docker Container Logs

    *** Running /etc/my_init.d/00_regen_ssh_host_keys.sh...
    *** Running /etc/rc.local...
    *** Booting runit daemon...
    *** Runit started as PID 9
    Jul 10 08:27:33 6d5c09e83a98 syslog-ng[15]: syslog-ng starting up; version='3.5.3'
    

    Thanks

    • BMitch
      BMitch almost 8 years
      If the command doesn't start apache, then what does it do? Difficult to debug without logs and the output of your commands.
    • Raheel
      Raheel almost 8 years
      How can i see logs ? I am on ubuntu 16
    • BMitch
      BMitch almost 8 years
      docker-compose logs frontend
    • Raheel
      Raheel almost 8 years
      This is saying no service frontend, However i tried to see logs of container. Please see the udpated part in question i am pasting there.
  • Raheel
    Raheel almost 8 years
    Tried your solution its giving me frontend_1 | runsv apache: fatal: unable to start ./run: access denied . I already gave chmod +x to run.sh
  • Raheel
    Raheel almost 8 years
    Its strange. I kept my file name start.sh instead of apache.sh it was not working now i exactly named is apache.sh and run again it works. Why is that so ...
  • joelnb
    joelnb almost 8 years
    The reason that is happening is because the new file you created does not have executable permission set (if you run ls -lh <directory-with-scripts> you will see a difference in the permissions). I have edited to add a line which ensures the file is marked executable in the container. I'm glad the explanation was helpful :)