Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker PHP + Apache deployment on Heroku crashes

I am trying to deploy a docker container and although I have tried several options, it always crashes. On local in works fine, on port 8080.

Right now, I am using PHP + Apache.

My folder herarchy looks like this:

docker-compose.yml
Dockerfile 
www
   .htaccess
   index.php

My Dockerfile is this one:

FROM php:7.1-apache
COPY www /var/www/html
RUN a2enmod rewrite
RUN a2enmod lbmethod_byrequests
RUN service apache2 restart
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

My docker-composer.yml:

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: myproject
    ports:
      - 8080:80

And the .htaccess:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

When I check the Heroku logs (heroku logs --tail), this is what I see:

Starting process with command `/usr/sbin/apache2ctl -D FOREGROUND`
State changed from starting to crashed
Process exited with status 1
(13)Permission denied: AH00072: make_sock: could not bind to address [::]:80
(13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
Action '-D FOREGROUND' failed.
like image 655
anexo Avatar asked Dec 21 '18 12:12

anexo


Video Answer


2 Answers

I recently solved this issue myself here in case you ever want to try Heroku again :)

Essentially, I used the Docker CMD statement to change the apache port configs at runtime.

CMD sed -i "s/80/$PORT/g" /etc/apache2/sites-enabled/000-default.conf /etc/apache2/ports.conf && docker-php-entrypoint apache2-foreground
like image 51
Caleb Gray Avatar answered Sep 30 '22 02:09

Caleb Gray


The accepted answer by @caleb-gray stopped working in September 2019 for me but I came up with an alternative solution:

Instead of replacing the port values in the Dockerfile I replaced them in the original apache2 .conf files with the env variables and then copied them over to the Docker image. That also means the .conf files are now part of my repository (I copied them from the running Docker container).

My folder hierarchy:

Dockerfile
apache-config/
  ports.conf
  000-default.conf

For example in my ports.conf the line

Listen 80

is changed to

Listen ${PORT}

And in my Dockerfile:

COPY ./apache-config/ports.conf /etc/apache2/ports.conf                                
COPY ./apache-config/000-default.conf /etc/apache2/sites-available/000-default.conf    
CMD docker-php-entrypoint apache2-foreground

Works fine so far

like image 29
perelin Avatar answered Sep 30 '22 02:09

perelin