0

I wanted to use external directory as volume and struggling

  mariadb:
    image: mariadb:10.4
   ...
    environment:
      ..
    logging:
...
    networks:
      - backend
    restart: on-failure
    volumes:
      - maria_volume:/var/lib/mysql
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
  
# Volumes
volumes:
  maria_volume:

what does maria_volume: mean?

Reference: https://www.cloudytuts.com/tutorials/docker/how-to-add-persistent-data-to-mysql-with-docker-compose/

Ideally I want something like

volumes:
  maria_volume:
    external:
      name: ${PWD}/mariadb

But it gives error

Volume C:/some/test/mariadb declared as external, but could not be found. Please create the volume manually using docker volume create --name=C:/some/test/mariadb and try again.

If I run the volume create, it complains

includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]"

How can I create external volume and see data files?

My environment is Windows10 with Gitbash. But I am looking for a solution that works anywhere i.e. my local + cloud

Kris Swat
  • 788
  • 1
  • 10
  • 39

2 Answers2

0

You need to use Bind mount as per your requirement.

Difference between two is as below -

Named volume: a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.

Bind mounts: When you use a bind mount, a file or directory on the host machine is mounted into a container.

You can refer working example -

Below is the yml file

ubuntu@ip-1-2-3-4:/Docker-Compose$ cat mysql_test.yml
version: '3.7'
services:
  database:
    image: mysql:latest
    container_name: sqldb
    environment:
      MYSQL_ROOT_PASSWORD: "Password1234"
    ports:
      - 3355:3306
    volumes:
      - /home/ubuntu/mysql_data:/var/lib/mysql
ubuntu@ip-1-2-3-4:/Docker-Compose$

output:

we can see we have mysql container data accessible in host directory

ubuntu@ip-1-2-3-4:/Docker-Compose$ ls /home/ubuntu/mysql_data/
'#ib_16384_0.dblwr'   auto.cnf        binlog.index   client-cert.pem   ib_logfile0   ibtmp1      performance_schema   server-cert.pem   undo_001
'#ib_16384_1.dblwr'   binlog.000001   ca-key.pem     client-key.pem    ib_logfile1   mysql       private_key.pem      server-key.pem    undo_002
'#innodb_temp'        binlog.000002   ca.pem         ib_buffer_pool    ibdata1       mysql.ibd   public_key.pem       sys
ubuntu@ip-1-2-3-4:/Docker-Compose$
PravinY
  • 500
  • 1
  • 5
  • 9
-1

In this file as you've shown it, maria_volume is a named volume. You probably want a bind mount instead: remove the volumes: block at the end of the file, and in the container setup, use the host directory name instead of the name maria_volume.

version: '3.8'
services:
  mariadb:
    volumes:
      # Compose knows how to interpret relative paths;
      # they are relative to the location of the docker-compose.yml file
      - ./mariadb:/var/lib/mysql
# no top-level volumes: for this setup

Note that, on some platforms, Docker named volumes may be significantly faster than bind mounts. Since the database storage will be opaque files in a complex binary format, you can't really look at them directly, even if you do have the files outside of Docker space. The named-volume approach in the question may actually be better for database storage.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • ./mariadb works. Can you help maria_volume:, if that is ok – Kris Swat Jul 07 '21 at 00:22
  • 1
    In the named-volume form, you can't directly see the files (because Docker manages them), but you also don't need to manually `docker volume create` (Compose does it for you). If you do want to directly see the files, use a bind mount instead. – David Maze Jul 07 '21 at 01:03
  • Thank you for the explanation. I have a following question after trying to move to Kubernetes and reading , https://stackoverflow.com/questions/63782456/docker-compose-yml-in-google-cloud-run which seems to suggest I should not use external volumes if I have to deploy to cloud. Should I ask as a new question? – Kris Swat Jul 08 '21 at 08:14