Access named volume from container when not runnin

2019-04-11 01:18发布

问题:

I'm running Celery under Docker Compose. I'd like to make Celery's Flower persistent. So I do:

version: '2'
volumes:
  [...]
  flower_data: {}
[...]
flower:
  image: [base code image]
  ports:
    - "5555:5555"
  volumes:
    - flower_data:/flower
  command:
    celery -A proj flower --port=5555 --persistent=True --db=/flower/flower

However, then I get:

IOError: [Errno 13] Permission denied: 'flower.dat'

I ran the following to elucidate why:

    bash -c "ls -al /flower; whoami; celery -A proj flower --persistent=True --db=/flower/flower"

This made it clear why:

flower_1 | drwxr-xr-x 3 root root 4096 Mar 10 23:05 .
flower_1 | drwxr-xr-x 7 root root 4096 Mar 10 23:05 ..

Namely, the directory is mounted as root, yet in [base code image] I ensure the user running is not root, as per Celery's docks to never run as root:

FROM python:2.7
...
RUN groupadd user && useradd --create-home --home-dir /usrc/src/app -g user user
USER user

What would be the best way for Celery Flower to continue to run not as root, yet be able to use this named volume?

回答1:

The following works: In the Dockerfile, install sudo and add user to the sudo group, requiring a password:

RUN apt-get update
RUN apt-get -y install sudo
RUN echo "user:SECRET" | chpasswd && adduser user sudo

Then, in the Docker Compose config, the command will be:

bash -c "echo SECRET | sudo -S chown user:user /flower; celery -A proj flower --power=5555 --persistent --db=/flower/flower"

I'm not sure if this is the best way, though, or what the security implications of this are.