dockervolumes

I can share files between Docker containers, how do I share the same files with the host


I have a number of containers running of a Pi4 as defined by a docker-compose.yml file (extract below). They collect data from a piece of hardware which records data to /home/hardware/data/ on the “hardware” container. This is successfully shared with the “celery_worker” container via the “data” named volume.

My problem is I also need the files to be shared onto the host (e.g. /tmp/hardware_data), because they need to be read by another application on another machine on my home network.

I have googled, read the Docker documentation and watched a number of youtube videos, but am no nearer a solution.

celery_worker:
    image: celery_worker:latest
    container_name: celery_worker
    volumes:
      - data:/srv/project/hardware/hardware_data/

hardware:
    image: hardware:latest
    container_name: hardware
    volumes:
      - data:/home/hardware/data/

volumes:
  #to share hardware data
  data: {}

XXXXXXXXXXXXXXXXX Update

Konrad’s advice on getting rid of the named volumes made sense and my (extract) docker-compose.yml file now looks like this:

celery_worker:
    image: celery_worker:latest
    container_name: celery_worker
    volumes:
      - /tmp/hardware_data:/srv/project/hardware/hardware_data/

hardware:
    image: hardware:latest
    container_name: hardware
    volumes:
      - /tmp/hardware_data:/home/hardware/data/

Whilst in development I am running Docker as root. I have “touched” a “test file” in the hardware container’s Dockerfile. If I run the containers with the volumes #’d out, the “test file” is created in /home/hardware/data/ on the hardware’s container with the following permissions “- r w-r - -r- - root root .

When I run docker-compose.yml with the volumes defined I get three empty directories (2 in containers 1 in host) i.e. the hardware container’s “test file” is not present in any of the directories. If I “touch” a “new test file” in any of the directories it is shared across all three with the same permissions as the original “test file”: “- r w- r - - r - - root root”.

Based on this I can't see that it is a permissions issue


Solution

  • Solution

    I got some clues from here: https://forums.docker.com/t/how-to-access-docker-volume-data-from-host-machine/88063 Meyay’s answer “Only with named volumes existing data is copied from the container target folder into the volume.”

    This code works. Please note: /tmp/data must pre-exist on the host, with the same permissions (and owner I think) as the docker container.

    celery_worker:
        image: celery_worker:latest
        container_name: celery_worker
        volumes:
          - data:/srv/project/hardware/hardware_data/
    
    hardware:
        image: hardware:latest
        container_name: hardware
        volumes:
          - data:/home/hardware/data/
    
    volumes:
      data:
        driver: local
        driver_opts:
          o: bind
          type: none
          device: /tmp/data