Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chown docker volumes on host (possibly through docker-compose)

I have the following example

version: '2'

services:
  proxy:
    container_name: proxy
    hostname: proxy
    image: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - proxy_conf:/etc/nginx
      - proxy_htdocs:/usr/share/nginx/html

volumes:
  proxy_conf: {}
  proxy_htdocs: {}

which works fine. When I run docker-compose up it creates those named volumes in /var/lib/docker/volumes and all is good. However, from the host, I can only access /var/lib/docker as root, because it's root:root (makes sense). I was wondering if there is a way of chowning the host's directories to something more sensible/safe (like, my relatively unprivileged user that I use to do most things on the host) or if I just have to suck it up and chown them manually. I'm starting to have a number of scripts already to work around other issues, so having an extra couple of lines won't be much of a problem, but I'd really like to keep my self-written automation minimal, if I can -- fewer chances for stupid mistakes.

By the way, no: if I mount host directories instead of creating volumes, they get overlaid, meaning that if they start empty, they stay empty, and I don't get the default configuration (or whatever) from inside the container.

Extra points: can I just move the volumes to a more convenient location? Say, /home/myuser/myserverstuff/volumes?

like image 794
Morpheu5 Avatar asked Mar 30 '16 15:03

Morpheu5


People also ask

Can you mount a Docker volume on the host?

You can mount host volumes by using the -v flag and specifying the name of the host directory. Everything within the host directory is then available in the container. What's more, all the data generated inside the container and placed in the data volume is safely stored on the host directory.

Does Docker compose RM remove volumes?

Removes stopped service containers. By default, anonymous volumes attached to containers are not removed.

What does volumes in Docker compose do?

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.

Can 2 Docker containers share a volume?

Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default. However, we can use a volume diver to share data across multiple machines. Finally, Docker also has –volumes-from to link volumes between running containers.


1 Answers

It's best to not try to access files inside /var/lib/docker directly. Those directories are meant to be managed by the docker daemon, and not to be messed with.

To access the data inside a volume, there's a number of options;

  • use a bind-mounted directory (you considered that, but didn't fit your use case).
  • use a "service" container that uses the same volume and makes it accessible through that container, for example a container running ssh (to use scp) or a SAMBA container (such as svendowideit/samba)
  • use a volume-driver plugin. there's various plugins around that offer all kind of options. For example, the local persist plugin is a really simple plug-in that allows you to specify where docker should store the volume data (so outside of /var/lib/docker)
like image 90
thaJeztah Avatar answered Sep 26 '22 22:09

thaJeztah