Docker化PostgreSQL

安装PostgreSQL

假设Docker Hub上没有Docker镜像适合你,你可以创建一个自己的。通过创建一个新的Dockerfile开始:

说明:这个PostgreSQL 配置是仅仅开发目标。参考PostgreSQL 文档微调这些设置,已让其达到安全。

  1. #
  2. # example Dockerfile for http://docs.docker.com/examples/postgresql_service/
  3. #
  4. FROM ubuntu
  5. MAINTAINER SvenDowideit@docker.com
  6. # Add the PostgreSQL PGP key to verify their Debian packages.
  7. # It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
  8. RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
  9. # Add PostgreSQL's repository. It contains the most recent stable release
  10. # of PostgreSQL, ``9.3``.
  11. RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main">/etc/apt/sources.list.d/pgdg.list
  12. # Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
  13. # There are some warnings (in red) that show up during the build. You can hide
  14. # them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
  15. RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
  16. # Note: The official Debian and Ubuntu images automatically ``apt-get clean``
  17. # after each ``apt-get``
  18. # Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
  19. USER postgres
  20. # Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
  21. # then create a database `docker` owned by the ``docker`` role.
  22. # Note: here we use ``&&\`` to run commands one after the other - the ``\``
  23. # allows the RUN command to span multiple lines.
  24. RUN/etc/init.d/postgresql start &&\
  25. psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';"&&\
  26. createdb -O docker docker
  27. # Adjust PostgreSQL configuration so that remote connections to the
  28. # database are possible.
  29. RUN echo "host all all0.0.0.0/0 md5">>/etc/postgresql/9.3/main/pg_hba.conf
  30. # And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
  31. RUN echo "listen_addresses='*'">>/etc/postgresql/9.3/main/postgresql.conf
  32. # Expose the PostgreSQL port
  33. EXPOSE 5432
  34. # Add VOLUMEs to allow backup of config, logs and databases
  35. VOLUME ["/etc/postgresql","/var/log/postgresql","/var/lib/postgresql"]
  36. # Set the default command to run when starting the container
  37. 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"]

构建一个镜像,分配一个名称:

  1. $ sudo docker build -t eg_postgresql .

运行PostgreSQL 服务容器(在前台):

  1. $ sudo docker run --rm -P --name pg_test eg_postgresql

这里有两种方式链接PostgreSQL 服务。我们可以使用Link Containers,或者我们可以通过网络或者本地操控它。

说明:—rm移除容器和它的镜像,当容器成功退出时。

使用容器互联

容器可以被其它容器端口所直接链接,使用-link remote_name:local_alias在客户端docker run时。这将设置系列的环境变量,然后可以在被使用来链接。

  1. $ sudo docker run --rm -t -i --link pg_test:pg eg_postgresql bash
  2. postgres@7ef98b1b7243:/$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password

从主机系统链接

假设你有安装好的postgresql-client,你可以使用主机映射端口的形式测试一下。你需要使用docker ps来发现容器映射的本地端口是多少。

$ sudo docker psCONTAINER IDIMAGE COMMANDCREATED STATUS PORTS NAMES

5e24362f27f6eg_postgresql:latest /usr/lib/postgresql/About an hour ago UpAbout an hour0.0.0.0:49153->5432/tcppg_test$ psql -h localhost -p 49153-d docker -U docker —password

测试数据库

一旦你被授权,有一个docker =#命令,你可以创建一个table然后落户。

  1. psql (9.3.1)
  2. Type"help"for help.
  3. $ docker=# CREATE TABLE cities (
  4. docker(# namevarchar(80),
  5. docker(# locationpoint
  6. docker(# );
  7. CREATE TABLE
  8. $ docker=# INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
  9. INSERT 01
  10. $ docker=# select * from cities; name | location
  11. ---------------+-----------
  12. SanFrancisco|(-194,53)
  13. (1 row)

使用容器数据卷

你可以使用定义的数据卷来监视PostgreSQL 日志文件,备份你的配置和数据:

  1. $ sudo docker run --rm --volumes-from pg_test -t -i busybox sh
  2. /# ls
  3. bin etc lib linuxrc mnt proc run sys usrdev home lib64mediaopt root sbin tmp var
  4. /# ls /etc/postgresql/9.3/main/
  5. environment pg_hba.conf postgresql.confpg_ctl.conf pg_ident.confstart.conf
  6. /tmp # ls /var/log
  7. ldconfigpostgresql