Run multiple startup commands in Docker.

Yashod Perera
2 min readJun 1, 2020

Wait! What? Can we do this here? Yes

When you use docker you might have a need of having more than one startup command. But it wont allow you to do following.

CMD ["Some-command"]
CMD ["Other-command"]

And following error will occur and inform you that you can only have one startup command.

Okay then how to solve this? There are two methods, one is hard and other is easy.

First let’s learn the hard way and then move to the easy way.

Hard way of running multiple startup commands.

  • Add one startup command to your docker file and run it docker run <image-name>
  • Then open the running container using docker exec command as follows and run the desired command using sh program.
docker exec -it <container-id> sh

This might okay with one or two containers and then what about hundreds of containers manage with k8s ? This is very hard and might not useful. Then let’s move with the easy way.

Easy way of running multiple startup commands.

  • Make a script file with the startup commands which you want to run. Important — Make sure to run them as background if needed.
  • Add the script to run as the startup command.

Wow it is easy. Yes it is.

Hope you get a better understanding about run multiple startup commands in docker.

If you have found this helpful please hit that 👏 and share it on social media :)

--

--

Yashod Perera

Technical Writer | Tech Enthusiast | Open source contributor