View Containers

View Running containers

A container is an instance of an image. So once you run an image you get an instance of a container. You can view all running containers with the following:

> docker ps

> docker container ls

Response:

CONTAINER ID      IMAGE       COMMAND                  CREATED           STATUS           PORTS                  NAMES

dfa45e069b37      nginx       "nginx -g 'daemon of…"   4 minutes ago     Up 4 minutes     0.0.0.0:8080->80/tcp   nifty_euclid

If there are no actively running containers at that time, the command will not return any results. eg the hello world app exited and is therefore no longer running.

View all containers

To see all containers on the system, including containers which have exited, run the following:

> docker ps -a

Response:

CONTAINER ID      IMAGE         COMMAND                 CREATED             STATUS          PORTS                 NAMES

dfa45e069b37      nginx         "nginx -g 'daemon of…"   6 minutes ago     Up 6 minutes     0.0.0.0:8080->80/tcp  nifty_euclid

f1034967a268      hello-world   "/hello"                 8 minutes ago     Exited (0) 10 minutes ago              lucid_sinoussi

collapse
Remove Container

remove a specific container

Remove a container using its name or ID.

> docker rm node

> docker rm 137948e8a084

remove all unused containers

Removing all containers one by can be difficult but you can do that with one command. The following will remove all stopped containers.

> docker container prune

collapse
Start up an exited/stopped container

If you have stopped a container you can start it up again.

> docker start nifty_euclid

> docker start dfa45e069b37

> docker start dfa4

collapse
Stop containers

You can stop a container from running using the containers name, Id or first few numbers of the Id.

> docker stop nifty_euclid

> docker stop dfa45e069b37

> docker stop dfa4

collapse
Attach or Open a terminal of a running container

If a container is running detached you can easily attach to it:

> docker attach dfa4

You may need to run commands on a running container therefore you need to open its terminal. You can specifiy the shell with "sh" or "bash" at the end.

> docker exec -it dfa45e069b37 bash

> docker exec -it dfa45e069b37 sh

-t or --tty allocates a pseudo TTY / simulates a terminal.

-i or --interactive keeps the terminal open when not attached to the container.

Once you are in try a ls command and then type exit when done.

$ ls

$ exit

collapse
View more details about containers

There are a number of commands to display more information of a running containers:

> docker logs dfa45e069b37

> docker top dfa45e069b37

> docker inspect dfa45e069b37

> docker stats

collapse
What else can I do with containers

The below line will give you commands you can use with containers:

> docker container --help

collapse

Check out these links for more info:

My Docker Samples