How to work with Containers in Docker

Last updated on May 27 2022
Raigiri Patil

Table of Contents

How to work with Containers in Docker

Docker – Containers

Containers are instances of Docker images that can be run using the Docker run command. The essential purpose of Docker is to run containers. Let’s discuss how to work with containers.

Running a Container

Running of containers is managed with the Docker run command. To run a container in an interactive mode, first launch the Docker container.

sudo docker run –it centos /bin/bash

Then hit Crtl+p and you’ll return to your OS shell.

Page 1 Image 1 59
run

You’ll then be running in the instance of the CentOS system on the Ubuntu server.

Listing of Containers

One can list all of the containers on the machine via the docker ps command. This command is employed to return the currently running containers.

docker ps

Syntax

docker ps

Options

None

Return Value

The output will show the currently running containers.

Example

sudo docker ps

Output

When we run the above command, it’ll produce the subsequent result −

Page 2 Image 2 53
ps

Let’s see some more variations of the docker ps command.

docker ps -a

This command is employed to list all of the containers on the system

Syntax

docker ps -a

Options

  • ─a − It tells the docker ps command to list all of the containers on the system.

Return Value

The output will show all containers.

Example

sudo docker ps -a

Output

When we run the above command, it’ll produce the subsequent result −

Page 3 Image 3 24
ps -a

docker history

With this command, you can see all the commands that were run with an image via a container.

Syntax

docker history ImageID

Options

  • ImageID − This is the Image ID for which you want to see all the commands that were run against it.

Return Value

The output will show all the commands run against that image.

Example

sudo docker history centos

The above command will show all the commands that were run against the centos image.

Output

When we run the above command, it’ll produce the subsequent result −

Page 4 Image 4 16
centos

Docker – Working with Containers

In this chapter, we will explore in detail what we can do with containers.

docker top

With this command, you can see the top processes within a container.

Syntax

docker top ContainerID

Options

  • ContainerID − This is the Container ID for which you want to see the top processes.

Return Value

The output will show the top-level processes within a container.

Example

sudo docker top 9f215ed0b0d3

The above command will show the top-level processes within a container.

Output

When we run the above command, it’ll produce the subsequent result −

Page 5 Image 5 6
docker top

docker stop

This command is employed to stop a running container.

Syntax

docker stop ContainerID

Options

  • ContainerID − This is the Container ID which needs to be stopped.

Return Value

The output will give the ID of the stopped container.

Example

sudo docker stop 9f215ed0b0d3

The above command will stop the Docker container 9f215ed0b0d3.

Output

When we run the above command, it’ll produce the subsequent result −

Page 5 Image 6 14
stop

docker rm

This command is employed to delete a container.

Syntax

docker rm ContainerID

Options

  • ContainerID − This is the Container ID which needs to be removed.

Return Value

The output will give the ID of the removed container.

Example

sudo docker rm 9f215ed0b0d3

The above command will remove the Docker container 9f215ed0b0d3.

Output

When we run the above command, it’ll produce the subsequent result −

Page 6 Image 7 6
remove

docker stats

This command is employed to provide the statistics of a running container.

Syntax

docker stats ContainerID

Options

  • ContainerID − This is the Container ID for which the stats need to be provided.

Return Value

The output will show the CPU and Memory utilization of the Container.

Example

sudo docker stats 9f215ed0b0d3

The above command will provide CPU and memory utilization of the Container 9f215ed0b0d3.

Output

When we run the above command, it’ll produce the subsequent result −

Page 7 Image 8 5
stats

docker attach

This command is employed to attach to a running container.

Syntax

docker attach ContainerID

Options

  • ContainerID − This is the Container ID to which you need to attach.

Return Value

None

Example

sudo docker attach 07b0b6f434fe

The above command will attach to the Docker container 07b0b6f434fe.

Output

When we run the above command, it’ll produce the subsequent result −

Page 8 Image 9 4
attach

Once you have attached to the Docker container, you can run the above command to see the process utilization in that Docker container.

Page 8 Image 10 3
process utilization

docker pause

This command is employed to pause the processes in a running container.

Syntax

docker pause ContainerID

Options

  • ContainerID − This is the Container ID to which you need to pause the processes in the container.

Return Value

The ContainerID of the paemployed container.

Example

sudo docker pause 07b0b6f434fe

