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

Closed
opened 2026-03-07 21:21:22 -06:00 by GiteaMirror · 3 comments
Owner

Originally created by @rony-io on GitHub (Dec 3, 2022).

Your dockerfile

e3ea3e9044/sparkjava-mysql/backend/Dockerfile (L25)

e3ea3e9044/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?
Originally created by @rony-io on GitHub (Dec 3, 2022). ### 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](https://docs.docker.com/engine/reference/builder/#cmd) - So, what is actually happening in this case?
Author
Owner

@mikesir87 commented on GitHub (Dec 3, 2022):

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 commented on GitHub (Dec 3, 2022): 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?
Author
Owner

@rony-io commented on GitHub (Dec 4, 2022):

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

@rony-io commented on GitHub (Dec 4, 2022): That makes sense ! **Thank you** for explaining this so well.
Author
Owner

@itJunky commented on GitHub (May 28, 2024):

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

@itJunky commented on GitHub (May 28, 2024): Why u start app in production instance like a developmen, with manage.py runserver? Need to use uwsgi or gunicorn for production.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/awesome-compose#663