I have two containers a Flask and a Celery container. Both containers use similar configs except the CMD. How can I change the CMD to change based on an env variable?
if [ $CONTAINER = 'flask' ] ; then
CMD ["uwsgi", "--ini", "uwsgi.ini"] ; else
CMD ["celery", "--app=flask_project.celery_app.celery", "worker"];
fi
2
Answers
Instead of having two separate CMDs, have one CMD that can run either thing.
In shell syntax, this might look like:
…in a Dockerfile, this might look like:
The
exec
forces the copy of/bin/sh
to replace itself in-place with wsgi or celery, so it doesn’t exist in the process tree except for the short period it needs to make a decision.Instead of having two separate CMDs, have the CMD do the most common thing, and override the CMD if you need to do the other thing.
For example, you might say that the "most common" thing your container will do is to launch the Flask server
and if you just run the container it will do that
but you can also provide an alternate command after the
docker run
image name, and this will replace the DockerfileCMD
.If you’re in a Docker Compose setup, you can use the
command:
setting to specify this override, like