Custom Postgres installation with Docker
So i've decided to play around with Docker. I've played with Docker in the past but i haven't done anything too serious, i just took the official postgres Dockerfile and run it to see how it works. This is how i started my whole Docker exploration, i took the official postgres docker file that can be found here and made an image. Immediately i saw that it wasn't exactly what i wanted, i wanted something less generic. The things that i would like are :
- latest postgres version, compiled from source
- easily customisable pgdata and prefix
- custom postgresql.conf settings (mostly having it replication ready)
- custom shell environment
- some extensions
- data page checksums
- easy replica deployment
So i started creating my own Dockerfile that would fill my needs and at the same time i would learn some more about Docker, 2 birds one stone kind of thing. After several hours and some testing i came up with something that was working (link at the bottom). It still needs some work to be done but i plan maintaining it for my own use so feel free to use it if you like what i've done there.
Let me explain a bit how it works.
With :
ENV PGBINDIR=/home/postgres/pgsql
ENV PGDATADIR=/home/postgres/pgdata
I can customise the installation and the pgdata directories, I install all packages i need, note that comments on joe will be deleted! Then i add postgres user to sudoers (this dockerfile is not meant to be secure, ease of use is what i am looking for when it comes to a lab postgres instance).I -always- get the latest postgres version sources, compile and install (including extensions , documentation etc) set some environmental variables, configure postgres (postgresql.conf , pg_hba.conf) , put a sample recovery.done there so slaves can get it from pg_basebackup , install some extensions in template1, initdb with checksums, create a replication user and finally copy a very simple replica creation script to the docker image.
How to make it work
With Docker installed its pretty simple :
to create dockers own playground network
docker network create --subnet=10.0.0.0/16 garden
to build the image, from the directory you have the Dockerfile downloaded
docker build -t pg_dev .
to run a container with a specific ip in our kindergarten docker network:
docker run --name master --net garden --ip 10.0.0.2 -i -t pg_dev /bin/bash
docker run --name slave1 --net garden --ip 10.0.0.3 -i -t pg_dev /bin/bash
and docker ps should look like this :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7a38d4b11769 pg_dev "/bin/bash" About a minute ago Up About a minute 5432/tcp slave1
2c0266f942ea pg_dev "/bin/bash" About a minute ago Up About a minute 5432/tcp master
Keep in mind that i have started playing around with Docker just 2 days ago. There might be a better way to assign static ips that I'm not aware of (yet).
Now, say that you have put some data in master and you wanna create a replica on slave1, something like this should work :
From any slave container home dir :
postgres@7a38d4b11769:~$ ./mk_replica.sh
waiting for server to shut down.... done
server stopped
29044/29044 kB (100%), 1/1 tablespace
server starting
LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "pg_log".
-[ RECORD 1 ]----+--------
lag_bytes | 0
pid | 37
application_name | a_slave
The Dockerfile can be found here and hopefully it can help some people on setting up a lab fast and easy.
Thanks for reading.
- Vasilis Ventirozos
ReplyDeleteNice blog..! I really loved reading through this article... Thanks for sharing such an amazing post with us and keep blogging...
Devops online training
Best Devops online training
Devops online training in Hyderabad
Devops online training in india
Great blog created by you. I read your blog, its best and useful information.
ReplyDeleteAWS Online Training
Devops Online Training
Apllication Packaging Online Training
Find my blog post here
ReplyDeleteweb designer
salesforce developer
laravel developer
web developer
We as a team of real-time industrial experience with a lot of knowledge in developing applications in python programming (7+ years) will ensure that we will deliver our best in python training in vijayawada. , and we believe that no one matches us in this context.
ReplyDeleteI am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging
ReplyDeleteAWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery
[no anchor text]
ReplyDelete[no anchor text]
http://globexdocuments.co//
http://globexdocuments.co//
http://globexdocuments.co///
http://globexdocuments.co///
http://globexdocuments.co//
http://globexdocuments.co//
Online Training | Classroom | Virtual Classes
ReplyDeleteAWS Training in Hyderabad with 100% placement assistance
1860 testers placed in 600 companies in last 8 years
Real time expert trainers
Indutry oriented training with corporate casestudies
Free Aptitude classes & Mock interviews
Nice Information Your first-class knowledge of this great job can become a suitable foundation for these people.
ReplyDeletePython training institute in Hyderabad
Nice blog Such a piece of beneficial information! Thanks for sharing this useful information with us. Really great effort.
ReplyDeleteKeep posting more