Docker Unable to connect to localhost

226 Views Asked by At

I'm trying to dockerise a symfony2 application. The container is up and running without any errors. However, I'm getting 'An error occurred during a connection to localhost' when I hit http://localhost:8081 in the browser

docker-compose.yml

version: "3.8"

services:
  app:
    container_name: "${PROJECT_NAME}"
    build:
      context: .
      dockerfile: ./Dockerfile
    restart: 'always'
    ports:
      - 8081:80
    volumes:
      - .:/var/www/html
      - ${LOG_DIR-./logs/apache2}:/var/log/apache2

Dockerfile

FROM php:7.0-apache
RUN a2enmod rewrite
COPY ./000-default.conf /etc/apache2/sites-available/000-default.conf
// installing php extensions / libraries and composer .. 
EXPOSE 80
CMD ["apache2-foreground"]

000-default.conf

Listen 80

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /var/www/html/web
    <Directory /var/www/html/web>
        EnableSendfile Off
        AllowOverride None
        Order Allow,Deny
        Allow from All

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app_dev.php [QSA,L]
        </IfModule>
    </Directory>
</VirtualHost>


1

There are 1 best solutions below

0
fam On

I generated ssl certificates(referred to https://medium.com/@nh3500/how-to-create-self-assigned-ssl-for-local-docker-based-lamp-dev-environment-on-macos-sierra-ab606a27ba8a) and updated the files as below

000-default.conf

// added this to my existing 000-default.conf (pls refer to the question)
<VirtualHost *:443>
    DocumentRoot "/var/www/html/web"
    ServerName localhost
    SSLEngine on
    SSLCertificateFile "/etc/apache2/ssl/server.crt"
    SSLCertificateKeyFile "/etc/apache2/ssl/server.key"
    <Directory /var/www/html/web>
        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app_dev.php [QSA,L]
        </IfModule>
    </Directory>
</VirtualHost>

docker-compose.yml

    ports:
      - "8081:80"
      - "8082:443"

Dockerfile

FROM php:7.0-apache
COPY server.crt /etc/apache2/ssl/server.crt
COPY server.key /etc/apache2/ssl/server.key
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY php.ini /usr/local/etc/php/php.ini

RUN a2enmod rewrite
RUN a2enmod ssl
// installing php libraries 
// composer install
// EXPOSE ports
CMD ["apache2-foreground"]