PostgreSQL の Docker を作成する。
この CookBook では、PostgreSQL 検証用の Docker の作成手順について紹介しています。
レシピ
- ベースイメージの作成
- PostgreSQL の Docker イメージを作成します
- 実行します
1. ベースイメージの作成
CentOS 6.9 を使用します。
Dockerfile
FROM centos:centos6.9
EXPOSE 22
ENV DEBIAN_FRONTEND noninteractive
# yum
RUN yum -y update && yum clean all
# locale
RUN yum reinstall -y glibc-common
RUN localedef -i ja_JP -f UTF-8 ja_JP.utf8
RUN touch /etc/sysconfig/i18n
RUN echo 'LANG="ja_JP.UTF-8"' >> /etc/sysconfig/i18n
ENV LANG ja_JP.UTF-8
ENV LC_ALL ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
# timezone
RUN yum install -y tzdata
RUN echo 'ZONE="Asia/Tokyo"' > /etc/sysconfig/clock
RUN echo 'UTC=false' >> /etc/sysconfig/clock
RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
# tools
RUN yum groupinstall -y 'Development Tools'
RUN yum install -y --enablerepo centosplus wget curl vim emacs tar unzip mlocate perl ssh openssh-server openssl-devel
# root passwd
RUN bash -c 'echo "root:password" | chpasswd'
# ssh
RUN sed -i -e "s/#PasswordAuthentication yes/PasswordAuthentication yes/g" /etc/ssh/sshd_config
RUN sed -i -e "s/#PermitRootLogin yes/PermitRootLogin yes/g" /etc/ssh/sshd_config
RUN sed -i -e "s/UsePAM yes/UsePAM no/g" /etc/ssh/sshd_config
RUN updatedb
CMD /etc/init.d/sshd restart && /bin/bash
ロケールを ja_JP, タイムゾーンを Asia/Tokyo に変更し、root/password で ssh 接続ができるように設定します。
mycentos:6.9 というタグでビルドします。
docker build -t mycentos:6.9 .
2. PostgreSQL の Docker イメージを作成します
先ほど作成したベースイメージを利用します。
Dockerfile
FROM mycentos:6.9
EXPOSE 22 5432
ENV DEBIAN_FRONTEND noninteractive
# yum
RUN yum -y update && yum clean all
RUN yum -y upgrade && yum -y update && yum clean all
RUN yum localinstall -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm
RUN yum install -y postgresql96-server
RUN yum install -y --setopt=protected_multilib=false epel-release
RUN yum install -y multitail ncurses-devel ncurses-static ncurses-term
# PostgreSQL
COPY setup_postgresql.sh /setup_postgresql.sh
RUN chmod u+x /setup_postgresql.sh
RUN /setup_postgresql.sh
RUN rm /setup_postgresql.sh
COPY run.sh /run.sh
RUN chmod u+x /run.sh
RUN updatedb
CMD /run.sh
細かいセットアップはシェルスクリプトに逃がし、Dockerfile からはそれを実行するようにします。
これにより、Dockerfile をシンプルにでき、かつイメージ容量の削減にもつながります。
setup_postgresql.sh
#!/bin/bash
service postgresql-9.6 initdb
echo "listen_addresses = '*'" >> /var/lib/pgsql/9.6/data/postgresql.conf
sed -i -e "s/ident$/trust/g" /var/lib/pgsql/9.6/data/pg_hba.conf
sed -i -e "s/127\.0\.0\.1\/32/0\.0\.0\.0\/0/g" /var/lib/pgsql/9.6/data/pg_hba.conf
sed -i -e "s/max_connections = 100/max_connections = 200/g" /var/lib/pgsql/9.6/data/postgresql.conf
sed -i -e "s/shared_buffers = 128MB/shared_buffers = 512MB/g" /var/lib/pgsql/9.6/data/postgresql.conf
echo "postgres:postgres" | chpasswd
service postgresql-9.6 start
sleep 2
su postgres -c "psql -c \"ALTER USER postgres WITH PASSWORD 'postgres'\""
su postgres -c "psql -c \"CREATE ROLE imart WITH LOGIN PASSWORD 'imart'\""
su postgres -c "psql -c \"CREATE DATABASE imart OWNER=imart encoding 'utf8' TEMPLATE template0\""
su postgres -c "psql -c \"CREATE DATABASE iap_db OWNER=imart encoding 'utf8' TEMPLATE template0\""
su postgres -c "psql -c \"CREATE DATABASE \\\"default\\\" OWNER=imart encoding 'utf8' TEMPLATE template0\""
su postgres -c "psql -d imart -c \"CREATE SCHEMA acceldocuments AUTHORIZATION imart\""
su postgres -c "psql -d iap_db -c \"CREATE SCHEMA acceldocuments AUTHORIZATION imart\""
su postgres -c "psql -d default -c \"CREATE SCHEMA acceldocuments AUTHORIZATION imart\""
service postgresql-9.6 stop
rm -rf /var/lib/pgsql/9.6/data/pg_log/*.log
以下のユーザを作成しています。
- postgres/postgres
- imart/imart
以下の名前の DB を作成しています。
- imart
- iap_db
- default
以下の名前のスキーマを作成しています。
- acceldocuments
以下の設定は小さすぎるためある程度拡張します。
- max_connections
- 100 -> 200
- shared_buffers
- 128MB -> 512MB
docker run 時に実行するスクリプトです。
run.sh
#!/bin/bash
/etc/init.d/sshd restart
service postgresql-9.6 restart
multitail -M 0 --follow-all --retry-all -q 1 "/var/lib/pgsql/9.6/data/pg_log/*.log"
sshd と PostgreSQL を起動しています。
mypostgresql:9.6 というタグでビルドします。
docker build -t mypostgresql:9.6 .
3. 実行します
docker run -it -p 5432:5432 -p 2222:22 mypostgresql:9.6
resin-web.xml
Resin で接続する場合次のように設定します。
localhost の部分は、docker run しているマシンの IP アドレスに置き換えてください。
<database jndi-name="jdbc/imart">
<driver>
<type>org.postgresql.Driver</type>
<url>jdbc:postgresql://localhost:5432/imart</url>
<user>imart</user>
<password>imart</password>
<init-param>
<param-name>preparedStatementCacheQueries</param-name>
<param-value>0</param-value>
</init-param>
</driver>
<max-connections>20</max-connections>
<prepared-statement-cache-size>0</prepared-statement-cache-size>
</database>
<database jndi-name="jdbc/iap_db">
<driver>
<type>org.postgresql.Driver</type>
<url>jdbc:postgresql://localhost:5432/iap_db</url>
<user>imart</user>
<password>imart</password>
<init-param>
<param-name>preparedStatementCacheQueries</param-name>
<param-value>0</param-value>
</init-param>
</driver>
<max-connections>20</max-connections>
<prepared-statement-cache-size>0</prepared-statement-cache-size>
</database>
<database jndi-name="jdbc/default">
<driver>
<type>org.postgresql.Driver</type>
<url>jdbc:postgresql://localhost:5432/default</url>
<user>imart</user>
<password>imart</password>
<init-param>
<param-name>preparedStatementCacheQueries</param-name>
<param-value>0</param-value>
</init-param>
</driver>
<max-connections>20</max-connections>
<prepared-statement-cache-size>0</prepared-statement-cache-size>
</database>
root/password, postgres/postgres というユーザで ssh 接続が、postgres/postgres, imart/imart というユーザで pgAdmin で接続できるようになっています。
あくまで検証用の Docker としてこのように設定しているため、セキュリティにはご注意ください。
このように、Docker イメージ化するだけで、検証用の PostgreSQL をすぐに準備できます。
是非ご活用ください。