Docker Community Forums

Share and learn in the Docker community.

How to persist an exited container

(Alstar) #1

Hi All,

I am trying to understand how to start an exited ubuntu container without stopping it again. I ran the below command to pull and install an ubuntu container on my machine:

docker container run --name ubuntu ubuntu (Notice that I did not use the -it option while creating this container)
I want to understand how do u start such a container, when i run a command docker container start ubuntu, it starts and immediately exits and I can verify that using docker container ps -a.

Even if i use docker container start -i ubuntu it doesn’t work. So my question is how can I start and persist this ubuntu container?


(Romgo) #2


I’m pretty new to docker. But this make sense, you just start a container with no process, so as soon as it as started it just stop. this is normal behaviour.

If you want to start a container with bash, you can run :

docker run -it debian:latest /bin/bash

But if you leave bash the container will just stop.

(Alstar) #3

Yea, i am new to Docker as well. I am just trying to see even though I didnt mention the bash argument interactively when I started this container, can I modify it at a later stage.

docker container run -it --name ubuntu_v2 ubuntu bash --> This works and even if I exit the bash shell, I notice that the container is stopped, however I can start that with docker container start ubuntu_v2.

Where if I created the container with docker container --name ubuntu ubuntu, I wanted to know if I can change its properties and persist starting this container. Just trying to understand this…


(Romgo) #4

If you change something in the container, stop it and start it again, changed will be lost. if that is your question.

(Alstar) #5

Hi Romgo,

No, I know that if you change something in a container and then stop it and start it again, I will not lose the info. for example if I install a utility called “htop” on my ubuntu container and then stop and restart it, I will still have “htop” installed.

What my question really was how can i start and keep the container running if I have created it without a -it parameter.
for example : docker container run --name ubuntu ubuntu

(Sam) #6

the design of Docker is a runtime with a single app. as long as that app keeps running, the container continues to run.

the ubuntu container has bin/bash as its ‘application’. in daemon mode (docker run -d) there is nothing for bash to do, so it exits, and then the container stops running… in interactive mode (docker run -it) bash starts and is waiting for commands to be entered… if you type exit, then bash ends, and the container stops.

SO, to make the ubuntu container stay running, you need some command that does not return. for example the sleep command

so, if u started the container like this

docker run -d ubuntu sleep 1000000

it will stay running for 1 million seconds.

you can examine an image to see what its startup pgm is by using

docker inspect image_name
for that container the info is

        "Config": {
            "Hostname": "706cb4504c90",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": null,
            "Cmd": [
                "/bin/bash"    <----------------------
            "Image": "e04c66a223c45a6247237510c40117cef92acb0a4355f1ba90580ef6274b490d",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,   <------- no entrypoint function
            "OnBuild": null,
            "Labels": {}