Logan
Logan

Reputation: 187

Docker compose always makes a new volume instead of using named ones

My problem is as the title states.

Here are the steps to reproduce. Note I am using docker compose file version 3.3, as I am running this off the apt python version of docker-compose as there is no binary for ARM64.

Create a docker file with these contents:

version: "3.3"

volumes:
  owncloud_data:
    external: true
  owncloud_mysql:
    external: true
  owncloud_backup:
    external: true
  owncloud_redis:
    external: true

services:

  traefik:
    image: "traefik"
    container_name: "traefik"
    restart: "always"
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.myresolver.acme.tlschallenge=true"
     #- "--certificatesresolvers.myresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
      # Be sure to have LeGo installed
      - "--certificatesresolvers.myresolver.acme.email=<my email>"
      - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"


  whoami:
    image: "containous/whoami"
    container_name: "simple-service"
    restart: "always"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`<my domain>`)"
      - "traefik.http.routers.whoami.entrypoints=websecure"
      - "traefik.http.routers.whoami.tls.certresolver=myresolver"

  owncloud:
    image: "owncloud/server"
    container_name: "owncloud"
    restart: "always"
    depends_on:
      - db
      - redis
    environment:
      - OWNCLOUD_DOMAIN=owncloud.<my domain>
      - OWNCLOUD_DB_TYPE=mysql
      - OWNCLOUD_DB_NAME=owncloud
      - OWNCLOUD_DB_USERNAME=owncloud
      - OWNCLOUD_DB_PASSWORD=owncloud
      - OWNCLOUD_DB_HOST=db
      - OWNCLOUD_ADMIN_USERNAME=<my username>
      - OWNCLOUD_ADMIN_PASSWORD=<my password>
      - OWNCLOUD_MYSQL_UTF8MB4=true
      - OWNCLOUD_REDIS_ENABLED=true
      - OWNCLOUD_REDIS_HOST=redis
    healthcheck:
      test: ["CMD", "/usr/bin/healthcheck"]
      interval: 30s
      timeout: 10s
      retries: 5
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.owncloud.rule=Host(`<my domain>`)"
      - "traefik.http.routers.owncloud.entrypoints=websecure"
      - "traefik.http.routers.owncloud.tls.certresolver=myresolver"
    volumes:
      - type: volume
        source: owncloud_data
        target: /owncloud/data

  db:
    image: webhippie/mariadb:latest
    restart: always
    environment:
      - MARIADB_ROOT_PASSWORD=owncloud
      - MARIADB_USERNAME=owncloud
      - MARIADB_PASSWORD=owncloud
      - MARIADB_DATABASE=owncloud
      - MARIADB_MAX_ALLOWED_PACKET=128M
      - MARIADB_INNODB_LOG_FILE_SIZE=64M
    healthcheck:
      test: ["CMD", "/usr/bin/healthcheck"]
      interval: 30s
      timeout: 10s
      retries: 5
    volumes:
      - type: volume
        source: owncloud_mysql
        target: /owncloud/mysql
      - type: volume
        source: owncloud_backup
        target: /owncloud/backup

  redis:
    image: webhippie/redis:latest
    restart: "always"
    environment:
      - REDIS_DATABASES=1
    healthcheck:
      test: ["CMD", "/usr/bin/healthcheck"]
      interval: 30s
      timeout: 10s
      retries: 5
    volumes:
      - type: volume
        source: owncloud_redis
        target: /owncloud/redis

I run this command before I start the containers:

for v in owncloud_data owncloud_mysql owncloud_backup owncloud_redis; do
    sudo docker volume create $v
done

I then run sudo docker-compose up.

When I run sudo docker volume ls I get my four (dangling volumes) I created earlier, and 4 more ones that were created from docker compose. What's the deal here? I specified their name explicitly. I'm not sure why the bind for the traefik container works.

I have tried seemingly everything. I put things in quotes. I tried the prepended project name. I tried the short syntax owncloud_data:/owncloud/data. I tried binds, although I don't want to use binds since it's easier to backup a volume.

Thank you, Logan

Upvotes: 3

Views: 1044

Answers (1)

F&#225;bio Batista
F&#225;bio Batista

Reputation: 25260

docker-compose is using the volumes you named, for the binds you specified. However, it is also creating unnamed volumes for each VOLUME declaration on the images you are using and not explicitly binding.

I've made this little script to extract the declared volumes from the images you're using:

images="traefik containous/whoami owncloud/server webhippie/mariadb:latest webhippie/redis:latest"

echo $images | xargs -n1 docker pull

docker inspect $images -f '{{.RepoTags}}, {{.Config.Volumes}}'

The result:

[traefik:latest], map[]
[containous/whoami:latest], map[]
[owncloud/server:latest], map[/mnt/data:{}]
[webhippie/mariadb:latest], map[/var/lib/backup:{} /var/lib/mysql:{}]
[webhippie/redis:latest], map[/var/lib/redis:{}]

None of your binds are binding to the declared volumes, so you're effectively creating new ones and leaving the declared ones empty.

Upvotes: 2

Related Questions