1

I have a Dockerfile and it ends with CMD command like below;

CMD ["npm", "start"]

So I want to add another command like below;

CMD java -javaagent:/work/dd-java-agent.jar

2 CMD entry not allowed for Dockerfile, how can I re-organize these commands in a single line?

Thanks!

yatta
  • 423
  • 1
  • 7
  • 22
  • Are these blocking commands? Which one do you want to run first? – Abhijit Sarkar Apr 28 '23 at 07:17
  • I guess it doesn't matter, second one is Datadog agent. – yatta Apr 28 '23 at 07:19
  • You'd almost always run two separate commands in two separate containers. In this setup it's not obvious what the two parts would have in common (one's a Node application and one Java) so I'd probably even run them with two separate images. – David Maze Apr 28 '23 at 10:41
  • [Why can't I use Docker CMD multiple times to run multiple services?](https://stackoverflow.com/questions/23692470/why-cant-i-use-docker-cmd-multiple-times-to-run-multiple-services) includes both some explanation and some other approaches. – David Maze Apr 28 '23 at 10:43

1 Answers1

0

Running two things in the same container is actually a bit of an anti-pattern for containers. However you can of course still do this and it's covered in the docker documnetation.

You use a script that does this like so:

#!/bin/bash

# Start the first process
npm start &
  
# Start the second process
java -javaagent:/work/dd-java-agent.jar &
  
# Wait for any process to exit
wait -n
  
# Exit with status of process that exited first
exit $?

Your dockerfile will look something like this:

COPY my_wrapper_script.sh my_wrapper_script.sh
CMD ./my_wrapper_script.sh
Rick Rackow
  • 1,490
  • 8
  • 19
  • 1
    How about something like this? --> CMD java -javaagent:/work/dd-java-agent.jar && npm start – yatta Apr 28 '23 at 07:48
  • 1
    @yatta you can, but with only one &. && means it will be executed after the end of the first command.. that as far as i understood it will never end. one & instead will launch it as a background process. anyway, i think the script proposed by Rick Rackow is far more clean and maintainable – Marco Frag Delle Monache Apr 28 '23 at 07:51
  • @rick This script&Dockerfile somehow gives error, "Container image "asdasdsa" already present on machine". – yatta Apr 28 '23 at 13:52