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.
3
Answers
Answer is simple, you can’t bind to a specific port you should use $PORT env variable. More about this thread here.
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.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:
For example in my ports.conf the line
is changed to
And in my Dockerfile:
Works fine so far