docker-compose,如何在容器启动后如何运行bash命令,而不覆盖图像码头中的CMD或入口点?

发布于 2025-01-19 21:47:03 字数 128 浏览 3 评论 0原文

我只想重命名一些文件,而不覆盖 docker 拉入的 wordpress 图像内的命令。 在 docker-compose.yml 中,我尝试使用“命令”和“入口点”来运行 bash 命令,两者基本上都会中断图像内部发生的事情,并且全部失败。

I just want to rename a few files, without overriding the commands inside the wordpress image that the docker is pulling in.
Inside the docker-compose.yml I tried using 'command' and 'entrypoint' to run bash commands, both basically interrupt what's happening inside the image and it all fails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬﹪忆之殇 2025-01-26 21:47:03

您有三种主要方法可以在容器开始之后运行命令

  1. 使用docker exec -d somecontainer从命令行中使用一些命令
  2. 带有cmd [“一些” ,“命令”],带有命令的dockerfile
  3. :docker-compose文件中的某些命令,

如果这些文件都不适合您,则可能,您在做错了什么。一个常见的错误是在您的docker-compose文件中使用多个命令,就像:

version: '3.8'
services:
  someService:
    command: a command
    command: another command

这是不起作用的,因为最后一个命令覆盖了上面的命令,您应该做的是condentate命令

version: '3.8'
services:
  someService:
    command: a command && another command

:查看此问题

编辑:我忘记包括的一件事是,上面的同样的行为对dockerfile中的 cmd 是正确的,您不能做到这一点:

CMD ["some", "command"]
CMD ["another", "command"]

取而代之的是,您应该像Docker-Compose一样加入命令:

CMD ["some", "command", "&&", "another", "command"]

但是,如果您有很多命令,这很无聊,因此替代方案是使用带有所需的所有命令的shell脚本并在Dockerfile中执行它:

#!/bin/sh
# bash file with your commands

run wordpress && rename files && do something else

# later in your Dockerfile
CMD ["sh", "/path/to/file.sh"]

请参阅此问题

因为您没有提供任何代码,但您也可以使用>运行命令将重命名为最后一个命令(在CMD之前使用它)在您的Dockerfile中以在构建时间重命名这些文件(IMHO更有意义,因为当您时,这是您应该做的事情正在构建您的图像)。因此,如果您需要更多帮助,请也包括您的代码。

you have three main ways to run a command after the container starts:

  1. with docker exec -d someContainer some command from the command line,
  2. with CMD ["some", "command"] from your Dockerfile
  3. with command: some command from a docker-compose file

if none of these is working for you, probably, you are doing something wrong. A common mistake is using multiple command in your docker-compose file, like so:

version: '3.8'
services:
  someService:
    command: a command
    command: another command

this doesn't work, because the last command overrides the commands above, what you should do is concatenate the commands:

version: '3.8'
services:
  someService:
    command: a command && another command

take a look at this question.

edit: one thing i forgot to include is that the same behavior above is true to CMD in your Dockerfile, you can't do this:

CMD ["some", "command"]
CMD ["another", "command"]

instead, you should concatenate the commands, just like the docker-compose:

CMD ["some", "command", "&&", "another", "command"]

but this is very boring if you have a lot of commands, so an alternative is to use a shell script with all the commands you need and execute it in your Dockerfile:

#!/bin/sh
# bash file with your commands

run wordpress && rename files && do something else

# later in your Dockerfile
CMD ["sh", "/path/to/file.sh"]

see this question

As you haven't provided any code it's hard to say, but also, maybe you can use RUN command to rename as the last command(just before the CMD if you are using it) in your Dockerfile to rename these files at build time(what IMHO makes more sense because this is kind of thing you should do when you are building your images). So if you want more help, please, include your code too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文