Anthony
Anthony

Reputation: 41

Docker-compose : volumes and user

I try to change the user who mount my Docker volumes but it's the "root" user who does it instead of "safe-user". Knowing that if I do a "chown -R /var/www" in the Dockerfile this one does not work since the mount point does not yet exist. how I can do for change the user mount point ?

My docker-compose.yml :

version: '2'
services:
  code-front:
    build:
      context: ./code-front/
    container_name: code-front
    volumes:
      - $HOME/web/IV3/code-front:/var/www/

  nginx-front:
    build:
      context: ./nginx-front/
    container_name: nginx-front
    ports:
      - 8000:80
    volumes_from:
      - code-front

My nginx-front/Dockerfile :

FROM nginx

# ====================================================================
#  Installation des differents paquets nécéssaire au bon 
#   fonctionnement du container (NodeJs 6.x, sudo, ...)
# ====================================================================
RUN apt-get update -y && apt-get upgrade -y && \
    apt-get install -y curl && \
    curl -sL https://deb.nodesource.com/setup_6.x | bash && \
    apt-get install -y nodejs && \
    apt-get install -y sudo && \
    apt-get install -y git

# ====================================================================
#           Configuration de NGINX
# ====================================================================
COPY interface_voyant_v3.conf /etc/nginx/conf.d/
RUN rm /etc/nginx/conf.d/default.conf

# ====================================================================
#                  Creation et configuration du user
# ====================================================================
RUN groupadd -r safe-group && \
    useradd -r -m -g safe-group safe-user

RUN usermod -u 1000 safe-user

RUN chown safe-user:safe-group /usr/bin/nodejs && \
        chmod 774 /usr/bin/nodejs

RUN chown safe-user:safe-group /usr/bin/node && \
        chmod 774 /usr/bin/node

RUN chown safe-user:safe-group /usr/bin/npm && \
    chmod 774 /usr/bin/npm

RUN chown safe-user:safe-group /usr/sbin/nginx && \
    chmod 774 /usr/sbin/nginx

RUN mkdir /var/www/ && \
    chown -R safe-user:safe-group /var/www/

RUN echo '%safe-group ALL=NOPASSWD:/usr/sbin/nginx' >> /etc/sudoers

RUN echo 'cd /var/www' >> /home/safe-user/.bashrc

EXPOSE 80

USER safe-user
CMD ["sudo", "nginx", "-g", "daemon off;"]

And my code-front/Dockerfile :

FROM busybox:glibc

VOLUME /var/www

Thanks for your help!

Upvotes: 3

Views: 6234

Answers (1)

maharr
maharr

Reputation: 121

do not change anything in docker-compose.yml

...
volumes:
  - $HOME/web/IV3/code-front:/var/www/
...

create directory first & change ownership before creating the mount point in nginx-front/Dockerfile

...
RUN mkdir -p /var/www ; chown -R safe-user:safe-user /var/www
VOLUME /var/www
...

Upvotes: 2

Related Questions