Search code examples
dockerubuntumagentodocker-composedocker-volume

Docker compose host volume directory empty


I have run docker-compose up -d command but volume directories are empty on host.

  1. Ubuntu 20.04
  2. docker-compose up -d
  3. page is displayed in the browser as expected
  4. docker volume inspect shows this:
    {
        "CreatedAt": "2020-08-29T22:26:49+01:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/magento-sandbox_magento_data/_data",
        "Name": "magento-sandbox_magento_data",
        "Options": null,
        "Scope": "local"
    }
]
  1. Image: https://hub.docker.com/r/bitnami/magento/
  2. yml file (/home/tomas/Documents/Projects/Magento/magento-sandbox):
version: '2'
services:
  mariadb:
    image: 'docker.io/bitnami/mariadb:10.3-debian-10'
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - MARIADB_USER=tomas
      - MARIADB_PASSWORD=tomas
      - MARIADB_DATABASE=magento_sandbox
    volumes:
      - 'mariadb_data:/home/tomas/Documents/Projects/Magento/magento-sandbox/db'
  magento:
    image: 'docker.io/bitnami/magento:2-debian-10'
    environment:
      - MARIADB_HOST=mariadb
      - MARIADB_PORT_NUMBER=3306
      - MAGENTO_DATABASE_USER=tomas
      - MAGENTO_DATABASE_PASSWORD=tomas
      - MAGENTO_DATABASE_NAME=magento_sandbox
      - ELASTICSEARCH_HOST=elasticsearch
      - ELASTICSEARCH_PORT_NUMBER=9200
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - 'magento_data:/home/tomas/Documents/Projects/Magento/magento-sandbox/webroot'
    depends_on:
      - mariadb
      - elasticsearch
  elasticsearch:
    image: 'docker.io/bitnami/elasticsearch:6-debian-10'
    volumes:
      - 'elasticsearch_data:/home/tomas/Documents/Projects/Magento/magento-sandbox/elasticsearch/data'
volumes:
  elasticsearch_data:
    driver: local
  mariadb_data:
    driver: local
  magento_data:
    driver: local
  1. All containers are running (docker ps command verifies this)

Why my /home/tomas/Documents/Projects/Magento/magento-sandbox/webroot is empty? I mean why mountpoint is not reflecting the configuration in the file?

How to achieve the result where this directory contains all the files used to render the page in the browser?


Solution

  • Uhm you did create volumes but you did not asign any source paths to them. So they are stored in the default path. (under ubuntu I know it is /var/lib/docker/volumes/HERE). Anyway you will have to find out where the data you want to mount is stored on the docker machines. (you normally set the paths in dockerfiles)

    Anyway, the correct syntax is

    /path/to/host/machine:/path/in/container/machine
    

    Once you have found that out go ahead and create a .env file in your diretory where your container and compose is stored. It contains

    HOST_BASE_DATA_PATH=/home/tomas/Documents/Projects/Magento/magento-sandbox
    MAGENTO_DATA_PATH=/insert/the/path/inside/the/magento/container
    ELASTICSEARCH_DATA_PATH=/insert/the/path/inside/the/elastic/container
    MARIADB_DATA_PATH=/insert/the/path/inside/the/mariadb/container
    

    We will use these variable to get a somehwat cleaner docker-compose file.

    In your compose we will specify volumes using the following attributes: ( I hope one example makes it clear enough)

    mariadb:
        image: 'docker.io/bitnami/mariadb:10.3-debian-10'
        environment:
          - ALLOW_EMPTY_PASSWORD=yes
          - MARIADB_USER=tomas
          - MARIADB_PASSWORD=tomas
          - MARIADB_DATABASE=magento_sandbox
        volumes:
          - type: bind
            # where it will be stored on the host machine
            source: ${HOST_BASE_DATA_PATH}/mariadb
            # where it is stored on the docker container
            target: ${MARIADB_DATA_PATH}
    

    greetings