Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker postgresql container with external data container

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?

like image 936
Kevin Avatar asked Apr 27 '26 20:04

Kevin


1 Answers

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/
like image 173
Adrian Mouat Avatar answered Apr 29 '26 14:04

Adrian Mouat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!