Docker-compose command: syntax

Hello,
I am using docker-compose to create my containers. It alls works fine but I realised that one of the volume I created must be owned by www-data:www-data rather than root for the application to work.

I thought I could use “command” in the docker file to do it but when I use it the app doesn’t want to start. Strangely I can see that the command is successful since the ownership of the volume is changed.

Here is my docker-compose file

version: '2'

services:
  db:
    image: mariadb
    container_name: sonerezh_db
    volumes:
     - "data_sql:/var/lib/mysql"
    environment:
     MYSQL_ROOT_PASSWORD: 'myrootpasswd'
     MYSQL_USER: 'sonerezh'
     MYSQL_PASSWORD: 'mypasswd'
     MYSQL_DATABASE: 'sonerezh'

  app:
    image: "sonerezh/sonerezh:latest"
    #container_name: sonerezh_app
    ports:
     - "8000:80"
    volumes:
     - "data_music:/music"
     - "data_thumbnails:/thumbnails"

    command: /bin/chown www-data:www-data /thumbnails

    environment:
     SONEREZH_DB_ENV_MYSQL_USER: 'sonerezh'
     SONEREZH_DB_ENV_MYSQL_PASSWORD: 'mypasswd'
     SONEREZH_DB_ENV_MYSQL_DATABASE: 'sonerezh'
     SONEREZH_DB_PORT_3306_TCP_ADDR: 'db'

volumes:
  data_sql:
  data_music:
  data_thumbnails:

and here is the error I can see when I use docker-compose up

Creating network "sonerezh_default" with the default driver
Creating volume "sonerezh_data_thumbnails" with default driver
Creating volume "sonerezh_data_sql" with default driver
Creating volume "sonerezh_data_music" with default driver
Creating sonerezh_db
Creating sonerezh_app_1
Attaching to sonerezh_app_1, sonerezh_db
sonerezh_app_1 exited with code 0
db_1   | Initializing database
db_1   | 2017-02-21 19:14:11 139747488479168 [Note] /usr/sbin/mysqld (mysqld 10.1.21-MariaDB-1~jessie) starting as process 58 ...
db_1   | 2017-02-21 19:14:11 139747488479168 [Note] InnoDB: Using mutexes to ref count buffer pool pages
...

state of the container show the app has exited with Exit 0. Is exit 0 the same as in linux and says it is normal?

docker-compose ps
     Name                   Command               State     Ports   
-------------------------------------------------------------------
sonerezh_app_1   /entrypoint.sh /bin/chown  ...   Exit 0            
sonerezh_db      docker-entrypoint.sh mysqld      Up       3306/tcp 

but the ownership change is working

ls -al sonerezh_data_thumbnails/
total 12
drwxr-xr-x 3 root     root     4096 Feb 21 19:14 .
drwx------ 7 root     root     4096 Feb 21 19:14 ..
drwxr-xr-x 2 www-data www-data 4096 Feb 21 19:14 _data

if the exit 0 means it is a normal exit what should I do to avoid it exiting?

your app has a command as chown ....
So your app just will change the file rights and after that exits.
So it behaves as specified.
You should try to start the webserver within the app-command.
This webserver process hopefully will never exit, so docker-compose up -d also will not complain about the exit.

PS: it is also a good practice to add a restart policy like restart: unless-stopped. This will restart everything even if your app will eventually crash sometime.

1 Like

Hello @think,
I have tried to go that line. I actually retrieved the docker file that the sonerezh project is using to create their image and tried to put the chmod command in on the RUN command but somehow it is still not changing the ownership. The container now stays up at least.

RUN git clone --branch 1.1.1 --depth 1 https://github.com/Sonerezh/sonerezh.git /usr/share/nginx/sonerezh &&
ln -s /usr/share/nginx/sonerezh/app/webroot/img/thumbnails /thumbnails &&
chown -R www-data: /usr/share/nginx/sonerezh &&
chown -hR www-data:www-data /thumbnails &&
chmod 775 -R /usr/share/nginx/sonerezh

What I found though is that only the _data is updated. Inside _data it is still owned by root… I have reported an issue about that on the github page of the project.

/thumbnail belongs to you, cause you explicitly mount that volume.

In docker the default user it root so this behaviour seems to be intended by them.

if you remove the “command”-line, does it work?

Yes it does work (as in the container does start and stay up) but the problem is that application try to write in that directory and since it is nginx it is using www-data and so it can’t because the directory belongs to root :frowning:

you do not provide the container the user it should use, so the container just could run as root.
if you want the container to run as www-data (or another user) you need to explicitly tell this to the container.
If the container itself creates a user www-data, then this user has another Id as the www-data user on your host and it will not work.

I am not sure I understand you! The container seems to work properly and the music application is a web app that runs as www-data inside the container.

The only issue I have is that it can’t write in that directory and I have not managed to change its ownership

I have actually progressed a bit. I created a directory in that volume and the Dockerfile then create the symbolic link to that directory. I can then change that directory. I am still trying to understand what is going on here!!!

I finally managed to solve my issue. I ended up editing the entrypoint.sh file and added a chow command inside it

chown -h www-data: /thumbnails/

and now all is working!