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


Have Questions? Post them here!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.