-1

I am having a rough time trying to figure this out.

So I have a dockerfile based on an Ubuntu image. At the end of my dockerfile I have:

CMD django-admin startproject $PROJECTNAME

I was told in a previous post that the base Ubuntu image has a CMD to run /bin/bash so my command is actually overwriting this (not sure if this is relevant or not).

The problem I'm encountering is if I run:

docker run -i -t <containerid> 

Nothing happens.. docker ps shows no containers are running

But if I run:

docker run -i -t <containerid> /bin/bash

The container starts running, I am in the shell, and docker ps shows that this container is running. Everything works as expected, but my django project is not there and my understanding is that running /bin/bash overrides the CMD in the dockerfile, which means django-admin startproject never gets run.

From inside the container, I can run django-admin startproject $projectname and it creates the project with no issues, which tells me django and all its dependencies are installed, and my environment variables are being registered.

However, I still suspect that there maybe is an issue with my CMD in my dockerfile and I don't know where to go from here.

With my provided dockerfile, if I do not run the container with /bin/bash, the container will not run.

Output of docker images:

REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
<none>              <none>              cbec557d6362        About a minute ago   579 MB
<none>              <none>              6919c19b159a        16 hours ago 

docker run:

docker run -i -t cbec557d6362
david
  • 6,303
  • 16
  • 54
  • 91
  • Two related questions, did you build an image using your Dockerfile? Do you understand the difference between an [image and a container](http://stackoverflow.com/questions/23735149/docker-image-vs-container)? – Roman Nov 07 '16 at 20:01
  • Yes to both questions. – david Nov 07 '16 at 20:04
  • Could you include the output of `docker images` and the exact `docker run` command? Just want to confirm you are launching the correct image. – Roman Nov 07 '16 at 20:07
  • Okay I included it. – david Nov 07 '16 at 20:19
  • Are you able to join [this room](http://chat.stackoverflow.com/rooms/76919/docker)? That may be faster than comments – Roman Nov 07 '16 at 20:33

1 Answers1

2

I commented on the original issue but for whatever reason there is a new one now. When you run /bin/bash that becomes the command run in that container, a bash shell. You can do whatever you want in the container (like create a Django project) but when you exit that shell the container still stop (as containers stop when the PID 1 process, in this case, /bin/bash) exits.

My suspicion (I don't know Django super well) is that the command django-admin startproject $PROJECTNAME is being run, completes successfully then exits. It's PID 1, so the container stops. This is why docker ps shows nothing.

My suggestion would be to instead use RUN django-admin startproject $PROJECTNAME in your Dockerfile followed by CMD /bin/bash.

Build the container then run it and you should be in a bash shell, check if your project is created.

johnharris85
  • 17,264
  • 5
  • 48
  • 52