Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose volume creation for non-root user

I'm trying to create a docker image with docker-compose for my web application. My web application needs permissions to write files on file system.

My Dockerfile contains

# some other stuff
RUN addgroup -S web && adduser -S -g web web
USER web  

I want to mount my current . directory to /code in docker container;

When I perform docker-compose build && docker-compose up, It occurs that my /code directory in container is owned for user "1000:1000", not my user that I created in Dockerfile.

version: '2'                                                                     

services:                                                                                           
  admin:                                                                         
    build:                                                                       
      context: .                                                                 
      dockerfile: Dockerfile                                                     
    command: python manage.py 0.0.0.0 8030                           
    volumes:                                                                     
    - .:/code                                                                    
    ports:                                                                       
     - 9000:8030                                                                 
    stdin_open: true                                                             
    tty: true                                                                                                                 
volumes:                                                                         
  .:                                                                             
    driver_opts:                                                                 
      o: uid=100,gid=65533

As you see, I tried to pass driver_opts with my user uid and gid parameters; but it didn't help.

My question is: what should I do to mount my directory . to /code in docker container privileges for user web?

OS: Ubuntu 16.04

docker version 17.09.0-ce

docker-compose version 1.8.0

like image 869
Ilya Boltnev Avatar asked Oct 16 '17 16:10

Ilya Boltnev


People also ask

Can docker compose create volumes?

We can also create a volume with Docker compose service or also specify existing volumes. For example, the following screenshot shows a 'docker-compose' file that creates a docker-compose service with a volume. As a result of the above command, a volume with the name 'myvolume' gets created.

Does docker compose require root?

to be able to read and write to and from /var/run/docker. sock , you must either be root or being in the docker group. docker-compose is another cli it has the same requirements as docker .

Will docker compose create volume if not exists?

Multiple containers can mount the same volume. Running docker-compose up will create a volume named <project_name>_html_files if it doesn't already exist .


1 Answers

You can try with:

RUN addgroup -g 1000 web && \ adduser -G web -g web -s /bin/sh -D web

your user will be able to write to the mounted directory.

like image 108
Wiicolas Avatar answered Oct 02 '22 23:10

Wiicolas