The above command will pause the processes in a running container 07b0b6f434fe.

Output

When we run the above command, it’ll produce the subsequent result −

Page 9 Image 11 2
pause

docker unpause

This command is employed to unpause the processes in a running container.

Syntax

docker unpause ContainerID

Options

  • ContainerID − This is the Container ID to which you need to unpause the processes in the container.

Return Value

The ContainerID of the running container.

Example

sudo docker unpause 07b0b6f434fe

The above command will unpause the processes in a running container: 07b0b6f434fe

Output

When we run the above command, it’ll produce the subsequent result −

Page 9 Image 12 2
unpause

docker kill

This command is employed to kill the processes in a running container.

Syntax

docker kill ContainerID

Options

  • ContainerID − This is the Container ID to which you need to kill the processes in the container.

Return Value

The ContainerID of the running container.

Example

sudo docker kill 07b0b6f434fe

The above command will kill the processes in the running container 07b0b6f434fe.

Output

When we run the above command, it’ll produce the subsequent result −

Page 10 Image 13 1
kill

Docker – Container Lifecycle

The subsequent illustration explains the entire lifecycle of a Docker container.

Page 11 Image 14
lifecycle
  • Initially, the Docker container will be in the created
  • Then the Docker container goes into the running state when the Docker run command is employed.
  • The Docker kill command is employed to kill an existing Docker container.
  • The Docker pause command is employed to pause an existing Docker container.
  • The Docker stop command is employed to pause an existing Docker container.
  • The Docker run command is employed to put a container back from a stopped state to a running

Docker – Architecture

The subsequent image shows the standard and traditional architecture of virtualization.

Page 12 Image 15
virtualization
  • The server is the physical server that is employed to host multiple virtual machines.
  • The Host OS is the base machine such as Linux or Windows.
  • The Hypervisor is either VMWare or Windows Hyper V that is employed to host virtual machines.
  • You would then install multiple operating systems as virtual machines on top of the existing hypervisor as Guest OS.
  • You would then host your applications on top of each Guest OS.

The subsequent image shows the new generation of virtualization that is enabled via Dockers. Let’s have a look at the various layers.

Page 12 Image 16
various layers
  • The server is the physical server that is employed to host multiple virtual machines. So this layer remains the equivalent.
  • The Host OS is the base machine such as Linux or Windows. So, this layer remains the equivalent.
  • Now comes the new generation which is the Docker engine. This is employed to run the operating system which earlier employed to be virtual machines as Docker containers.
  • All of the Apps now run as Docker containers.

The clear advantage in this architecture is that you don’t need to have extra hardware for Guest OS. Everything works as Docker containers.

So, this brings us to the end of blog. This Tecklearn ‘How to work with Containers in Docker’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Docker and build a career in DevOps domain, then check out our interactive, Containerization using Docker Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/containerization-using-docker/

Containerization using Docker Training

About the Course

Tecklearn has specially designed this Containerization using Docker Training Course to advance your skills for a successful career in this domain. his Docker training online course will help you learn Docker containerization, running Docker containers, Docker image creation, Dockerfile, Docker orchestration, security best practices and more through hands-on projects and case studies. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Docker.

Why Should you take Containerization using Docker Training?

  • Average salary of Docker Expert is $110k – Indeed.com
  • According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.
  • Intuit, PayPal, Splunk, Uber & many other MNC’s worldwide use Docker across industries

What you will Learn in this Course?

Introduction to DevOps

  • What is Software Development
  • Software Development Life Cycle
  • Why DevOps?
  • What is DevOps?
  • DevOps Lifecycle
  • DevOps Tools
  • Benefits of DevOps
  • How DevOps is related to Agile Delivery
  • DevOps Implementation

Containerization using Docker – Part 1

  • Introduction to Docker
  • Understanding Docker Lifecycle
  • Docker Architecture
  • Components of Docker Ecosystem
  • Common Docker Operations
  • Committing changes in a Container
  • Hands On

Containerization using Docker – Part 2

  • Building Custom Docker Images
  • Docker Image
  • Introduction to Docker Swarm
  • Deploying a 2-Node Cluster using Docker Swarm
  • Hands on

 

 

0 responses on "How to work with Containers in Docker"

Leave a Message

Your email address will not be published. Required fields are marked *