Tuesday, April 26, 2016

Make PostgreSQL as Docker Image




# Start empty Ubuntu container
docker run -it ubuntu bash

# Show current Ubuntu version
lsb_release -a
(See instruction detail from http://www.postgresql.org/download/linux/ubuntu/)

vi /etc/apt/sources.list.d/pgdg.list
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main

apt-get install wget
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

apt-get update

apt-get install postgresql-9.4 postgresql-client-9.4 postgresql-contrib-9.4

exit

### DOCKER COMMIT PH1 ###
docker commit postgresql nutthaphon/postgresql-9.4

-- Place data on VirtualBox folder sharing   ** NOW not working
docker run -it -v /cygdrive/d/TEMP/postgresdata:/data nutthaphon/postgresql-9.4 bash


root@3435dcd9bad7:/data# su postgres --command "/usr/lib/postgresql/9.4/bin/initdb -D /data/main"
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /data/main ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /data/main/base/1 ... LOG:  could not link file "pg_xlog/xlogtemp.37"                                                                     to "pg_xlog/000000010000000000000001": Operation not permitted
FATAL:  could not open file "pg_xlog/000000010000000000000001": No such file or directory
child process exited with exit code 1
initdb: removing contents of data directory "/data/main"

-- Place data on Docker volume (Union Filesystem) see more.
docker create -v /data --name postgresdata ubuntu
docker run -it --name postgres_service --volumes-from postgresdata nutthaphon/postgresql-9.4 bash


cd /etc/postgresql/9.4/main
cp postgresql.conf /data/postgresql.conf
cp pg_hba.conf /data/pg_hba.conf
cp pg_ident.conf /data/pg_ident.conf

cd /data
sed -i '/^data_directory*/ s|/var/lib/postgresql/9.4/main|/data/main|' postgresql.conf
sed -i '/^hba_file*/ s|/etc/postgresql/9.4/main/pg_hba.conf|/data/pg_hba.conf|' postgresql.conf
sed -i '/^ident_file*/ s|/etc/postgresql/9.4/main/pg_ident.conf|/data/pg_ident.conf|' postgresql.conf

mkdir -p /data/main
mkdir -p /var/run/postgresql/9.4-main.pg_stat_tmp
chown postgres /data/*
chgrp postgres /data/*
chmod 700 /data/main
su postgres --command "/usr/lib/postgresql/9.4/bin/initdb -D /data/main"


The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /data/main ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /data/main/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/lib/postgresql/9.4/bin/postgres -D /data/main
or
    /usr/lib/postgresql/9.4/bin/pg_ctl -D /data/main -l logfile start


sed -i "/^#listen_addresses/i listen_addresses='*'" postgresql.conf
sed -i "/^# DO NOT DISABLE\!/i # Allow access from any IP address" pg_hba.conf
sed -i "/^# DO NOT DISABLE\!/i host all all 0.0.0.0/0 md5\n\n\n" pg_hba.conf


#### Startup database
su postgres --command "/usr/lib/postgresql/9.4/bin/postgres -D /data/main -c config_file=/data/postgresql.conf" &

#### Shutdown database
su postgres --command '/usr/lib/postgresql/9.4/bin/pg_ctl --pgdata=/data/main stop'



### Create database
su postgres --command 'createuser -P -d -r -s docker'
su postgres --command 'createdb -O docker docker'

### DOCKER COMMIT PH2 ###
docker commit postgres_service nutthaphon/postgresql-9.4:first_use


### Upload to Docker Hub
docker login --username=nutthaphon --email=nutthaphon@gmail.com
docker push nutthaphon/postgresql-9.4:first_use

### Start PostgresSQL container
docker run -it --name postgres_service --volumes-from postgresdata -d -p 5432:5432 nutthaphon/postgresql-9.4:first_use su postgres --command "/usr/lib/postgresql/9.4/bin/postgres -D /data/main -c config_file=/data/postgresql.conf"




### Test connect to docker database with pgAdminIII

new connection

pgAdminIII


****** Some of instruction come from Amattn.com and Docker.com


No comments:

Post a Comment