I have a Python/PostgreSQL project I'm trying to put in a Docker container. When I try to run psql commands inside the container to create a user and a database while building the image, I get this error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Here's one Dockerfile:
FROM python:2.7-slim
WORKDIR /app
ADD . /app
RUN apt-get update && apt-get install -y postgresql postgresql-contrib libgeos-c1
RUN service postgresql start
RUN su postgres bash -c "psql -c \"CREATE USER myuser WITH PASSWORD 'password';\"" \
&& su postgres bash -c "psql -c \"CREATE DATABASE db WITH OWNER = myuser;\""```
Notice that I even try to start the postgresql service before psql commands.
I tried another approach which is to use base image postgres:9.6.3
and install Python pip on it instead. I still get the same error. Here's the second Dockerfile for reference:
FROM postgres:9.6.3
WORKDIR /app
ADD . /app
RUN apt-get update && apt-get install -y python-pip libgeos-c1
RUN service postgresql start
RUN su postgres bash -c "psql -c \"CREATE USER myuser WITH PASSWORD 'password';\"" \
&& su postgres bash -c "psql -c \"CREATE DATABASE db WITH OWNER = myuser;\""
Confession: I'm a docker noob.