-2

I'm new to Docker, when I run my docker image with -it option, docker container finishes running, echoed "test" as indicated in my docker file and exited with zero. The expected behavior should be that docker keeps open a virtual terminal of the container: docker run -it test Here is my Docker file:

FROM centos
CMD echo test

I tried both CMD and ENTRYPOINT, and both shell form and exec form, the behavior is the same. However, if I replace the CMD by RUN in the docker file, the -it option can work normally.

Jason
  • 1
  • Possible duplicate of [Why docker container exits immediately](https://stackoverflow.com/questions/28212380/why-docker-container-exits-immediately) – David Maze Sep 04 '18 at 10:26
  • 1
    Containers don't have "virtual terminals". The design point is that a container runs a single reasonably-long-running service, and exits (discarding all of its state) if the service ever shuts down. – David Maze Sep 04 '18 at 10:27

1 Answers1

1

Docker requires main process to keep running in the foreground. Otherwise, it thinks that application is stopped and it shutdown the container.

In your case, -i means keep STDIN open even if not attached & -t means allocate a pseudo-tty. They are just meaningful when the container is running.

But the container running is not depends on the above flags, they depends on the main process. Here, it's echo test, it executes and then finish, so the container found the main process finish, it exited.

If you replace CMD with RUN, then it will use default CMD, that is /bin/bash, see dockerfile of centos here, and /bin/bash will not auto exit, so container not exit.

atline
  • 28,355
  • 16
  • 77
  • 113