Dockerize PostgreSQL
Dockerize PostgreSQL
Installing PostgreSQL on Docker
Assuming there is no Docker image that suits your needs on the Docker Hub, you can create one yourself.
Start by creating a new Dockerfile
:
Note: This PostgreSQL setup is for development-only purposes. Refer to the PostgreSQL documentation to fine-tune these settings so that it is suitably secure.
- #
- # example Dockerfile for https://docs.docker.com/engine/examples/postgresql_service/
- #
- FROM ubuntu
- # 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 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=noninteractive
- 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
- # 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"]
Build an image from the Dockerfile assign it a name.
- $ docker build -t eg_postgresql .
Run the PostgreSQL server container (in the foreground):
- $ docker run --rm -P --name pg_test eg_postgresql
There are 2 ways to connect to the PostgreSQL server. We can use Link Containers, or we can access it from our host (or the network).
Note: The
--rm
removes the container and its image when the container exits successfully.
Using container linking
Containers can be linked to another container’s ports directly using -link remote_name:local_alias
in the client’s docker run
. This sets a number of environment variables that can then be used to connect:
- $ 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
Connecting from your host system
Assuming you have the postgresql-client installed, you can use the host-mapped port to test as well. You need to use docker ps
to find out what local host port the container is mapped to first:
- $ 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
Testing the database
Once you have authenticated and have a docker =#
prompt, you can create a table and populate it.
- 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)
Using the container volumes
You can use the defined volumes to inspect the PostgreSQL log files and to backup your configuration and data:
- $ 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
Dockerize PostgreSQL的更多相关文章
- postgresql 基本语法
postgresql数据库创建/修改/删除等写入类代码语法总结: 1,创建库 2,创建/删除表 2.1 创建表 create table myTableName 2.2 如果表不存在则创建表 crea ...
- postgresql无法安装pldbgapi的问题
要对函数进行调试需要安装插件pldbgapi,当初在windows上面的postgresql实例中执行了一下语句就安装上了: create extension pldbgapi; 但是在linux中执 ...
- ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...
- MongoDB与PostgresQL无责任初步测试
PostgresQL一秒能插入多少条记录,MongoDB呢?读取的情况又如何?我写了一些简单的程序,得出了一些简单的数据,贴在这里分享,继续往下阅读前请注意下本文标题中的“无责任”,这表示此测试结果不 ...
- [PostgreSQL] 图解安装 PostgreSQL
图解安装 PostgreSQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5894462.html 序 园友的一篇<Asp.Net Cor ...
- Asp.Net Core 项目实战之权限管理系统(3) 通过EntityFramework Core使用PostgreSQL
0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...
- PostgreSQL介绍以及如何开发框架中使用PostgreSQL数据库
最近准备下PostgreSQL数据库开发的相关知识,本文把总结的PPT内容通过博客记录分享,本随笔的主要内容是介绍PostgreSQL数据库的基础信息,以及如何在我们的开发框架中使用PostgreSQ ...
- PostgreSql性能测试
# PostgreSql性能测试 ## 1. 环境+ 版本:9.4.9+ 系统:OS X 10.11.5+ CPU:Core i5 2.7G+ 内存:16G+ 硬盘:256G SSD ## 2. 测试 ...
- postgresql 导出数据字典文档
项目上需要整理目前数据库的数据字典文档.项目不规范,这种文档只要后期来补.这么多张表,每个字段都写到word文档里真心头大.就算前面写了个查询表结构的sql,但是最后整理到word里还是感觉有点麻烦. ...
随机推荐
- uva 10369 Arctic Network
题意: 有许多基地,每个基地都有两种收发信号的方式,一种是通过无线电收发机,另一种是通过卫星.两个基地之间可以通过卫星交流不管它们相距多远:但是通过无线电交流,就要求它们的距离不超过D.为了方便布置, ...
- mysql 安装目录说明
- .pages怎么在windows上打开?Windows下打开在Mac中编辑的.pages文件方法
.pages怎么在windows上打开?Windows下打开在Mac中编辑的.pages文件方法 1.最简单的方法是修改后缀名为.zip然后解压,解压后就可以看到一张图片,这个就是文档内容了. 2.更 ...
- Symfony2 学习笔记之控制器
一个controller是你创建的一个PHP函数,它接收HTTP请求(request)并创建和返回一个HTTP回复(Response).回复对象(Response)可以是一个HTML页面,一个XML文 ...
- Python+OpenCV图像处理(七)—— 滤波与模糊操作
过滤是信号和图像处理中基本的任务.其目的是根据应用环境的不同,选择性的提取图像中某些认为是重要的信息.过滤可以移除图像中的噪音.提取感兴趣的可视特征.允许图像重采样等等.频域分析将图像分成从低频到高频 ...
- javascript 的01
javaScript可以实现验证表单.制作特效等功能,JavaScript的目的主要是一下三点: 1 客户端表单验证2 页面动态效果3 jQuery的基础JavaScript是一种描述性语言,也是一种 ...
- 八大排序算法之七—堆排序(Heap Sort)
堆排序是一种树形选择排序,是对直接选择排序的有效改进. 基本思想: 堆的定义如下:具有n个元素的序列(k1,k2,...,kn),当且仅当满足 时称之为堆.由堆的定义可以看出,堆顶元素(即第一个元素) ...
- HFA and outhandler differentiate or not
better trouble shooting auto-open accidently? HFA not stable? check code modification ASAP!!!
- gcahce事物不够,借助binlog追上
gcahce事物不够,借助binlog追上 宕机节点以单机集群启动,既自己作为一个集群启动,不过UUID要和旧的集群保持一致: 修复grastate.dat 文件的方式这里略,直接通过wsrep_re ...
- 关于functools模块的wraps装饰器用途
测试环境:Python3.6.2 + win10 + Pycharm2017.3 装饰器之functools模块的wraps的用途: 首先我们先写一个装饰器 # 探索functools模块wraps ...