在docker中安装postgresql
如果docker hub中没有你需要的docker镜像,你可以创建自己的镜像,开始先创建一个dockerfile:
注意:这个postgresql仅设置用途。请参阅postgresql文档来调整这些设置,以便它是安全的。
#
# example dockerfile for http://docs.docker.com/examples/postgresql_service/
#
from ubuntu
maintainer [email protected]
# add the postgresql pgp key to verify their debian packages.
# it should be the same key as https://www.postgresql.org/media/keys/accc4cf8.asc
run apt-key adv --keyserver keyserver.ubuntu.com --recv-keys b97b0afcaa1a47f044f244a07fcc7d46accc4cf8
# add postgresql's repository. it contains the most recent stable release
# of postgresql, ``9.3``.
run echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
# update the ubuntu and postgresql repository indexes
run apt-get update
# install ``python-software-properties``, ``software-properties-common`` and postgresql 9.3
# there are some warnings (in red) that show up during the build. you can hide
# them by prefixing each apt-get statement with debian_frontend=noninteractive
run apt-get -y -q install python-software-properties software-properties-common
run apt-get -y -q install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
# note: the official debian and ubuntu images automatically ``apt-get clean``
# after each ``apt-get``
# run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
user postgres
# create a postgresql role named ``docker`` with ``docker`` as the password and
# then create a database `docker` owned by the ``docker`` role.
# note: here we use ``&&\`` to run commands one after the other - the ``\``
# allows the run command to span multiple lines.
run /etc/init.d/postgresql start &&\
psql --command "create user docker with superuser password 'docker';" &&\
createdb -o docker docker
# adjust postgresql configuration so that remote connections to the
# database are possible.
run echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf
# and add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
run echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
# expose the postgresql port
expose 5432
# add volumes to allow backup of config, logs and databases
volume ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
# 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"]
使用dockerfile构建镜像并且指定名称
$ sudo docker build -t eg_postgresql .
并且运行postgresql服务容器
$ sudo docker run --rm -p --name pg_test eg_postgresql
有2种方法可以连接到postgresql服务器。我们可以使用链接容器,或者我们可以从我们的主机(或网络)访问它。
注:
--rm
删除容器,当容器存在时成功。
使用容器连接
在客户端docker run
中直接使用-link remote_name:local_alias
使容器连接到另一个容器端口。
$ sudo docker run --rm -t -i --link pg_test:pg eg_postgresql bash
postgres@7ef98b1b7243:/$ psql -h $pg_port_5432_tcp_addr -p $pg_port_5432_tcp_port -d docker -u docker --password
连接到你的主机系统
假设你有安装postgresql客户端,您可以使用主机端口映射测试。您需要使用docker ps
找出映射到本地主机端口:
$ docker ps
container id image command created status ports names
5e24362f27f6 eg_postgresql:latest /usr/lib/postgresql/ about an hour ago up about an hour 0.0.0.0:49153->5432/tcp pg_test
$ psql -h localhost -p 49153 -d docker -u docker --password
测试数据
一旦你已经通过身份验证,并且有docker =#
提示,您可以创建一个表并填充它。
psql (9.3.1)
type "help" for help.
$ docker=# create table cities (
docker(# name varchar(80),
docker(# location point
docker(# );
create table
$ docker=# insert into cities values ('san francisco', '(-194.0, 53.0)');
insert 0 1
$ docker=# select * from cities;
name | location
--------------- -----------
san francisco | (-194,53)
(1 row)
使用容器卷
您可以使用postgresql卷检查定义日志文件、备份配置和数据:
$ docker run --rm --volumes-from pg_test -t -i busybox sh
/ # ls
bin etc lib linuxrc mnt proc run sys usr
dev home lib64 media opt root sbin tmp var
/ # ls /etc/postgresql/9.3/main/
environment pg_hba.conf postgresql.conf
pg_ctl.conf pg_ident.conf start.conf
/tmp # ls /var/log
ldconfig postgresql