Home > Net >  Volume defined in docker-compose.yml shows up empty
Volume defined in docker-compose.yml shows up empty

Time:11-17

I have the following in my docker-compose.yml file:

version: '3.9'

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
      - db_import:/import
    restart: always
    ports:
      - "3338:3306"
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
volumes:
  db_data: {}
  db_import: {}
  dist: {}

The problem is that even though I have a dump.sql file in my db_import folder (inside the same folder where docker-compose.yml resides), I find that docker-compose exec db ls -al /import shows an empty directory. I've tried restarting all docker containers without any improvement.

Why is this directory not passing my content through?

CodePudding user response:

By writing:

    volumes:
      - db_data:/var/lib/mysql
      - db_import:/import
(...)
volumes:
  db_data: {}
  db_import: {}
  dist: {}

You created so called Named Volumes with names db_data, db_import and dist. Docker doesn't tell us where those volumes are stored (and we shouldn't care about that). That kind of volumes is used to share data between containers and they do not have access to anything from your host machine.

If you want to share files beetween your host and container you must use Mount Binds instead - syntax is almost identical, you just need to replace db_data and db_import with absolute paths to that directories:

version: '3.9'

services:
  db:
    image: mysql:5.7
    volumes:
      - /PATH/TO/db_data:/var/lib/mysql
      - /PATH/TO/db_import:/import
    restart: always
    ports:
      - "3338:3306"
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
#volumes: That part isn't needed unless you use "dist" volume somewhere
#  db_data: {}
#  db_import: {}
#  dist: {}
  • Related