Skip to main content

Docker Lifecycle And Useful Docker Commands

Maintaining a large software application is not that easy task since it may have lots of dependencies and OS related configurations. What if you can create an OS image which already contains required libraries and configurations that needs to run your application? Then that would be really easy for software deployer to deploy their application easily on a cloud without doing a tedious task of setting up an OS environment. One possible solution to overcome this problem is to use a Virtual Machine. You can install all libraries, setting up configurations and take an image. When you need to deploy the application, you can simply start the machine with that image. A VM provide complete low-level machine to run an Operating System. But it does not perform faster due to VM’s operational overhead. In this case Container technology comes to rescue.

Containers vs VMs

VM is nothing more than a computer that executes a program. VM is running on top of software which called Hypervisor. Here, the physical computer is known as the Host machine and the VM that running top of Hypervisor known as Guest Machine. Containers act the same as the VM, but the difference is that Containers use host machine OS instead of hardware visualization to run as a VM. Each container has its own userspace and runs on top of host OS. This makes container much faster than VMs.
Another thing about the docker is that you can keep docker images in a central repository. This repository is known as Docker Registry. Docker Registry works the same as GIT Repository. You can commit your current container as an image and push it into the Docker Registry. You can also pull images back from the Docker Registry. Docker Hub is a one such a Docker Registry that you can keep images. Following services are also widely used as Docker Registries.
  1. Quay
  2. Google Container Registry
  3. AWS Container Registry
A container can be compared with a process in OS. A process is an instance of a computer program that is being execting. A process can have multiple thread running. Containers are also working the same as a process, but the difference is that Containers are processing with their full environment. Containers can have the following states.
  • Created: A container that has been created. but not started
  • Restarting: A container that is in the process of being restarted
  • Started: A currently running container
  • Paused: A container whose processes have been paused
  • Exited: A container that ran and completed
  • Dead: A container that the daemon tried and failed to stop (usually due to a busy device or resource used by the container)
Docker life cycle

Common Commands in Docker Life Cycle

Here, below are the list of commands that used to change the Docker states.

docker pull

docker pull [OPTIONS] NAME[:TAG|@DIGEST]
This command pulls an image from a remote docker hub and saves it in your local machine. This command works same as Git pull command. As an example, you can pull an Ubuntu image from the Docker hub by executing the following command.
docker pull ubuntu
You can specify the version/tag of the image name by adding a colon(:) mark at the end of the image name. For example, to pull the latest Ubuntu, you can execute the command as follows.
docker pull ubuntu:latest

docker images

docker images [OPTIONS] [REPOSITORY[:TAG]]
You can see what are the available docker images by executing this command. To get all available docker images, run
docker images

docker ps

docker ps [OPTIONS]
You can use this command to list the containers. Following command can be used to list all the containers.
docker ps -a

docker create

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
This command creates a new Docker container by specified image as example mention below.
docker create ubuntu
once you created a new Docker container it moves its status to “created” status.

docker start

docker start [OPTIONS] CONTAINER [CONTAINER...]
This command can be used to start an exited container.
docker start 3a09b2588478
Here you need to define the container id that needs to start with the docker start command. Both create and start command can be executed by a single command by using the docker run command as follows.

docker run

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
This command creates a new container and runs the image in the newly created container. Example docker runs for Ubuntu image as follows.
docker run --name UbuntuImage -it ubuntu
here “-it” option allows you to connect to the container from your terminal. You can set a name for the container by setting value for “ — name” option.

docker attach

docker attach [OPTIONS] CONTAINER
This command let you attach local standard input, output and error stream to run a container. Here, the container should be in the running state in order to run this command.

docker stop

docker stop [OPTIONS] CONTAINER [CONTAINER...]
This command stops the running container. This will change the container status from running to excited. You can add docker container id at the end of the docker stop command as follows.
docker stop 3a09b2588478
Docker stop command sends SIGTERM signal to the container to stop the container gracefull. If the container cannot be stoped in timeout it sends SIGKILL signal to the container and stops the container. Alternatively, you can use the “docker kill” command to stop containers forcefully.

docker rm

docker rm [OPTIONS] CONTAINER [CONTAINER...]
This command removes container completely from your computer. So be careful when using this command. Example command as follows.
docker rm 3a09b2588478

docker pause

docker pause CONTAINER [CONTAINER...]
This command suspends the running process in running container by issuing SIGSTOP command.

docker unpause

docker unpause CONTAINER [CONTAINER...]
This command gets back suspended process into the running state.

Conclusion

Since containers are lightweight than the VM, containers perform much faster than VMs. Therefore, people are tending to use containers to deploy their services due to deployment overhead. Docker Registry provides an elegant way to keep your Container images in a repository and use it whenever you need it. Same as a process, Docker also has a life cycle. We can use Docker commands to change the state of the Docker life cycle so that we can start, stop, pause and unpause containers.
Hope this article will be helpful for you to understand the basics of the Container and see you in the next article. Cheers :)

Comments

Popular posts from this blog

Database Internel Architecture: SQLite

Introduction A database is an essential part of building a software system which used to store and read data efficiently. Here, We are going to discuss some architectural details of database implementation by using an early version of SQLite. SQLite is a small database application which used in millions of software and devices. SQLite invented by D.Richard Hipp in August 2000. SQLite is a high performance, lightweight relational database. If you are willing to learn internal of a database in coding level, then SQLite is the best open source database available out there with highly readable source code with lots of documentation. Reading later versions of SQLite become a little harder since it contains lots of new features. In order to understand the basic implementation of database internals, You should have good knowledge about data structures, some knowledge about Theory of Computing and how an operating system works. Here we are looking into the SQLite 2.5.0 version. Here

Weird Programming Languages

There are thousands of programming languages are invented and only about hundred of programming languages are commonly used to build software. Among this thousands of programming languages, there are some weird type of programming languages can be also found. These programming languages are seems to be called weird, since their programming syntax and the way it represent its code. In this blog we will look into some of these language syntax. Legit Have you ever wonder, when you come to a Github project that print hello world program, but you cannot see any codes or any content. Check this link  https://github.com/blinry/legit-hello  and you will see nothing in this repository. But trust me, there is hidden code in this project. If you see the  commit  section, you can reveal the magic. Yeah, you are right. Its storing hello world code in inside the git commit history. If you clone this project and run the following command, then you can see the hidden code in this project. g

Basic Concepts of the Kubernetes

Handling large software which has multiple services is a tedious, time-consuming task for DevOps engineer. Microservices comes into the rescue DevOps engineers from all these complicated deployment processes. Simply, each microservice in the system has it own responsibility to handle one specific task. The container can be used to deploy each of these micro-tasks as a unit of service. If you are not that familiar with Containers, read this article to get to know about Docker, Which is the most popular and widely used container technology to deploy microservices. As I described early, we can use single container to deploy a single service and container contain all required configurations and dependencies. Single service always faces a common problem of a single point of failure. In order to avoid single point failure, we need to set up another service such that if one service is getting down, next available service takes that load and continue to provide the service. Another requi