DevOps

How to List / Start / Stop Docker Containers

How to list, start, or stop Docker containers?

Introduction

Docker is a popular virtualization tool that replicates a specific operating environment on top of a host OS. Each environment is called a container. Managing containers is essential for working in Docker.

A container uses an image of a preconfigured operating system optimized for a specific task. When a Docker image is launched, it exists in a container. For example, multiple containers may run the same image at the same time on a single host operating system.

This guide shows you how to list, stop, and start Docker containers.

How to list, start, or stop Docker containers?

Prerequisites

  • A Linux-based operating system
  • Access to a user account with root or sudo privileges
  • A preconfigured Docker installation with images

List Docker Containers

The basic format for using docker is:

docker command [options]

To list all running Docker containers, enter the following into a terminal window:

docker ps
no running containers on this image

As you can see, the image above indicates there are no running containers.

To list all containers, both running and stopped, add –a :

docker ps –a
all docker containers on a system listed

To list containers by their ID use –aq (quiet):

docker ps –aq
all docker containers listed by id

To list the total file size of each container, use –s (size):

docker ps –s

To list the latest created containers, use –l (latest):

docker ps –l
show latest docker container

The ps command provides several columns of information:

  • Container ID – a unique alphanumeric number for each container
  • Image – The base operating system image the container is based on
  • Command – The command that launched the container
  • Created – How long ago the container was created
  • Status – Uptime or downtime
  • Ports – Specifies any ports forwarded to the container for networking
  • Name – A memorable name assigned by the Docker software

Note: This guide assumes you already have an existing image. If you don’t, use the following:
docker pull name:tag
For example, enter docker pull ubuntu:14.04 to grab a copy of the Ubuntu 14.04 image.

Start Docker Container

The main command to launch or start a single or multiple stopped Docker containers is docker start:

docker start [options] container_id 
how to start a specific docker container

You can specify the container by either using its name or ID (long or short).

To create a new container from an image and start it, use docker run:

docker run [options] image [command] [argument] 

If you do not define a name for your newly created container, the deamon will generate a random string name. To define container name, use the ––name option:

docker run ––name=Ubuntu_Test ubuntu:14.04

The above mentioned command will create the Ubuntu_test container based on the ubuntu:14.04 image and start it.

A container may be running, but you may not be able to interact with it. To start the container in interactive mode, use the –i and –t options:

docker run –it ––name=Ubuntu_Test ubuntu:14.04
how to start a container in interactive mode

In the above mentioned example, the system creates the Test_2 container from the ubuntu image, and connects to it enabling you to run commands directly on the container.

Instead of using -i or -t options, use the attach command to connect to a running container:

docker attach container_id

Stop Docker Container

Use the docker stop command to stop a container:

docker stop [option] container_id

Replace container_id with the container’s name or ID.

By default, you get a 10 second grace period. The stop command instructs the container to stop services after that period. Use the --time option to define a different grace period expressed in seconds:

docker stop --time=20 container_id
stop a docker container with a custom --time

To immediately kill a docker container without waiting for the grace period to end use:

docker kill [option] container_id

To stop all running containers, enter the following:

docker stop $(docker ps –a –q)

The same command could be used with kill. This would stop all containers without giving them a chance to exit.

Conclusion

This tutorial provided options to list, start, and stop, Docker containers.

Docker is used by development teams to ensure consistency across different machines.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !