Run multiple startup commands in Docker.

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 :)




Technical Writer | Tech Enthusiast | Open source contributor

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How To Efficiently Use Common Meta Tags for Your Website?

Using WorkManager in Android

How Do SQL Database Engines Work?

How Does DevOps Handle Change Management?

How to pass Professional Scrum Master (PSM I) & Professional Scrum Product Owner…

11 Flutter widgets every developer should know to build beautiful applications

Writing text Files using Print function !

Arithmetic Expressions and Precedence rule

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yashod Perera

Yashod Perera

Technical Writer | Tech Enthusiast | Open source contributor

More from Medium

Build and Deploy Docker Container in GitLab

Hands-on Docker

Setup Master Slave Replication with MySQL