What is Docker?
Docker, in simple words is a tool that allows developers who build applications, to package them with all their dependencies into a ‘container’ which can easily be shipped to run on the host operating . Containers do not have high overhead and allow more efficient use of system and resources. Develop, ship and run are often heard Docker keywords and makes deploying an application far simpler. In this Docker tutorial, we’ll go through some of the basic blocks of Docker and how to begin to understand them.
What are containers and why should we use them?
Applications, in general, are deployed using VMs. These Virtual Machines essentially run applications on virtual hardware powered by the server’s host OS. VMs can provide full process isolation for applications. This means that a problem in the host OS does not affect the software, or vice-versa. But, the downside to this is computational overhead is quite a lot.
Containers on the other hand, provide most of the isolation at a much lower computing power cost. Which allows container-based applications to be deployed easily and consistently, owing to the predictability of the environment on which it will be run.
Containerization is bringing abstraction to the OS level. Whereas, virtualization brings abstraction to the hardware.
Installing Docker onto your computer:
I have a Windows Computer. So, I’m going to be guiding you through the process to install docker on the Windows platform. But in the chance you use MAC or Linux – don’t worry. The process is similar and fairly simple, for which you can follow the tutorials on the official page – for MAC and Linux
- Download Docker from the Docker Hub
2. Install Docker Desktop on Windows by double-clicking on Docker Desktop Installer.exe to run the installer. Then follow the instructions on the installation wizard.
3. Click Finish on the setup complete dialog and launch the Docker Desktop application.
4. Start Docker by searching and selecting Docker Desktop. When the whale icon in the status bar stays steady it is ready to be used.
To test that it runs properly, we can use the run command to download and run a simple HelloWorld Docker container. Which will download the hello-world image, and run it as a container.
sudo docker run hello-world
If you want to run the Ubuntu OS on Windows, you can download the Ubuntu Image by typing
docker run –it Ubuntu bash
Docker Hub
Docker Hub is a service on the cloud which lets the user download Docker images built by others. You can also upload your own Docker built images to the hub. Click here to go to the official Docker hub site
Step 1 : Sign-up on Docker hub.
Step 2 − Log into Docker Hub and you will be able to see the repositories
Step 3 − Next, let’s browse and find the image you want, for example the Jenkins image
Step 4 – Scroll and find the Docker pull command. Which can be used to download the Jenkins image onto the local Ubuntu server.
Step 5 − Run the following command:
sudo docker pull jenkins
To run Jenkins as a container on the Ubuntu machine., you need to run the following command.
sudo docker run -p 8080:8080 -p 50000:50000 jenkins
Docker – Images
An image is a combination of a file system and parameters, and forms the basis in Docker.
Displaying Docker Images
To display the list of all the images currently installed on the system, you can issue the following command.
sudo docker images
When we run the above command, it will produce something like the above. This server has three images: centos, newcentos, and jenkins. Each image has:
- TAG − This is used to logically tag images.
- Image ID − This is used to uniquely identify the image.
- Created − The number of days since the image was created.
- Virtual Size − The size of the image.
Downloading Docker Images
Images can be downloaded from Docker Hub using the Docker run command. Let’s see in detail how we can do this. For instance to download the centos image, and run the container:
sudo docker run centos
You will now see the CentOS Docker image downloaded. Now, if we run the Docker images command to see the list of images on the system, we should be able to see the centos image as well.
Removing Docker Images
The Docker images on the system can be removed via the docker rmi command. Let’s look at this command in more detail.
Few other widely used commands are:
docker images -q :to return only the Image ID’s of the images.
docker inspect : to see the details of an image or container.
Docker – Containers
The basic purpose of Docker is to run containers, which are essentially instances of Docker images.
Running a Container
We use the run command to run a container. For instance
sudo docker run –it centos /bin/bash : to run the instance of the CentOS system on the Ubuntu server. Then, press Crtl+P to return to the OS shell.
Listing of Containers
We can list all of the containers currently running on a machine using the docker ps command
sudo docker ps
Other commands are:
docker ps -a :used to list all of the containers on the system
docker history : to see all the commands that were run with an image via a container.
Docker – Working with Containers
docker top : to see the top-level processes within a container.
docker stop : to stop a running container.
docker rm : to delete a container.
docker stats : to provide the CPU and Memory utilization of a running container.
docker attach : used to attach to a running container, and after attaching to the Docker container, you see the process utilization in that container.
docker pause : to pause the processes in a current running container.
docker unpause : to reverse the pause, i.e., unpause the processes in a running container.
docker kill : to kill the processes in a running container.
This is just the tip of the iceberg of what you can do with Docker. But once you’ve installed and started to understand the commands, you can begin to explore the endless possibilities!