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.
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)
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
Post a Comment