How to run Postgres as a Docker container
PostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.
Just a quick overview for one should use the PostgreSQL:
- is developed by the PostgreSQL Global Development Group, a diverse group of multiple companies and individual contributors.
- is free and open-source software and is released under the PostgreSQL license, a liberal Open Source license, similar to the BSD or MIT licenses.
- is largely SQL compliant.
- tackles concurrency efficiently with its MVCC implementation, which achieves very high levels of concurrency.
- supports JSON and other NoSQL features like native XML support and key-value pairs with HSTORE. It also supports indexing JSON data for faster access.
Hope this helps of your wonder why it is better than other DBs out there like MySQL.
Now, Lets see how we can run it as docker container.
$ docker pull postgres
This will pull the latest stable release of PostgreSQL with the following OS release:
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"NAME="Debian GNU/Linux"VERSION_ID="9"VERSION="9 (stretch)"ID=debianHOME_URL="https://www.debian.org/"SUPPORT_URL="https://www.debian.org/support"BUG_REPORT_URL="https://bugs.debian.org/"
which makes the image size to around 312 MB.
alpineversions to reduce the image size to lesser than 30 MB.
$ docker search alpine-postgres
will show you multiple alpine versions out there.
Once the necessary image has been downloaded, you can verify it using,
$ docker images
Volumes are easy and encouraged way to mount the data generated and used by the containers.
Let's create a local directory to mount as volume
$ mkdir -p $HOME/docker/volumes/postgres
-poption creates the subdirectories if necessary.
We can do multiple things (create/run/mount) all at once in the following:
$ docker run --name ashkeys-docker -e POSTGRES_PASSWORD=ashkeys -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
What are we doing here:
run-- creates and runs the newly created container with options in case specified.
--name-- name of the container.
-e-- Exposes environment variable
dockeras value. There are others too namely
POSTGRES_USERsets the superuser name. If not provided, the superuser name defaults to postgres.
-d-- Runs the container in detached mode. By default, docker attaches the container. It is common to run in detached mode like running it in the background.
-p-- Bind port 5432 on
localhostto port 5432 within the container.
$HOME/docker/volumes/postgreson the host machine to the container side volume path
/var/lib/postgresql/datacreated inside the container. This ensures that postgres data persists even after the container is removed.
postgres-- Last one is the image from which we want to create the docker container.
Since our localhost port 5432 is bound to postgres container port, we can directly run our CRUD operations to
localhost:5432. Lets connect to the database.
$ psql -h localhost -U postgres -d postgres
You might have to install the
psqlas global dependencies using