Russ Bateman
Russ Bateman

Reputation: 18643

docker run --volume mapping ignored

I successfully run PostgreSQL thus:

$ docker run --name postgresql --env POSTGRES_PASSWORD=password --publish 6000:5432 --volume /home/russ/dev/pg:/var/lib/postgresql/data postgres

only to find that:

$ docker inspect postgresql
...
        "Mounts": [
            {
                "Type": "volume",
                "Name": "06d27a1fe489cedfa47d6a3e801cb286494958e1c3a17f044205629cc7070952",
                "Source": "/var/lib/docker/volumes/06d27a1fe489cedfa47d6a3e801cb286494958e1c3a17f044205629cc7070952/_data",
                "Destination": "/var/lib/postgresql/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
...

Docker's usual, random filesystem backing is used instead of the hard-coded path I tried to map. Why is this or what should I have done instead?

Upvotes: 0

Views: 451

Answers (1)

MrDiggles
MrDiggles

Reputation: 768

If you look at the Postgres Dockerfile, you'll see a VOLUME [/var/lib/postgresql/data].

This command creates the default, "anonymous" volume you're seeing and takes precedence over the --volume argument you provide with the CLI (as well as any commands in "child" Dockerfiles or configuration in docker-compose files).

This extremely annoying quirk of Docker applies to other commands as well is currently being debated in https://github.com/moby/moby/issues/3465. This comment describes a similar problem with mysql images.

Unfortunately, there isn't an easy workaround but here are some common methods I've seen used:

  • Reconfigure Postgres to work out of a different directory and mount to that instead
  • Have another container mount to the same anonymous volume and to your machine and have it copy data over periodically

If you just want the data persist between container starts, I would recommend keeping it in the anonymous volume to keep it simple.

Upvotes: 1

Related Questions