I am trying to create a Docker for a PostgreSQL database and to import some data into it. I have followed the official documentation.
The Dockerfile looks like this:
FROM postgres:14.10
#Set env vars
ENV PGDATA=/var/pgdata
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=admin
ENV POSTGRES_HOST_AUTH_METHOD=trust
# Copy backup to container
COPY opr.dump /opr.dump
COPY entrypoint2.sh /docker-entrypoint-initdb.d/entrypoint2.sh
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["postgres"]
The entrypoint script looks like this:
#!/bin/bash
set -e
# Create the Postgres database
createdb opr
# Extract the schema and data from the backup file
pg_restore -U postgres -d opr /opr.dump
Everything works fine but after importing the data the container stops, both in attached and detached mode. I copied the ENTRYPOINT and CMD from the official image but without success.
I should mention that I tried also without copying ENTRYPOINT and CMD and the container still stops.
My question is how do I make the container not stop after importing the data?
docker exec psqlafter the database container is started up. The important detail here is that you can't runpg_restoreuntil the database is actually running, which makes it hard to run in an entrypoint script. The stock entrypoint script has logic to handle this, which @ABWassim's answer uses.