How to stop and start a docker container


Example - Docker Stop and Start containers

Let us take a look at how to stop and start a docker container,

First let's create a container using the docker run command,

% docker run -id --name my_alpine alpine
a7fdf87c9868281057018c20cbfcaa0c8108214b4f2a151ff9f22d8627774c38

Let's see the details of the running docker containers using the docker ps command,

% docker ps
CONTAINER ID   IMAGE     COMMAND     CREATED          STATUS          PORTS     NAMES
a7fdf87c9868   alpine    "/bin/sh"   53 seconds ago   Up 52 seconds             my_alpine

Stop a docker Container

To stop a docker container make use of the command docker stop followed by the CONTAINER ID or the name of the docker container,

Example:
% docker stop my_alpine
my_alpine

Let's see if got stopped with the docker ps command again, but this time with a -a option,

% docker ps -a
CONTAINER ID   IMAGE     COMMAND     CREATED          STATUS                        PORTS     NAMES
a7fdf87c9868   alpine    "/bin/sh"   24 minutes ago   Exited (137) 1 minutes ago             my_alpine


Start a Start a stopped Docker Container

As you may have guessed we can start a stopped/exited container using docker start <container_name or id>

% docker start a7fdf87c9868
a7fdf87c9868
% docker ps
CONTAINER ID   IMAGE     COMMAND     CREATED          STATUS         PORTS     NAMES
a7fdf87c9868   alpine    "/bin/sh"   28 minutes ago   Up 2 minutes             my_alpine

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap