1

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>
4
  • Does your config/app.php exist? Commented Feb 28, 2019 at 15:19
  • No it does not. Commented Feb 28, 2019 at 15:29
  • Could you post a screenshot of your file directory tree please? Commented Feb 28, 2019 at 17:18
  • Edited my post to include the screenshot @party-ring.The problem appear to be related to the public in DOCUMENT_ROOT=./vbs-master/public. I I take that out and navigate to localhost\public instead of localhost, it appears to work, although causes other issues. Commented Feb 28, 2019 at 17:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you, I have applied the first change. With your explanation, it makes a lot more sense now! Where do I need to apply the second change? Would that be in config/vhosts/default.conf ? Could you please add what that file needs to look like?
It is probably in an XML file with .conf extension inside ./config/vhosts
Yes, I have edited my question to show what that file currently looks like (its at the very bottom). Do I need to change both DocumentRoot and Directory? Is there anything else in that file that needs changing?
I have made the second change and now I see a gray/white page with the error message: InvalidArgumentException in FileViewFinder.php line 137: View [users.signin] not found. And furthermore: in FileViewFinder.php line 137 at FileViewFinder->findInPaths('users.signin', array('/Users/MY-HOST-USERNAME/Documents/Docker/laravel-test-three/vbs-master/resources/views')) in FileViewFinder.php line 79 How does it know my host user name if it runs in a container?
Could your browser be reaching your application through some other apache running in the host? The path you showed shouldn't exist in the container. You can use these commands to have a clearer picture of your setup: 1. docker ps to show you all your running containers; and 2. docker exec -it [container-name] bash to open a terminal "inside" the container. There you can inspect the container file system.
|
1

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.

10 Comments

Thank you so much for your help with this. I have done everything except the yellow note (already did that previously). Now the error is gone and I instead see the Apache "Index of /" page, listing all the files and folders within the vbs-master directory. So that's readme.md, public and so on. What do I need to do next?
Hi, @Ben. Have you already update your vhost config file? Make sure it points to /var/www/html/public directory.
I have updated my config/vhosts/default.conf, if that is what you are reffing to. Just noticed: If I click on the public folder, it simply shows the same error message as before: InvalidArgumentException in FileViewFinder.php line 137: View [users.signin] not found.
Again, using my host user name: in FileViewFinder.php line 137 at FileViewFinder->findInPaths('users.signin', array('/Users/HOST-USER-NAME/Documents/Docker/laravel-test-three/vbs-master/resources/views')) in FileViewFinder.php line 79
Also, could you please be more specific regarding - ${APP_ROOT-./app}:/var/www/html # Here you may mount the root project of your application in docker-compose.yml?. Why did you replace DOCUMENT_ROOT with APP_ROOT?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.