简体   繁体   English

Dockerfile:ENTRYPOINT和CMD

[英]Dockerfile: ENTRYPOINT and CMD

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: 例如,我在postgres映像的Dockerfile( https://github.com/docker-library/postgres/blob/master/10/Dockerfile )中看到,容器的启动定义如下:

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. 如果我理解这一权利,则将参数postgres传输到docker-entrypoint.sh因此脚本中的$1postgres替换,脚本将被执行。

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? 现在我的问题是,是否可以基于postgres映像( FROM postgres )定义自己的Dockerfile,但覆盖基本Dockerfile的CMD并完成以在启动时首先执行命令,然后使用postgres参数执行docker-entrypoint.sh

Something like this: 像这样:

FROM postgres
...
CMD <my-command> && [“postgres”]

You can create you own my-entrypoint.sh 您可以创建自己的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 而且您的docker文件将如下所示

FROM postgres

# your stuff

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

Yes you can do such a thing 是的,你可以做这样的事情

For the CMD command in a Dockerfile, you have 3 possible syntaxes 对于Dockerfile中的CMD命令,您有3种可能的语法

Extract from 从中提取

https://docs.docker.com/engine/reference/builder/#cmd 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 因此,您可以使用任何形式的shell形式(最后一种形式)看起来很适合

You can also launch a shell script that does all your stuff 您还可以启动一个shell脚本来完成所有工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM