Gerben Kranenborg
Gerben Kranenborg

Reputation: 41

Docker container stops right after starting Postgres DB

I am working on setting up Postgres 9.5 AS in Docker, and got everything installed. The issue however is, when I start the Docker Container, it appears that Postgres starts at first, but then the Container stops right away. (it does not show up with a docker container ls.). When I overwrite the Container startup with --entrypoint sh, and manually start Postgres, it all works fine.

I also checked with docker logs <container-id>, but that does not give me any info at all.

The setup is like this :

Dockerfile :

ENTRYPOINT ["/opt/edb/9.5AS/bin/init.sh"]

init.sh :

su enterprisedb -c '/opt/edb/9.5AS/bin/pg_ctl start -D /opt/edb/9.5AS/data'

From my command prompt I run :

docker run -it -v pgdata:/opt/edb/9.5AS/data <image_name>

It almost looks like it does start, but as soon as the start process is done, the shell stops, and as a result the Container stops as well.

So how to get it so the Container starts, Postgres starts and everything stays running, preferable in detached mode of course?

Upvotes: 2

Views: 1745

Answers (1)

Gerben Kranenborg
Gerben Kranenborg

Reputation: 41

After researching some more, I found the answer in part also by finding clues on Stackoverflow.

Anyway, I modified my init.sh script to look like this :

/opt/edb/9.5AS/bin/pg_ctl start -D /opt/edb/9.5AS/data exec
"$@"

And the Dockerfile now ends like below :

USER enterprisedb
ENTRYPOINT ["/opt/edb/9.5AS/bin/init.sh"]
CMD ["/bin/bash"]

The core of the solution is the last line in the init.sh script as well as the last line in the Dockerfile. Both combined make it so that once the DB started, a new shell (/bin/bash) gets started. This will run in the foreground, thus keeping the Container alive. By starting the container in detached mode, it now does exactly what we need it to do.

Upvotes: 2

Related Questions