From: W. Trevor King Date: Thu, 2 Jan 2014 06:23:41 +0000 (-0800) Subject: postgresql: Declare /var/lib/postgresql a VOLUME X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f6feceb9f1db730d3b87020f9d2115a34599b5cf;p=dockerfile.git postgresql: Declare /var/lib/postgresql a VOLUME Avoid the hassle of maintaining a host-mounted volume by letting Docker handle the volume maintenance ;). We need to declare the VOLUME *after* filling it with content (with 'emerge --config'), otherwise ownership and permissions on the empty volume are lost [1,2,3], and future RUN commands die due to: initdb: could not access directory "/var/lib/postgresql/9.3/data": Permission denied [1]: https://github.com/dotcloud/docker/issues/2360 [2]: https://github.com/dotcloud/docker/issues/2969 [3]: https://github.com/dotcloud/docker/issues/2975 [4]: https://github.com/dotcloud/docker/pull/3008 --- diff --git a/postgresql/Dockerfile.template b/postgresql/Dockerfile.template index a69b695..e7c094b 100644 --- a/postgresql/Dockerfile.template +++ b/postgresql/Dockerfile.template @@ -29,6 +29,7 @@ RUN emerge -v dev-db/postgresql-server RUN eselect news read new RUN rc-update add $(basename /etc/init.d/postgresql-*) default RUN yes | emerge --config dev-db/postgresql-server +VOLUME ["/var/lib/postgresql"] # Expose PostgreSQL to external network connections RUN echo "listen_addresses = '*'" >> $(echo /etc/postgresql*/postgresql.conf) diff --git a/postgresql/README.md b/postgresql/README.md index 065e6e6..fd279f4 100644 --- a/postgresql/README.md +++ b/postgresql/README.md @@ -46,22 +46,25 @@ You can also access it from unlinked containers: Basically, anyone with access to the `docker0` bridge has access to the client's port. -If you're going to be loading a large database ([devicemapper only -supports 16GB][devicemapper-size-limit]), you may want to -[volume-mount] the database. Grab the configured stuff we'll want to -mount: - - # docker run -d -name postgresql-0 wking/postgresql - # docker cp postgresql-0:/var/lib/postgresql/ /var/lib/ - # mv /var/lib/postgresql/ /var/lib/postgresql-0 - # docker kill postgresql-0 - # docker rm postgresql-0 - -And run future containers with: - - $ docker run -d -name postgresql-0 -v /var/lib/postgresql-0:/var/lib/postgresql wking/postgresql +To allow mounting a large database ([devicemapper only supports +16GB][devicemapper-size-limit]) we declare `/var/lib/postgresql` as a +[VOLUME][]. This means that `/var/lib/postgresql` is stored outside +the image (under `/var/lib/docker/vfs/dir` with metadata under +`/var/lib/docker/volumes`) and mounted automatically when you spin up +a container. From a [pending version][fd24041] of [issue 3389][3389]: + +> If you remove containers that mount volumes, including the initial +> `DATA` container, or the middleman, the volumes will not be deleted +> until there are no containers still referencing those volumes. This +> allows you to upgrade, or effectivly migrate data volumes between +> containers. + +That means you should be able to migrate your `/var/lib/postgresql` +data to new PostgreSQL containers (e.g. if you upgrade PostgreSQL). [PostgreSQL]: http://postgresql.io/ [linking]: http://docs.docker.io/en/latest/use/port_redirection/#linking-a-container [devicemapper-size-limit]: https://www.kernel.org/doc/Documentation/device-mapper/thin-provisioning.txt -[volume-mount]: http://docs.docker.io/en/latest/use/working_with_volumes/#mount-a-host-directory-as-a-container-volume +[VOLUME]: http://docs.docker.io/en/latest/use/working_with_volumes/#getting-started +[fd24041]: https://github.com/SvenDowideit/docker/commit/fd240413ff835ee72741d839dccbee24e5cc410c +[3389]: https://github.com/dotcloud/docker/pull/3389