1

So the last line in my Dockerfile is:

CMD Django-admin startproject testproject

I'm new to Docker and I just learned that I can only use the CMD line once in a Dockerfile and that running /bin/shell in the following example overrides the CMD:

docker run -it <containerid> /bin/bash

My question is how do I open a shell without overriding the Django-admin startproject CMD? I have looked into docker attach but this does not seem to open a shell. I feel like I am missing something obvious here. I just want to run my container and go into the shell and poke around and ensure my Django project is actually being created.

david
  • 6,303
  • 16
  • 54
  • 91

1 Answers1

4

You can simply run container without overwriting command in background:

docker run -d --name container_name <image_name>

And next you can attach to running container with new session:

docker exec -it container_name bash
Slawomir Jaranowski
  • 7,381
  • 3
  • 25
  • 33
  • I am running into a problem here, If I run the 'docker run' command you posted, docker ps shows no containers running. However, if I do a docker run with /bin/bash then docker ps shows the container running. So the problem is I cannot attach to the container because it is not running (with your solution) – david Nov 07 '16 at 03:36
  • @david, you need to check CMD and `docker logs` then, it is nor running, because the CMD is finished (program exit, or has errors so was not able to start, and so on) – Evedel Nov 07 '16 at 03:43
  • I have removed the CMD from the Dockerfile all together so the issue is no longer related to CMD. If I run the docker run command posted by Slawomir, there are no containers running. If I do a docker run with -it, the container is running and there are no errors. – david Nov 07 '16 at 03:53
  • I will make a new post for this issue. – david Nov 07 '16 at 03:53
  • http://stackoverflow.com/questions/40457681/why-wont-my-docker-container-run-unless-i-use-i-t – david Nov 07 '16 at 04:05