Docker postgresql container with external data container

docker, postgresql

Solution

The postgres user probably hasn't been assigned the same UID in the separate containers.

There's no need to make it this difficult though, just use the postgres image to create your data container e.g:

docker run --name data-container postgres echo "Data Container"

That way all the permissions will be set up correctly. For more information see:

- http://container42.com/2014/11/18/data-only-container-madness/ and

- http://container-solutions.com/2014/12/understanding-volumes-docker/

Problem

I have a data container with Dockerfile: ``` from ubuntu:latest VOLUME ["/var/lib/postgresql/9.3/main"] ``` and a postgresql service container: ``` #install stuff ............ # Set the default command to run when starting the container CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"] ``` then I start the data container and postgresql container: docker run -i -t -d --name data docker:data ``` docker run -i -t -p 49131:5432 --name postgresql --volumes-from data --rm docker:postgresql ``` it says : ``` FATAL: data directory "/var/lib/postgresql/9.3/main" has wrong ownership HINT: The server must be started by the user that owns the data directory. ``` it seems like the `/var/lib/postgresql/9.3/main` folder belong to the root user in the `data` container. Then i attach to the container, add user and change owner of that folder to postgres: ``` docker attach data useradd postgres -s /bin/bash chown -R postgres:postgres /var/lib/postgresql ``` then try again with the same error. what is the problem and what am i missing?

Original source