docker / awesome-compose

Awesome Docker Compose samples

Home Page:https://docs.docker.com/compose/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Rony-dot opened this issue · comments

Your dockerfile

CMD ["java", "-jar", "target/app.jar" ]

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?

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?

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