Docker: how to set up file ownership in a data-only container?

2.9k views Asked by At

I've dockerized a PHP application using a data-only container. I used this (docker-symfony) containers stack.

This is a simple definition of a data-only container:

FROM debian:jessie
MAINTAINER Vincent Composieux <[email protected]>
VOLUME /var/www/symfony

Everything plays really well apart from ownership and permissions. I've noticed that when I mount volumes (my local directory) to the data-only container, the mounted files remain owned by my current user on the host, which in not recognized inside the container.

For example, If I'm starting the containers with docker-compose up as ltarasiewicz, and then I log into the data container, I can see that the mounted files have ownership set to:

drwxrwxr-x 7 1000 1000 4096 Jun 10 21:27 symfony

uid and gid of 1000 correspond to my host's user uid and gid. Because there is no such user inside the container, only IDs are displayed for the symfony directory. This makes it impossible to run the application.

So my question is how I can mount volumes to a data-only container and assign correct ownership to the mounted files, e.g. root:www-data or whatever other users I choose.

1

There are 1 answers

3
Adrian Mouat On BEST ANSWER

Use the same image you use for running the application to make your data container. For example, if you want to make a postgresql database, use the postgres image for your data container:

$ docker run --name dc postgres echo "Data Container"

This command created the data container and exited - note that data containers aren't left running.

In the case of postgres, the volume ownership won't be set correctly until you use the volume to start a db:

$ docker run -d --volumes-from dc postgres

But other images will set up the ownership correctly in the Dockerfile.

Of course, if you just want to fix the permissions in a data container, just mount it and run chown:

$ docker run --volumes-from dc postgres chown -R postgres:postgres /var/lib/postgresql/data