docker / awesome-compose

Awesome Docker Compose samples
https://docs.docker.com/compose/
Creative Commons Zero v1.0 Universal
34.57k stars 6.64k forks source link

How did you manage to declare two "CMD" in a single Dockerfile? #311

Closed Rony-dot closed 1 year ago

Rony-dot commented 1 year ago

Your dockerfile

https://github.com/docker/awesome-compose/blob/e3ea3e9044dcae2ed409665d29434fa53a5a2198/sparkjava-mysql/backend/Dockerfile#L25

https://github.com/docker/awesome-compose/blob/e3ea3e9044dcae2ed409665d29434fa53a5a2198/sparkjava-mysql/backend/Dockerfile#L31

Problem / Question:

mikesir87 commented 1 year ago

You are correct that a container image can have only one defined CMD. Therefore, when a second is specified, it replaces the previously defined value.

This case is a little different because the Dockerfile has multiple stages (the multiple FROM statements). The first CMD is for the stage named dev-env while the second is for the final stage.

Why would you do something like this? There are times in which you might want to build or target a container image, but only end at a specific stage. This particular project isn't doing that at the moment though (which maybe it should!).

I do this quite often in development environments where an intermediate stage might be watching for code changes and rebuilding my app, while the final stage is production ready. Using this approach, you can have everything defined within a single Dockerfile. Make sense?

Rony-dot commented 1 year ago

That makes sense ! Thank you for explaining this so well.

itJunky commented 5 months ago

Why u start app in production instance like a developmen, with manage.py runserver? Need to use uwsgi or gunicorn for production.