Docker Community Forums

Share and learn in the Docker community.

Cannot start postgres server


(Donkeyx) #1

Hey guys,

I am trying to install and start postgres in the same container that i have an apache install. Everything seems to work ok apart from the start command, so i think i am missing something, either in how i am using docker or how i am starting it? If i login to the container i can start myself as the postgres user with ‘pg_ctl start’ fine, just not automatically.

what am i missing ;). do i maybe just have to set the run level or something?

ENV POSTGRES_DATA="/var/lib/pgsql/data"

RUN yum install -y
postgresql-server.x86_64
RUN chkconfig postgresql on

USER postgres
ENV PGDATA="/var/lib/pgsql/data"
ENV LOGDIR="${PGDATA}/postgresql.log"
ENV LANG=“en_AU.UTF-8”

RUN whoami
RUN env
RUN echo "-------------------------"
RUN initdb -E ‘UTF-8’ --lc-collate=‘en_AU.UTF-8’ --lc-ctype=‘en_AU.UTF-8’

RUN pg_ctl -w start


(David Maze) #2

It’s usually considered a best practice to run these in separate containers. You can use the standard postgres image, then use either Docker linking or a private Docker network to point your application at the database. That avoids this problem altogether.

Each of the RUN commands starts a Docker container, runs a single command, and creates a new image layer from the resulting filesystem. You can’t start a long-running process from a RUN command: either the RUN command won’t return (if it’s a foreground process), or it will start, write a pid file, and that pid file will be recorded in the image without the actual daemon running (if it’s a background process).

Usual good Docker design is one process per container. So in this case, you’d set up one container to run Postgres, and one container to run apachectl start -DFOREGROUND.

(You could make that “one process” some sort of init system, typically supervisord, and the other solution to your problem is to install supervisord and set it up to run both the database and the application together.)