Docker Creates Random Volumes and Mounts Them to Already Mounted Directories in containers

Hello,

I currently have the problem that Docker creates additional volumes for seemingly random containers to which I assign a volume. These additional volumes are then mounted on paths in the container that are already covered by my mount.

An example:

I have two containers in a Docker Compose file, both using subpaths in a volume.

version: '3'
services:
  homeassistant:
    container_name: homeassistant_justin
    image: "ghcr.io/home-assistant/home-assistant:stable"
    volumes:
      - type: volume
        source: data
        target: /config
        volume:
          subpath: homeassistant
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - mosquitto

    ports:
      - 8123:8123
      - 1400:1400
    restart: unless-stopped
    networks:
      justins_homeassistant:
        ipv4_address: 172.25.0.2
      HomeAssistant_Link:
        ipv4_address: 172.21.0.3


  mosquitto:
    image: eclipse-mosquitto
    container_name: mosquitto_justin
    restart: unless-stopped
    ports:
      - 1883:1883
      - 8883:8883
    volumes:
      - type: volume
        source: data
        target: /mosquitto
        volume:
          subpath: mosquitto
    environment:
      - TZ=Europe/Berlin
    networks:
      justins_homeassistant:
        ipv4_address: 172.25.0.3



volumes:
  data:

networks:
  justins_homeassistant:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/16
          gateway: 172.25.0.1
  HomeAssistant_Link: 
    external: true
  • The Home Assistant container runs perfectly fine and only has one volume (justins_homeassistant_data)
  • The Mosquitto container, however, receives the volume I created for it (in this case justins_homeassistant_data), and this is mounted in the container at /mosquitto.

Docker then creates two additional volumes and mounts them at /mosquitto/data and /mosquitto/log within the container.

Why does Docker do this, and how can I prevent it? I have already tried deleting and recreating the container without success.

The eclipse-mosquitto image declares two mountpoints, /mosquitto/data and /mosquitto/log in its metadata. You can see the declaration on line 97 of its Dockerfile.

When containers are created from such images, if the user has not specified any mounts for those specific mountpoints, the docker engine automatically creates anonymous volumes and mounts them. Attaching a volume to a parent path, like you have done, will not prevent this from happening.

Image creators usually have a good reason to declare separate mountpoints, such as keeping data and logs separate so that, for example, you can mount a regular volume to /mosquitto/data and a tmpfs directory to /mosquitto/logs.

I am not sure if there is a way to prevent this behaviour. In your case, I would simply mount the data volume to /mosquitto/data instead of /mosquitto, and maybe a different volume (or the same volume with a different subpath) to /mosquitto/logs if you wanted to keep the logs.

1 Like

Unfortunately, there is no way. That is why many image maintainers stopped defining volumes in Dockerfiles.