How restart a stopped docker container

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.


It should be

$ docker restart container_id # OR
$ docker restart container_name

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.


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.

Tags:

Docker