Stop and Start a Docker container

Published: November 16, 2022

Stop and Start a Docker container

In previous article, we have covered how to run a Docker container. We won't get into specifics of running a container here. We shared it, because we can only stop a running container or we need to first start it.

Before we could stop and start a Docker container, we need to have either NAME of the container or its CONTAINER ID. This is just to identify the container. To get either of these two, we can use the following command -

docker container ls -a

where -a option is used to list all containers irrespective of their status. The output is divided in seven fields which get us the CONTAINER ID and NAME of the container.

Stop a Docker container

So, we have got either the CONTAINER ID or NAME of the container with us. Now, to stop a container -

docker container stop <container-name>

or,

docker container stop <container-id>

For instance,

docker container stop ABC
docker container stop ba017416ac98

It would take a while for container to stop.

Start a Docker container

We need to follow a similar process here. Just use start in place of stop in above commands. Apart from that, use -i option if you want to attach containers' standard input/output (STDIN).

Use either CONTAINER ID or NAME of the container to start a container -

docker container start <container-name>

or,

docker container start <container-id>

For instance,

docker container start ABC
docker container start ba017416ac98

As already covered, to attach containers' standard input/output -

docker container start -i <container-name>
docker container start -i <container-id>

In conclusion, we have covered how to stop and start a Docker container here.