skip to Main Content

I am trying to create a PHP, Apache, MySQL image for a Laravel app using Docker. It’s my first Docker app.

When running docker-compose up -d, I receive the following errors in the browser:

enter image description here


This is my docker-compose.yml file:

version: "3"
services:

webserver:
    build: 
    context: ./bin/webserver
    container_name: 'webserver'
    restart: 'always'
    ports:
    - "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
    - "${HOST_MACHINE_SECURE_HOST_PORT}:443"
    links: 
    - mysql
    volumes: 
    - ${DOCUMENT_ROOT-./www}:/var/www/html
    - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
    - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
    - ${LOG_DIR-./logs/apache2}:/var/log/apache2
    networks:
    - app-network

mysql:
    build: ./bin/mysql
    container_name: 'mysql'
    restart: 'always'
    ports:
    - "${HOST_MACHINE_MYSQL_PORT}:3306"
    volumes: 
    - ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
    - ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
    environment:
    MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    MYSQL_DATABASE: ${MYSQL_DATABASE}
    MYSQL_USER: ${MYSQL_USER}
    MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    networks:
    - app-network

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: 'phpmyadmin'
    links:
    - mysql
    environment:
    PMA_HOST: mysql
    PMA_PORT: 3306
    PMA_USER: ${MYSQL_USER}
    PMA_PASSWORD: ${MYSQL_PASSWORD}
    MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    MYSQL_USER: ${MYSQL_USER}
    MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    ports:
    - '8080:80'
    volumes: 
    - /sessions
    networks:
    - app-network

networks:
    app-network:
        driver: bridge

volumes:
    dbdata:
        driver: local

This is my .env file:

DOCUMENT_ROOT=./vbs-master/public
VHOSTS_DIR=./config/vhosts
APACHE_LOG_DIR=./logs/apache2
PHP_INI=./config/php/php.ini
MYSQL_DATA_DIR=./data/mysql
MYSQL_LOG_DIR=./logs/mysql

HOST_MACHINE_UNSECURE_HOST_PORT=80
HOST_MACHINE_SECURE_HOST_PORT=443

HOST_MACHINE_MYSQL_PORT=3306

MYSQL_ROOT_PASSWORD=rootroot
MYSQL_USER=root
MYSQL_PASSWORD=rootroot
MYSQL_DATABASE=dockertest

This is my bin/webserver/Dockerfile:

FROM php:5.6-apache

RUN apt-get -y update && apt-get upgrade -y

# Install tools && libraries
RUN apt-get -y install --fix-missing apt-utils nano wget dialog 
    build-essential git curl libcurl3 libcurl3-dev zip 
    libmcrypt-dev libsqlite3-dev libsqlite3-0 mysql-client 
    zlib1g-dev libicu-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev 
    && rm -rf /var/lib/apt/lists/*

# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# PHP5 Extensions
RUN docker-php-ext-install curl 
    && docker-php-ext-install tokenizer 
    && docker-php-ext-install json 
    && docker-php-ext-install mcrypt 
    && docker-php-ext-install pdo_mysql 
    && docker-php-ext-install pdo_sqlite 
    && docker-php-ext-install mysqli 
    && docker-php-ext-install zip 
    && docker-php-ext-install -j$(nproc) intl 
    && docker-php-ext-install mbstring 
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ 
    && docker-php-ext-install -j$(nproc) gd 
    && pecl install xdebug-2.5.5 && docker-php-ext-enable xdebug 
    && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/php.ini

# Enable apache modules
RUN a2enmod rewrite headers

EXPOSE 80

ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

File directory tree:

enter image description here


My config/vhosts/default.conf file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "/var/www/html"
    ServerName localhost
    <Directory "/var/www/html/">
        AllowOverride all
    </Directory>
</VirtualHost>

2

Answers


  1. You need to set to DOCUMENT_ROOT=./vbs-master/, cause otherwise your container will have access only to the public folder, but the Laravel project needs to access the parent directory.

    You need to change the Apache configuration to look for index.php inside /var/www/html/public then.

    Login or Signup to reply.
  2. As @thiago suggested, you need to mount all your application to your container, so change your webserver service as follow:

    webserver:
        build: 
        context: ./bin/webserver
        container_name: 'webserver'
        restart: 'always'
        ports:
        - "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
        - "${HOST_MACHINE_SECURE_HOST_PORT}:443"
        links: 
        - mysql
        volumes: 
        - ${APP_ROOT-./app}:/var/www/html # Here you may mount the root project of your application
        - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
        - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
        - ${LOG_DIR-./logs/apache2}:/var/log/apache2
        networks:
        - app-network
    

    And add this to your .env file

    APP_ROOT=./vbs-master
    

    Last but not least, point your DocumentRoot to public folder, make a change to your config/vhosts/default.conf file

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot "/var/www/html/public"
        ServerName localhost
        <Directory "/var/www/html/public">
            AllowOverride all
        </Directory>
    </VirtualHost>
    

    NOTE: Make sure you’ve executed composer install before running your application.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search