6

When I use the postgres docker image, I run

docker run --name mydb -e POSTGRES_PASSWORD=password -d postgres

And I see a container running.

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                       NAMES
a36b98h8982a        postgres            "docker-entrypoint.s…"   36 minutes ago      Up 36 minutes       5432/tcp                    mydb

So I make my Dockerfile because I want to initialize my database

FROM postgres:latest

COPY pg-setup.sql /docker-entrypoint-initdb.d/

CMD ['postgres']

But when I use the Dockerfile the container just exits immediately. How do I get it to run like the postgres image?

1
  • There may be something wrong with the pg-setup.sql script. Could you post the logs? Commented Jan 15, 2019 at 7:20

1 Answer 1

7

If your SQL file pg-setup.sql is no problem, you can just change your Dockerfile like this:

FROM postgres:latest
COPY pg-setup.sql /docker-entrypoint-initdb.d/

Take a look at another case. It will be helpful.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, there is no need to override the CMD in your dockerfile. The command from the image will be used.
this doesnt work at all for me: COPY failed: stat /var/lib/docker/tmp/docker-builder335488150/init.sql: no such file or directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.