Run postgres container with data volumes through docker-machine
Asked Answered
L

2

6

I have an issue with running postgres container with set up volumes for data folder on my Mac OS machine. I tried to run it such like this:

docker run \
  --name my-postgres \
  -e POSTGRES_USER=admin \
  -e POSTGRES_PASSWORD=password \
  -e POSTGRES_DB=some_db_dev \
  -v $PG_LOCAL_DATA:/var/lib/postgresql/data \
  -d postgres:9.5.1

Every time I got the following result in logs:

* Starting PostgreSQL
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are enabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
initdb: could not create directory "/var/lib/postgresql/data/pg_xlog": Permission denied
initdb: removing contents of data directory "/var/lib/postgresql/data"

Versions of docker, docker-machine, virtualbox and boot2docker are:

docker-machine version 0.6.0, build e27fb87
Docker version 1.10.2, build c3959b1
VirtualBox Version 5.0.16 r105871
boot2docker 1.10.3

I saw many publications about this topic but the most of them are outdated. I had tried do the similar solution as for mysql but it did not help.

Maybe somebody can updated me: does some solution exist to run postgres container with data volumes through docker-machine?

Thanks!

Lindbom answered 6/4, 2016 at 12:51 Comment(0)
I
2

If you are running docker-machine on a Mac, at this time, you cannot mount to a directory that is not part of your local user space (/Users/<user>/) without extra configuration.

This is because on the Mac, Docker makes a bind mount automatically with the home ~ directory. Remember that since Docker is being hosted in a VM that isn't your local Mac OS, any volume mounts are relative to the host VM - not your machine. That means by default, Docker cannot see your Mac's directories since it is being hosted on a separate VM from your Mac OS.

Mac OS => Linux Virtual Machine => Docker
                  ^------------------^
                   Docker Can See VM
   ^-----------------X----------------^
         Docker Can't See Here

If you open VirtualBox, you can create other mounts (i.e. shared folders) to your local machine to the host VM and then access them that way.

See this issue for specifics on the topic: https://github.com/docker/machine/issues/1826

I believe the Docker team is adding these capabilities in upcoming releases (especially since a native Mac version is in short works).

Inigo answered 6/4, 2016 at 12:57 Comment(0)
P
0

You should use docker named volumes instead of folders on your local file system. Try creating a volume:

docker volume create my_vol

Then mount the data directory in your above command:

docker run \
  --name my-postgres \
  -e POSTGRES_USER=admin \
  -e POSTGRES_PASSWORD=password \
  -e POSTGRES_DB=some_db_dev \
  -v my_vol:/var/lib/postgresql/data \
  -d postgres:9.5.1

Checkout my blog post for a whole postgres, node ts docker setup for both dev and prod: https://yzia2000.github.io/blog/2021/01/15/docker-postgres-node.html

For more on docker volumes: https://docs.docker.com/storage/volumes/

Peruse answered 17/1, 2021 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.