Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache not running automatically on docker compose up

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

like image 312
Raheel Avatar asked Jul 09 '16 08:07

Raheel


1 Answers

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).

like image 161
joelnb Avatar answered Sep 25 '22 13:09

joelnb