Postgresql – Running psql/postgres client in an ephemeral mode in a container

containersdockerpostgresql

I want to link two containers1 where one is a full-blown Postgres (9.3)/PostGIS server, and another one is pure client needing no configuration. Just bear with me – this is a for an evolutionary POC which will later emerge into reactive microservices architecture, and we'll abandon container linking. For now, I am experimenting.

My question is about the Postgres (9.3) client.

I would like to link client container to postgres server, and just run some trivial queries from psql from the container's bash prompt using either scheduled events (cron), or from a docker's exec command line.

For this, I would like to use psql in the dumbest possible way – no initdb, no pga_hba.conf, not postgres.conf, just do something like:

1. Link containers (assume I have pg_server container running):

docker run -it --link pg_server:pg_server_1 --name pg_client_1 pg_client

2. Run query from client (client_1) to running server container (pg_server_1):

docker exec pg_client_1 psql -U my_user -h pg_server_1 -p 5432 -d my_db -c "select uncertainty_estimate(' land_cover', manifold_envelope(-81.85, 6.8, -81.01, 7.8, 4326))"

So this is all docker stuff so far.

This is a Docker file I would like to use for my client. Notice it only installs Postgres.

FROM centos:6.8
MAINTAINER Me

ENV PG_FULL  "9.3"
ENV PG_VER "93"
ENV OS_MAJOR "6"
ENV OS_MINOR "8"

RUN rpm -ivh http://yum.postgresql.org/${PG_FULL}/redhat/rhel-${OS_MAJOR}-x86_64/pgdg-centos${PG_VER}-${PG_FULL}-1.noarch.rpm && \
    yum -y install postgresql${PG_VER}

USER postgres

Question: can I just run the container like this and get to psql prompt from a docker exec, or do I need to configure anything else so I can run a psql client in the client container against the server container (as I have shown above)? The problem is that the container dies as it executes. I need to keep it alive, probably by running some kind of a process in it. That still does not answer the question of the most ephemeral setup for postgres client.

1 – I will appropriately network them later in Docker Compose.

Best Answer

Currently your Dockerfile doesn't contain an execution script so it will finish & exit...

If you want it to always run, you can 'tail -f some file '

But, you can skip it and combine the 2 commands to run the psql command & exit:

docker run -it --link pg_server:pg_server_1 --name pg_client_1 psql -U my_user -h pg_server_1 -p 5432 -d my_db -c "select uncertainty_estimate(' land_cover', manifold_envelope(-81.85, 6.8, -81.01, 7.8, 4326))"

Docker treats the last arguments as a command to run directly.