0
votes
  1. I have been trying to install drupal using the official image from docker hub. I created a new folder in my D directory, for my Drupal project and created a docker-compose.yml file.
  Drupal with PostgreSQL
 
  Access via "http://localhost:8080"
    (or "http://$(docker-machine ip):8080" if using docker-machine)
 
  During initial Drupal setup,
  Database type: PostgreSQL
  Database name: postgres
  Database username: postgres
  Database password: example
  ADVANCED OPTIONS; Database host: postgres
 
 version: '3.1' services:
 
  drupal:
     image: drupal:8-apache    ports:
       - 8080:80
      volumes:
       - /var/www/html/modules
       - /var/www/html/profiles
       - /var/www/html/themes
         this takes advantage of the feature in Docker that a new anonymous
        volume (which is what we're creating here) will be initialized with the
         existing content of the image at the same location
        - /var/www/html/sites
      restart: always
      postgres:
      image: postgres:10
      environment:
       POSTGRES_PASSWORD: example
     restart: always

When I ran the docker-compose up -d command in a terminal from within the folder which constrong texttained docker-compose.yml file, my drupal container and its databse were successfully installed and running and I was able to access the site from http://localhost:8080 but I couldnt find their core files in the folder. It was just docker-compose.yml file in the folder.

  1. I then removed the whole docker container and began with a fresh installation again with by editing the volume section in the docker-compose.yml file to point to the directory and folder where I want the core files of drupal to be populated. Example D:/My Project/Drupal Project.
  Drupal with PostgreSQL
 
  Access via "http://localhost:8080"
    (or "http://$(docker-machine ip):8080" if using docker-machine)
 
  During initial Drupal setup,
  Database type: PostgreSQL
  Database name: postgres
  Database username: postgres
  Database password: example
  ADVANCED OPTIONS; Database host: postgres
 
 version: '3.1'
 
 services:
 
   drupal:
     image: drupal:latest
     ports:
       - 8080:80
     volumes:
       - d:\projects\drupalsite/var/www/html/modules
       - d:\projects\drupalsite/var/www/html/profiles
       - d:\projects\drupal/var/www/html/themes
        this takes advantage of the feature in Docker that a new anonymous
        volume (which is what we're creating here) will be initialized with the
        existing content of the image at the same location
       - d:\projects\drupalsite/var/www/html/sites
     restart: always
 
   postgres:
     image: postgres:10
     environment:
       POSTGRES_PASSWORD: example
     restart: always

When I ran the docker-compose.yml command I received the error as shown below.

 Container drupalsite_postgres_1  Created                                                                          3.2s
 - Container drupalsite_drupal_1    Creating                                                                         3.2s
 Error response from daemon: invalid mount config for type "volume": invalid mount path: 'z:/projects/drupalsite/var/www/html/sites' mount path must be absolute
 PS Z:\Projects\drupalsite>

Please help me find a solution to this.

1

1 Answers

0
votes

If these directories contain your application, they probably shouldn't be in volumes: at all. Create a file named Dockerfile that initializes your custom application:

FROM drupal:8-apache
COPY modules/ /var/www/html/modules/
COPY profiles/ /var/www/html/profiles/
COPY themes/ /var/www/html/themes/
COPY sites/ /var/www/html/sites/
# EXPOSE, CMD, etc. come from the base image

Then reference this in your docker-compose.yml file:

version: '3.8'
services:
  drupal:
    build: .  # instead of image:
    ports:
      - 8080:80
    restart: always
    # no volumes:
  postgres:
    image: postgres:10
    environment:
      POSTGRES_PASSWORD: example
    restart: always
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:

If you really want to use volumes: here, there are three forms of that directive. The form you have in the question with just a path creates an anonymous volume: it causes Compose to persist that directory, initialized from what's in the image, but disconnected from your host system. With a bare name and a path, it creates a named volume, which is similar but can be explicitly managed. With two paths, it creates a bind mount, which unconditionally replaces the container content with the host-system content (there is no initialization).

version: '3.8'
services:
  something:
    volumes:
      - /path1             # anonymous volume
      - named:/path2       # named volume
      - /host/path:/path3  # bind mount
volumes:                   # named volumes referenced in containers only
  named:                   # usually do not need any settings

So if you do want to replace the image's contents with host directories, you need to use the bind-mount syntax. Relative paths here are interpreted relative to the location of the docker-compose.yml file.

version: '3.8'
services:
  drupal:
    image: drupal:8-apache
    volumes:
      - ./modules:/var/www/html/modules
      # etc.

A final comment on named volume initialization: your file has a comment about initializing anonymous volumes. There are two major problems with this approach, though. First, the second time you start the container, the content of the volume takes precedence, and any changes in the underlying images will be ignored. Second, this setup only works for Docker named and anonymous volumes, but not Docker bind mounts, volume mounts in Kubernetes, or other types of mount. I'd generally avoid relying on this "feature".