在Docker上玩转PostgreSQL -- Mac篇
jopen
9年前
今天给大家带来的话题是:*在Docker上玩转PostgreSQL* (标题看到是不是有点小激动呢,这么高大尚的容器技术,让"云"变得更容易)
以下是华丽的正文分隔线
google一下发现 Docker官方PostgreSQL镜像 已经介绍了怎么制作PostgreSQL镜像教程。
今天的分享是面向Mac用户(悄悄的告诉你,小编俺也是第一次在Mac上玩起PostgreSQL On Docker)。
战前准备
大家以热烈的掌声来欢迎神器: Docker Toolbox 登场!
-
简述
Docker Toolbox 的诞生是广大 Mac 和 Windows 用户玩转Docker的福音。
再也不用再折腾 boot2docker 各种墙的 问题 ,俺是不会告诉你:运行boot2docker init会有一种让你砸掉键盘的冲动,好吧,小编还真砸掉一块了,-_-!!。
-
安装Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
-
安装Cask
brew install caskroom/cask/brew-cask
-
安装Docker Toolbox
brew cask install dockertoolbox
运行Docker Quickstart Terminal.app
bash --login '/Applications/Docker/Docker Quickstart Terminal.app/Contents/Resources/Scripts/start.sh'
出现以下提示,说明Docker已经就绪
## . ## ## ## == ## ## ## ## ## === /"""""""""""""""""\___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\_______/ docker is configured to use the default machine with IP *.*.*.* For help getting started, check out the docs at https://docs.docker.com
否则会提示
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
动手制作PostgreSQL Docker镜像
-
编写Dockerfile
翠花,上PG Dockerfile
$ cd ~;mkdir mypostgresql;cd mypostgresql $ cat Dockerfile FROM ubuntu:16.04 RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list RUN apt-get update && apt-get -y -q install python-software-properties software-properties-common \ && apt-get -y -q install postgresql-9.4 postgresql-client-9.4 postgresql-contrib-9.4 USER postgres RUN /etc/init.d/postgresql start \ && psql --command "CREATE USER pger WITH SUPERUSER PASSWORD 'pger';" \ && createdb -O pger pgerdb USER root RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.4/main/pg_hba.conf RUN echo "listen_addresses='*'" >> /etc/postgresql/9.4/main/postgresql.conf EXPOSE 5432 RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"] USER postgres CMD ["/usr/lib/postgresql/9.4/bin/postgres", "-D", "/var/lib/postgresql/9.4/main", "-c", "config_file=/etc/postgresql/9.4/main/postgresql.conf"]
-
制作Docker镜像
docker build --rm=true -t mypostgresql:9.4 .
-
运行PostgreSQL On Docker
docker run -i -t -p 5432:5432 mypostgresql:9.4
-
测试
psql -h localhost -p 5432 -U pger -W pgerdb
结果显示如下,说明镜像制作成功了,细心的读者可能会发现里面已经多了一个名叫pgerdb的数据库
Password for user pger: psql (9.4.2, server 9.4.5) SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off) Type "help" for help. pgerdb=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+-----------+---------+-------+----------------------- pgerdb | pger | SQL_ASCII | C | C | postgres | postgres | SQL_ASCII | C | C | template0 | postgres | SQL_ASCII | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | SQL_ASCII | C | C | =c/postgres + | | | | | postgres=CTc/postgres (4 rows) pgerdb=#
搞定,以上就是今天要分享的PostgreSQL On Docker的话题,希望对大家有用,祝玩得开心~~