sss
sss

Reputation: 678

Can I run container from an image twice

I used docker run command, in interactive mode, to downloaded a docker image for busybox and create a container for the same.

After exiting from the container, I tried run command again, without interactive mode, to create another container of busybox from the image already present in the local repo. While checking the list of active containers through ps, I do not see any new container running, but in the list of all containers ps -a I see new container created and Exited(0).

Though I can start an inactive container through its id, I am wondering why run command is exiting the container after creating it.

docker cli

Upvotes: 1

Views: 1572

Answers (1)

Maxim Zaslavsky
Maxim Zaslavsky

Reputation: 18065

This is the expected behavior of using busybox non-interactively. That's because the busybox image just runs sh.

To see that, take a look at the Dockerfile:

FROM scratch
ADD busybox.tar.xz /
CMD ["sh"]

A Docker container will stop as soon as the process that runs in the container terminates. So when you start busybox non-interactively, the shell will exit, and so the container will terminate.

If you have a Docker image with a process capable of running non-interactively, you can use -d to keep the container alive in non-interactive mode. (If that fails, I suggest taking a look at docker logs [containername] to see what's happening under the hood.)

For example, this will run indefinitely non-interactively: docker run -d --rm busybox sh -c "while :; do sleep 500; done"

Upvotes: 2

Related Questions