Docker4drupal: Drupal Composer project : web root inside project subdir

Created on 31 Jul 2016  ยท  13Comments  ยท  Source: wodby/docker4drupal

Linux Mint 18 (Ubuntu 16), Composer version 1.2.0, Docker version 1.12.0, docker-compose version 1.8.0

Using https://github.com/drupal-composer/drupal-project (after composer install & also update + sudo docker-compose up -d) I got :

Warning: require(/var/www/html/../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/autoload.php on line 16

Fatal error: require(): Failed opening required '/var/www/html/../vendor/autoload.php' (include_path='.:') in /var/www/html/autoload.php on line 16

Modifying docker-compose.yml, I replaced :

  php:
    image: wodby/drupal-php:7.0 # Allowed: 7.0, 5.6.
    environment:
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      PHP_XDEBUG_ENABLED: 0 # Set 1 to enable.
    volumes:
      - ./:/var/www/html

by :

...
    volumes:
      - ./web:/var/www/html

Then :

sudo docker-compose stop
sudo docker-compose up -d

Accessing http://localhost:8000/ or http://localhost:8000/web I got File not found....

Here's the project tree :

./
โ”œโ”€โ”€ composer.json
โ”œโ”€โ”€ composer.lock
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ docker-runtime/
โ”‚ย ย  โ””โ”€โ”€ mariadb/
โ”œโ”€โ”€ drush/
โ”‚ย ย  โ””โ”€โ”€ ...
โ”œโ”€โ”€ scripts/
โ”‚ย ย  โ””โ”€โ”€ ...
โ”œโ”€โ”€ vendor/
โ”‚ย ย  โ””โ”€โ”€ ...
โ”œโ”€โ”€ web/
โ”‚ย ย  โ”œโ”€โ”€ autoload.php
โ”‚ย ย  โ”œโ”€โ”€ core/
โ”‚ย ย  โ”œโ”€โ”€ index.php
โ”‚ย ย  โ”œโ”€โ”€ modules/
โ”‚ย ย  โ”œโ”€โ”€ profiles/
โ”‚ย ย  โ”œโ”€โ”€ robots.txt
โ”‚ย ย  โ”œโ”€โ”€ sites/
โ”‚ย ย  โ”œโ”€โ”€ themes/
โ”‚ย ย  โ”œโ”€โ”€ update.php
โ”‚ย ย  โ””โ”€โ”€ web.config
โ””โ”€โ”€ wodby.yml

What's the proper way to run a project which "web" root is inside a subdirectory ?

question

Most helpful comment

Use NGINX_SERVER_ROOT environment variable, which is by default /var/www/html, e.g. for projects based on composer template it will be /var/www/htm/web. There's no need to do anything with PHP image.

All 13 comments

Hi, Paul.
I've integrated NGINX_DOCROOT environment variable to drupal-nginx container.
So, the proper way is:

  php:
    ...
    volumes:
      - ./:/var/www/html

  nginx:
    ...
    environment:
      ...
      NGINX_DOCROOT: web
      ...
    volumes_from:
      - php
    ...

Is there a chance a regression could have happened ?

Steps :

  • (reboot)
  • cd ~/path/to/project
  • sudo docker-compose up -d
  • http://localhost:8000 gives File not found.

Then :

  • sudo docker-compose stop
  • Replace contents of docker-compose.yml with latest version from github.com/Wodby/docker4drupal
  • sudo docker-compose up -d
  • http://localhost:8000 gives File not found.

Last test :

  • sudo docker-compose stop
  • sudo docker-compose up --build
  • http://localhost:8000 gives File not found.

Details :

screenshot-regression-2016-08-24--docker-compose yml
screenshot-regression-2016-08-24--docker-compose--build

Hi, @Paulmicha

I've tested and it worked well for me. Could you provide output of the following commands:

docker-compose config
docker-compose exec nginx ls -lah /var/www/html/
docker-compose exec nginx ls -lah /var/www/html/web
docker-compose exec php ls -lah /var/www/html/
docker-compose exec php ls -lah /var/www/html/web

โ€ข sudo docker-compose config :

networks: {}
services:
  mailhog:
    image: mailhog/mailhog
    ports:
    - 8002:8025
  mariadb:
    environment:
      MYSQL_DATABASE: drupal
      MYSQL_PASSWORD: drupal
      MYSQL_RANDOM_ROOT_PASSWORD: 1
      MYSQL_USER: drupal
    image: wodby/drupal-mariadb
    volumes:
    - /home/paul/projects/cuisine/docker-runtime/mariadb:/var/lib/mysql:rw
  nginx:
    environment:
      DRUPAL_VERSION: 8
      NGINX_DOCROOT: web
      NGINX_SERVER_NAME: localhost
      NGINX_UPSTREAM_NAME: php
    image: wodby/drupal-nginx
    ports:
    - 8000:80
    volumes_from:
    - service:php:rw
  php:
    environment:
      PHP_DOCROOT: web
      PHP_HOST_NAME: localhost:8000
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      PHP_SITE_NAME: dev
      PHP_XDEBUG_ENABLED: 0
    image: wodby/drupal-php:7.0
    volumes:
    - /home/paul/projects/cuisine:/var/www/html:rw
  pma:
    environment:
      PHP_MAX_INPUT_VARS: 1G
      PHP_UPLOAD_MAX_FILESIZE: 1G
      PMA_HOST: mariadb
      PMA_PASSWORD: drupal
      PMA_USER: drupal
    image: phpmyadmin/phpmyadmin
    ports:
    - 8001:80
version: '2.0'
volumes: {}

โ€ข sudo docker-compose exec nginx ls -lah /var/www/html/ :

total 380
drwxr-xr-x    8 1000     1000        4.0K Aug 25 14:25 .
drwxr-xr-x    3 root     root        4.0K Jul 26 09:54 ..
drwxr-xr-x    8 1000     1000        4.0K Jul 30 21:28 .git
-rw-r--r--    1 1000     1000         664 Jul 30 16:55 .gitignore
-rw-r--r--    1 1000     1000        1.0K Jul 30 13:43 .travis.yml
-rw-r--r--    1 1000     1000       17.6K Jul 30 13:43 LICENSE
-rw-r--r--    1 1000     1000        4.6K Jul 30 16:28 README.md
-rw-r--r--    1 1000     1000        2.1K Jul 30 21:23 composer.json
-rw-r--r--    1 1000     1000      192.8K Jul 30 21:28 composer.lock
-rw-r--r--    1 1000     1000        1.1K Aug 25 14:22 docker-compose-config.txt
-rw-r--r--    1 1000     1000        2.1K Aug 24 21:47 docker-compose.yml
drwxr-xr-x    3 root     root        4.0K Jul 30 17:58 docker-runtime
drwxr-xr-x    2 1000     1000        4.0K Jul 30 13:43 drush
-rw-r--r--    1 1000     1000         481 Jul 30 13:43 phpunit.xml.dist
drwxr-xr-x    4 1000     1000        4.0K Jul 30 16:02 scripts
drwxr-xr-x   41 1000     1000        4.0K Jul 30 21:28 vendor
drwxr-xr-x    7 1000     1000        4.0K Jul 30 15:18 web
-rw-r--r--    1 1000     1000          97 Jul 30 16:02 wodby.yml

โ€ข sudo docker-compose exec nginx ls -lah /var/www/html/web :

total 168
drwxr-xr-x    7 1000     1000        4.0K Jul 30 15:18 .
drwxr-xr-x    8 1000     1000        4.0K Aug 25 14:25 ..
-rw-r--r--    1 1000     1000         948 Jul 30 15:18 .csslintrc
-rw-r--r--    1 1000     1000         350 Jul 30 15:18 .editorconfig
-rw-r--r--    1 1000     1000         185 Jul 30 15:18 .eslintignore
-rw-r--r--    1 1000     1000          36 Jul 30 15:18 .eslintrc
-rw-r--r--    1 1000     1000        3.3K Jul 30 15:18 .gitattributes
-rw-r--r--    1 1000     1000        7.7K Jul 30 15:18 .htaccess
-rw-rw-rw-    1 1000     1000         382 Jul 30 15:18 autoload.php
drwxr-xr-x   12 1000     1000        4.0K Jul 30 15:16 core
-rw-r--r--    1 1000     1000         549 Jul 30 15:18 index.php
drwxr-xr-x    2 1000     1000        4.0K Jul 30 15:18 modules
drwxr-xr-x    2 1000     1000        4.0K Jul 30 15:18 profiles
-rw-r--r--    1 1000     1000        1.6K Jul 30 15:18 robots.txt
drwxr-xr-x    3 1000     1000        4.0K Jul 30 16:22 sites
drwxr-xr-x    2 1000     1000        4.0K Jul 30 15:18 themes
-rw-r--r--    1 1000     1000         554 Jul 30 15:18 update.php
-rw-r--r--    1 1000     1000        4.2K Jul 30 15:18 web.config

โ€ข sudo docker-compose exec php ls -lah /var/www/html/ :

total 404
drwxr-xr-x    8 1000     1000        4.0K Aug 25 14:27 .
drwxr-xr-x    3 www-data www-data    4.0K Jul 27 11:41 ..
drwxr-xr-x    8 1000     1000        4.0K Jul 30 21:28 .git
-rw-r--r--    1 1000     1000         664 Jul 30 16:55 .gitignore
-rw-r--r--    1 1000     1000        1.0K Jul 30 13:43 .travis.yml
-rw-r--r--    1 1000     1000       17.6K Jul 30 13:43 LICENSE
-rw-r--r--    1 1000     1000        4.6K Jul 30 16:28 README.md
-rw-r--r--    1 1000     1000        2.1K Jul 30 21:23 composer.json
-rw-r--r--    1 1000     1000      192.8K Jul 30 21:28 composer.lock
-rw-r--r--    1 1000     1000        1.1K Aug 25 14:22 docker-compose-config.txt
-rw-r--r--    1 1000     1000        2.1K Aug 24 21:47 docker-compose.yml
drwxr-xr-x    3 root     root        4.0K Jul 30 17:58 docker-runtime
drwxr-xr-x    2 1000     1000        4.0K Jul 30 13:43 drush
-rw-r--r--    1 1000     1000         481 Jul 30 13:43 phpunit.xml.dist
drwxr-xr-x    4 1000     1000        4.0K Jul 30 16:02 scripts
drwxr-xr-x   41 1000     1000        4.0K Jul 30 21:28 vendor
drwxr-xr-x    7 1000     1000        4.0K Jul 30 15:18 web
-rw-r--r--    1 1000     1000          97 Jul 30 16:02 wodby.yml

โ€ข sudo docker-compose exec php ls -lah /var/www/html/web :

total 168
drwxr-xr-x    7 1000     1000        4.0K Jul 30 15:18 .
drwxr-xr-x    8 1000     1000        4.0K Aug 25 14:27 ..
-rw-r--r--    1 1000     1000         948 Jul 30 15:18 .csslintrc
-rw-r--r--    1 1000     1000         350 Jul 30 15:18 .editorconfig
-rw-r--r--    1 1000     1000         185 Jul 30 15:18 .eslintignore
-rw-r--r--    1 1000     1000          36 Jul 30 15:18 .eslintrc
-rw-r--r--    1 1000     1000        3.3K Jul 30 15:18 .gitattributes
-rw-r--r--    1 1000     1000        7.7K Jul 30 15:18 .htaccess
-rw-rw-rw-    1 1000     1000         382 Jul 30 15:18 autoload.php
drwxr-xr-x   12 1000     1000        4.0K Jul 30 15:16 core
-rw-r--r--    1 1000     1000         549 Jul 30 15:18 index.php
drwxr-xr-x    2 1000     1000        4.0K Jul 30 15:18 modules
drwxr-xr-x    2 1000     1000        4.0K Jul 30 15:18 profiles
-rw-r--r--    1 1000     1000        1.6K Jul 30 15:18 robots.txt
drwxr-xr-x    3 1000     1000        4.0K Jul 30 16:22 sites
drwxr-xr-x    2 1000     1000        4.0K Jul 30 15:18 themes
-rw-r--r--    1 1000     1000         554 Jul 30 15:18 update.php
-rw-r--r--    1 1000     1000        4.2K Jul 30 15:18 web.config

Note that in non-Docker context, I usually set the following permissions (with ownership : project user / web server group, e.g. www-foobar:www-data) :

  • Files : 0640
  • Folders : 0750
  • Writeable files : 0660
  • Writeable folders : 1771
  • Write-protected files : 0540
  • Write-protected folders : 0550

โ†’ What's the recommendation for projects using docker4drupal ?

It looks okay. I didn't see problems in your configuration.

What's the recommendation for projects using docker4drupal ?

Normally, you should only fix permissions on the files directory.

# 82 == www-data
sudo chgrp -R 82 sites/default/files
sudo chmod -R 775 sites/default/files

Can you make a new setup of docker4drupal (without any code)? And ensure you will see the PHP info page when you will being on http://localhost:8000. Then set web for NGINX_DOCROOT and PHP_DOCROOT, down/up compose, and you must see the same PHP info page.

Intial test :

cd ~/projects
git clone https://github.com/Wodby/docker4drupal.git
cd docker4drupal
sudo docker-compose up -d
sudo docker-compose ps

Result :

         Name                        Command               State                Ports               
---------------------------------------------------------------------------------------------------
docker4drupal_mailhog_1   /MailHog                         Up      1025/tcp, 0.0.0.0:8002->8025/tcp 
docker4drupal_mariadb_1   docker-entrypoint.sh mysqld      Up      3306/tcp                         
docker4drupal_nginx_1     /bin/sh -c "docker-entrypo ...   Up      443/tcp, 0.0.0.0:8000->80/tcp    
docker4drupal_php_1       /bin/sh -c "docker-entrypo ...   Up      9000/tcp                         
docker4drupal_pma_1       /run.sh                          Up      0.0.0.0:8001->80/tcp

http://localhost:8000 is working (shows PHP info page). Details :

$_SERVER['PWD'] : /var/www/html
$_SERVER['DOCUMENT_ROOT'] : /var/www/html

Then, after modification in docker-compose.yml :

screenshot-test-2016-08-29--step02

sudo docker-compose stop
sudo docker-compose up -d

โ†’ http://localhost:8000 is still working (shows PHP info page).
However, these did _not_ change :

$_SERVER['PWD'] : /var/www/html
$_SERVER['DOCUMENT_ROOT'] : /var/www/html

โ†’ shouldn't it be /var/www/html/web, given that docker-compose.yml had PHP_DOCROOT: web and NGINX_DOCROOT: web ?

I found a workaround, using a different volumes declaration in php :
./web:/var/www/html

screenshot-test-2016-08-29--step03

... but it's not playing nice with Composer :

Warning: require(/var/www/html/../vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/autoload.php on line 16

Fatal error: require(): Failed opening required '/var/www/html/../vendor/autoload.php' (include_path='.:') in /var/www/html/autoload.php on line 16

http://localhost:8000 is still working (shows PHP info page).

Please, check the values of PWD and DOCUMENT_ROOT environment variables in the PHP info page.
Here is my version:
image

Also, you can try to debug the /usr/local/bin/docker-entrypoint.sh file in the nginx container. All magic happens there.

...
# Configure docroot.
if [ -n "$NGINX_DOCROOT" ]; then
    sed -i 's@root /var/www/html/;@'"root /var/www/html/${NGINX_DOCROOT};"'@' /etc/nginx/conf.d/*.conf
fi
...

I'm going to close the issue due to no activity.

Hi!
Do you find a solution.. I have the same problem. And cannot find the env variable NGINX_DOCROOT. docker4drupal does not allow to configure that variable.

any idea?

It seems the relative path option disappeared in last update.

This used to work (@ e146a13 - 13 Jan 2017) - check the lines with comment _"Relative path inside the /var/www/html/ directory"_ :

services:

  # ...

  php:
    image: wodby/drupal-php:7.0-1.0.0
    # image: wodby/drupal-php:5.6-1.0.0
    environment:
      PHP_SITE_NAME: dev
      PHP_HOST_NAME: localhost:8000
      PHP_DOCROOT: web # Relative path inside the /var/www/html/ directory.
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
    volumes:
      - ./:/var/www/html

  nginx:
    image: wodby/drupal-nginx:1.10-1.1.0
    environment:
      NGINX_SERVER_NAME: localhost
      NGINX_UPSTREAM_NAME: php
      NGINX_DOCROOT: web # Relative path inside the /var/www/html/ directory.
      DRUPAL_VERSION: 8 # Allowed: 7, 8.
    volumes_from:
      - php
    ports:
      - "8000:80"

  # ...

โ†’ What's the equivalent in the new release ?

Use NGINX_SERVER_ROOT environment variable, which is by default /var/www/html, e.g. for projects based on composer template it will be /var/www/htm/web. There's no need to do anything with PHP image.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dcre picture dcre  ยท  6Comments

KN-Bill picture KN-Bill  ยท  3Comments

Vacilando picture Vacilando  ยท  9Comments

quietone1 picture quietone1  ยท  10Comments

featherbelly picture featherbelly  ยท  5Comments