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.
1 Answer
you have three main ways to run a command after the container starts:
- with
docker exec -d someContainer some commandfrom the command line, - with
CMD ["some", "command"]from your Dockerfile - with
command: some commandfrom 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"]
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.
-
8These methods you described will not override the CMD or ENTRYPOINT in the image docker is pulling in? That was my main requirement– MladenCommented Apr 8, 2022 at 11:33
-
1Of course, if don't work i recommend to include your code in the question, because probably you are doing something wrong Commented Apr 8, 2022 at 22:05
-
I have a tomcat image and I am trying to create a file relying on injected secrets in the build. I tried using
command: echo `cat /run/secret/sec` > x.params && /usr/local/tomcat/bin/startup.shbut it is making tomcat exit with code 0 without running. Am I missing something?– RafsCommented May 9, 2024 at 9:05 -
Is there a way to run these commands after the container is healthy?– RafsCommented May 28, 2024 at 12:53
-
Is it supposed to shut the container down right after the command runs? Because that's what I'm seeing. Commented Sep 9, 2024 at 20:06
docker inspect -f '{{.Config.Entrypoint}}' <image id>