4

I saw for example in the Dockerfile of the postgres image (https://github.com/docker-library/postgres/blob/master/10/Dockerfile) that at the end the startup of the container is defined like this:

ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]

If I understand this right, the argument postgres is transferred into the docker-entrypoint.sh so $1 in the script is replaced with postgres and the script will be executed.

Now my question is if I can define my own Dockerfile based on the postgres image (FROM postgres) but overwrite the CMD of the base Dockerfile and accomplish to first execute a command on startup and then execute the docker-entrypoint.sh with the postgres argument?

Something like this:

FROM postgres
...
CMD <my-command> && [“postgres”]
Palmi
  • 2,381
  • 5
  • 28
  • 65

2 Answers2

3

You can create you own my-entrypoint.sh

$ cat my-entrypoint.sh
#!/bin/bash

#do what you need here e.g. <my-command>

#next command will run postgres entrypoint will all params
docker-entrypoint.sh "$@"

And your docker file will look as follows

FROM postgres

# your stuff

ENTRYPOINT ["my-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]
khoroshevj
  • 1,077
  • 9
  • 15
0

Yes you can do such a thing

For the CMD command in a Dockerfile, you have 3 possible syntaxes

Extract from

https://docs.docker.com/engine/reference/builder/#cmd

CMD ["executable","param1","param2"] (exec form, this is the preferred form) CMD ["param1","param2"] (as default parameters to ENTRYPOINT) CMD command param1 param2 (shell form)

So you can use any, for doing what you want, the shell form (the last one) seems well suited

You can also launch a shell script that does all your stuff

user2915097
  • 30,758
  • 6
  • 57
  • 59
  • I think there is a misunderstanding. Or could you please give an example of the syntax what I want to accomplish – Palmi Feb 28 '18 at 15:31
  • read also https://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile?rq=1 – user2915097 Feb 28 '18 at 15:46
  • root execution of the PostgresSQL is not permitted – Palmi Feb 28 '18 at 17:57
  • I guess you have not created a non privileged user, and so your Dockerfile does not have a `USER` directive. You should post your Dockerfile – user2915097 Feb 28 '18 at 18:05
  • But isn’t the postgres entrypoint script supposed to create this user (see https://github.com/docker-library/postgres/blob/master/10/docker-entrypoint.sh) – Palmi Feb 28 '18 at 18:18
  • see https://hub.docker.com/_/postgres/ extract `docker run -it --rm --user 1000:1000...` so it uses a nonprivileged user – user2915097 Feb 28 '18 at 18:53
  • So than how do I accomplish this? – Palmi Feb 28 '18 at 18:54
  • does this form "CMD command param1 param2 (shell form)" is also parameters for ENTRYPOINT please ? – Webwoman Sep 14 '18 at 01:12