Search code examples
dockergunicorn

Use one CMD for DEV and another CMD for PROD in the same Dockerfile


I have in my Dockerfile :

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "nerul.wsgi:application"]

But docker run -p 8000:8000 -v ${PWD}:/app nerul-docker doesn't work because it says "ModuleNotFoundError: No module named 'nerul'" even though my directory structure is correct

docker run -p 8000:8000 nerul-docker works but there's no hot-reload.

So how do I make my Dockerfile such that for dev it has

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

while for PROD it has

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "nerul.wsgi:application"]

?


Solution

  • You can provide an alternate command when you run the container. Anything you put after the docker run command replaces the Dockerfile CMD.

    So, you can run for example,

    docker run -p 8000:8000 \
      -v "$HOME/src/other-app:/app" \
      ./manage.py runserver 0.0.0.0:8000
    

    As I hint in the specific -v line, this means you aren't using any of the code in the image, only the Python interpreter from the base image and any libraries you RUN pip install. If you RUN any other commands to set up the source tree, that work will be lost in this setup. Correspondingly, this setup may "work on your machine" but isn't really related to the image you're eventually deploying. You might find it simpler to use the Python interpreter that's already on your (MacOS or Linux) host system.