awesome-compose icon indicating copy to clipboard operation
awesome-compose copied to clipboard

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

Open Rony-dot opened this issue 2 years ago • 2 comments

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:

  • On both line 25, and line 31 has "CMD" commands.
  • But from Official Documentation we know that:
    • There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect Reference Dockerfile for #CMD

  • So, what is actually happening in this case?

Rony-dot avatar Dec 03 '22 07:12 Rony-dot

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?

mikesir87 avatar Dec 03 '22 15:12 mikesir87

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

Rony-dot avatar Dec 04 '22 14:12 Rony-dot

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

itJunky avatar May 28 '24 13:05 itJunky