How restart a stopped docker container

Docker

Docker Problem Overview


I launch a docker container from an image with the following command:

$ docker run -d myimage /bin/bash -c "mycommand"

When "mycommand" is finished, the container is stopped (I suppose it is stopped), but it is not deleted, because I can see it with this command:

$ docker ps -a

Is there any way to restart this container with the same parameters and keep data generated by mycommand?

Docker Solutions


Solution 1 - Docker

Yes, when the initial command finish its execution then the container stops.

You can start a stopped container using:

docker start container_name

If you want to see the output of your command then you should add -ai options:

docker start -ai container_name

PS. there is a docker restart container_name but that is used to restart a running container - I believe that is not your case.

Solution 2 - Docker

First, $ docker ps -a shows all containers (the ones that are running and the stopped ones), so that is the reason you are not seeing your stopped container listed.

Second, you can easily start a stopped container running:

$ docker start container_name

Once the container has been started, you can run your command by:

$ docker exec -it container_name bash -c "mycommand"

The stuff you create in your container will remain inside your container as long as it exists. If you want to keep data even if your container is removed you can use a volume.

Solution 3 - Docker

It should be

$ docker restart container_id # OR
$ docker restart container_name

Solution 4 - Docker

enter image description here

From the above picture we see one container is up and other status is Exited.

When a container is exited we can still start it back up, because a container stop doesn't mean that it's like dead or cannot be used again we can very easily stop and then start containers again at some point in the future. To start a container backup we can take it's ID and then execute docker start and paste the ID end.

sudo docker start container_id

command for exited container in the above picture will be.

sudo docker start -a bba606a95392

Out put:

enter image description here

By the way: While restarting a container we can not replace the default command, as soon as you started up with the default command is set for the container, for example if we start our container overriding the default command let's see what happened:

enter image description here

Docker is thinking we are trying to start and attach multiple container at the same time.

So when we up a container and let it exit, we can start it back up again which is going to reissue the default command that was used when the container was first created. It is part of docker container lifecycle.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBob5421View Question on Stackoverflow
Solution 1 - DockerlmtxView Answer on Stackoverflow
Solution 2 - DockerlmiguelvargasfView Answer on Stackoverflow
Solution 3 - DockerDoan VuView Answer on Stackoverflow
Solution 4 - DockerRafiqView Answer on Stackoverflow