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?
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.
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).
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.
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.
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With