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?
2
Answers
In the end I managed to create a custom entrypoint script that did the trick.
The Dockerfile looks like this:
The entrypoint script looks like this:
You just need to copy your
entrypoint2.sh
script in thedocker-entrypoint-initdb.d
directory, as according to the documentation (see section Initialization scripts)Therefore no need to override the
ENTRYPOINT
and theCMD
.EDIT : looks like you will have to do it after the container starts, like David Maze pointed out.