
ubuntu 版本:16.04
Postgresql版本:9.3
1. 编写dockerfile文件
dockerfile仅用于开发测试使用,具体构建可参考Postgresql 文档调整 Dockerfile
## example Dockerfile for https://docs.docker.com/engine/examples/postgresql_service/#FROM ubuntu:16.04# Add the PostgreSQL PGP key to verify their Debian packages.# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.ascRUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --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# 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=noninteractiveRUN apt-get update && apt-get install -y python-software-properties software-properties-common 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 portEXPOSE 5432# Add VOLUMEs to allow backup of config, logs and databasesVOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]# Set the default command to run when starting the containerCMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]
2. 构建
$ docker build -t eg_postgresql .
3. 完成后查看镜像
(base) ➜ docker docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEeg_postgresql latest ddcb89b572d2 9 seconds ago 391MBubuntu 16.04 56bab49eef2e 2 weeks ago 123MBhaoxy/oracle 19.3.0-ee 29678b999bb3 4 weeks ago 6.65GBcentos latest 3fa822599e10 2 years ago 204MB
4.启动postgresql容器服务
docker run --rm -P --name pg_test eg_postgresql
前台运行,--rm 在容器成功退出时删除容器
-P 暴露全部的端口
5.连接
打开另外一个终端窗口,使用容器链接连接容器 在客户端docker run中直接使用-link remote_name:local_alias使容器连接到另一个容器端口。
docker run --rm -t -i --link pg_test:pg eg_postgresql bash
2 测试数据
一旦你已经通过身份验证,并且有docker =#提示,您可以创建一个表并填充它。
postgres@75328bdd96e7:/$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --passwordPassword for user docker:psql (9.3.17)SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256)Type "help" for help.docker=#docker=# create table cities(docker(# name varchar(80),docker(# location pointdocker(# );CREATE TABLEdocker=# INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');INSERT 0 1docker=# select * from cities;name | location---------------+-----------San Francisco | (-194,53)(1 row)docker=# \q
6. 在本地连接postgersql
psql -h localhost -p 49153 -d docker -U docker --password
7.推送到镜像仓库
(base) ➜ ~ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEeg_postgresql latest ddcb89b572d2 2 hours ago 391MB(base) ➜ ~ docker tag ddcb89b572d2 haoxy/postgresql:9.3(base) ➜ ~(base) ➜ ~(base) ➜ ~ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEhaoxy/postgresql 9.3 ddcb89b572d2 2 hours ago 391MBdocker push haoxy/postgresql:9.3
8.镜像仓库
https://hub.docker.com/r/haoxy/postgresql
笔记(一)中有两个链接没有显示,补充入下:
英文文档 https://www.postgresql.org/docs/11/install-procedure.html
中文文档 http://www.postgres.cn/docs/11/installation.html
最后修改时间:2020-01-17 09:34:57
文章转载自运维笔记本,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




