Docker Container Tutorial – Command and Lifecycle

FREE Online Courses: Click, Learn, Succeed, Start Now!

Today, we will see Docker Container Tutorial. Moreover, in this Docker tutorial, we will discuss why containers are used in Docker. Also, we will look at example and lifecycle of Container in Docker. Along with this, we will see Docker Container Command with syntax.

So, let’s start Docker Containers tutorial.

What is Docker Container?

Basically, instances of Docker images that can be run by using the Docker run command is what we call Containers. However, Docker’s basic purpose is to run containers.

By using the Docker API or CLI, we can create, start, stop, move, or delete a container. Moreover, on the basis of the current state of a container, we can connect a container to one or more networks, attach storage to it, or even can create a new image.

Additionally, a container is relatively well isolated from other containers as well as its host machine. Though, it is possible to control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.

Also, make sure that any changes to its state that are not stored in persistent storage, disappears, when we remove a container.

i. Example of Docker Run Command

Below command runs an Ubuntu container, attaches interactively to your local command-line session, and runs /bin/bash.

$ docker run -i -t ubuntu /bin/bash

Following happens, while we run this command (assuming you are using the default registry configuration):

Docker pulls ubuntu image from our configured registry if we do not have the ubuntu image locally.

We run a docker container create command manually, Docker creates a new container.

As its final layer, Docker allocates a read-write filesystem to the container. It permits a running container to create or modify files and directories in its local filesystem.

The container stops when we type exit to terminate the /bin/bash command, but not removed. Though, we can again start it or remove it.

Why Docker Container?

It virtualizes at the operating system level, with multiple containers running atop the OS kernel directly, rather than virtualizing the hardware stack as with the virtual machines approach.

It simply means that containers are far more lightweight, start much faster, and use a fraction of the memory compared to booting an entire OS. Some more advantages of using a Container in Docker are:

i. Consistent Environment

Here Consistent Environment means, Containers give developers the ability to create predictable environments which are isolated from other applications.

Also, Containers include software dependencies needed by the application, like specific versions of programming language runtimes as well as other software libraries.

ii. Run Anywhere

Basically, on many platforms like Linux, Windows, and Mac operating systems, virtual machines or bare metal, developer’s machine or in data centres on-premises or also in the public cloud, containers can run.

Docker Container and Layers

Well, the top writable layer is the major difference between a container and an image. The writable layer is also deleted when the container is deleted. The underlying image remains unchanged.

Also, ensure that all changes are stored in this container layer, multiple containers can share access to the same underlying image because each container has its own writable container layer.

In addition, to manage the contents of the image layers and the writable container layer, Docker uses storage drivers. All drivers use stackable image layers and the copy-on-write (CoW) strategy, even if each storage driver handles the implementation differently.

Docker Container Size on Disk

We can use the docker ps -s command, to view the approximate size of a running container. However, 2 columns relate to size.

Size: Here size refers to the amount of data (on disk) which we use for the writable layer of each container

Virtual size: And, virtual size refers to the amount of data which containers used for the read-only image data.

All of the running containers on disk use the total disk space, that space is the combination of each container’s size and the virtual size values.

Though, the total size on disk for these containers would be SUM (size of containers) plus one container’s (virtual size- size), if multiple containers started from the same exact image.

Docker Container Lifecycle

Here is the entire lifecycle of a container in Docker.

  • At first, the container will be in the created state.
  • Further, the container moves into the running state while we use the Docker run command
  • Though, to kill an existing container, we use the Docker kill command.
  • In order to pause an existing container, we use the Docker pause command.
  • Moreover, to pause an existing container, we use the Docker stop command.
  • And, to put a container back from a stopped state to a running state, we use the Docker run command.

Docker Container Command

i. docker top

We can see the top processes within a container by using the below command.

Syntax

docker top ContainerID

ii. docker stop

In order to stop a running container.

Syntax

docker stop ContainerID

iii. docker rm

To delete a container.

Syntax

docker rm ContainerID

iv. docker stats

To offer the statistics of a running container.

Syntax

docker stats ContainerID

v. docker attach

In order to attach to a running container.

Syntax

docker attach ContainerID

vi. docker pause

To pause the processes in a running container.

Syntax

docker pause ContainerID

vii. docker unpause

To unpause the processes in a running container.

Syntax

docker unpause ContainerID

viii. docker kill

To kill the processes in a running container.

Syntax

docker kill ContainerID

Conclusion

Hence, in this Docker Container Tutorial, we have learned all about Container in Docker in detail. Moreover, we discussed Why Containers used in Docker with its lifecycle.

Also, we looked at Docker Container Command with syntax. Still, if any doubt occurs, feel free to ask in the comment tab. We are happy to help!

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

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