Docker PHP + Apache deployment on Heroku crashes

陌路散爱 提交于 2021-02-07 08:12:26

问题


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.

回答1:


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



回答2:


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




回答3:


Answer is simple, you can't bind to a specific port you should use $PORT env variable. More about this thread here.



来源:https://stackoverflow.com/questions/53884647/docker-php-apache-deployment-on-heroku-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!