Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defining multiple services on Dockerfile with single CMD

Say I have the commands:

rails server

and

bundle exec something:default

I understand my Dockerfile can only have 1 CMD line so is it ok to do:

CMD ["sh", "-c", "rail server && bundle exec something:default"]

As it's just those 2 commands, I don't want to COPY a script.sh then use that (hoping for a simple 1 line way).

Is there a best practice I should be aware of when running 2 services in one container?

like image 869
userMod2 Avatar asked May 23 '18 06:05

userMod2


People also ask

Can I have multiple CMD commands in Dockerfile?

Docker will always run a single command, not more. So at the end of your Dockerfile, you can specify one command to run. Not more.

How do I run multiple services in one container?

A container's main running process is the ENTRYPOINT and/or CMD at the end of the Dockerfile . It is generally recommended that you separate areas of concern by using one service per container. That service may fork into multiple processes (for example, Apache web server starts multiple worker processes).

Which tool is used to run multiple services in a single container?

Although Docker provides a Docker-compose tool for building and running multi-services applications in multiple containers. Docker-compose requires a YAML file to configure your multiple services.


2 Answers

It is best practice to run a single service in each container, but it is not always practical. I run CMD ["sh", "-c", "/usr/sbin/crond && php-fpm"] so I can use the laravel scheduler. So in answer to the question, yes the above does work.

like image 148
Jason Pascoe Avatar answered Oct 26 '22 17:10

Jason Pascoe


You can create a bash script

#!/bin/bash
rail server
bundle exec something:default

then add a COPY to your Dockerfile

COPY mycmd.sh /app

/app is your destination

and finally execute your script in a CMD step

CMD ["mycmd.sh"]

like image 2
Jorge Avatar answered Oct 26 '22 15:10

Jorge