david david - 1 year ago 91
Linux Question

Why does my Dockerfile CMD not work?

So at the end of my Dockerfile I have this:

CMD django-admin startproject whattt
CMD /bin/bash

When I create image and then run container, everything works as expected there are no errors, and no errors in the Docker log. However there are still some issues that I cannot seem to figure out.

The first and most important problem is that CMD django-admin startproject is not actually creating any project. AFTER I run the container, then I can manually run django-admin startproject and it works as expected. When I issue this as a CMD from the Dockerfile though, then no project gets created.

The second issue is after the django-admin line, I put a second CMD with /bin/bash so when I run the container it opens a shell (so I can go in and check if my django project was created). Will this create a problem or conflict with the previous django-admin line? If I remove this line, then when I run the container I have no way to open the shell and check if my django project is there do I ?

Any help would be appreciated, thanks.

Answer Source

“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.” via Dockerfile reference. So your first CMD will not take effects.

If you want execture the bash of your container, try docker exec command, and the document provides example commands so you can follow.