Most of us, especially developers and administrators working in the software industry, must be familiar with the term Docker. Docker has become a standard in the IT industry when it comes to packaging, deploying, and running distributed applications with ease. Today, we will be learning about Docker end to end.
The aim of this Docker tutorial is to make you learn about Docker basics. Additionally, we will dive into more concepts such as virtualization, containerization, need for Docker, Docker architecture, Docker installation, Docker images, and so on.
Here is the list of topics included in this best Docker Tutorial:
Let us learn Docker from scratch!
Watch this DevOps Tutorial video on YouTube:
What is Docker?
Docker is a platform that is used to containerize our software, using which we can easily build our applications and package them, with the dependencies required, into containers, and these containers are easily shipped to run on other machines.
Docker simplifies the DevOps Methodology by allowing developers to create templates called “images,” using which we can create lightweight virtual machines called “containers.” Docker makes things easier for software developers by giving them the capability to automate infrastructure, isolate applications, maintain consistency, and improve resource utilization. There might arise a question that such tasks can also be done through virtualization, then why choose Docker over it. It is because virtualization is not as efficient.
Why? We shall discuss this as we move along in this Docker Tutorial.
To begin with, let us understand, what is virtualization?
Prepare yourself for top MNCs by going through these DevOps Interview Questions and Answers!
What is virtualization?
Virtualization refers to importing a guest operating system on the host operating system and allowing developers to run multiple OS on different VMs while all of them run on the same host, thereby eliminating the need to provide extra hardware resources.
These virtual machines are being used in the industry in many ways:
- Enabling multiple operating systems on the same machine
- Cheaper than the previous methods due to less or compact infrastructure setup
- Easy to recover and do maintenance if there is any failure
- Faster provisioning of applications and resources required for tasks
- Increase in IT productivity, efficiency, and responsiveness
Let us check out the working of VMs with the architecture and also understand the issues faced by them.
The Docker Cheat Sheet by Intellipaat is very handy. Do look into the blog.
What is a virtualization host?
From the above VM architecture, it is easy to figure out that the three guest operating systems acting as virtual machines are running on a host operating system. In virtualization, the process of manually reconfiguring hardware and firmware and installing a new OS can be entirely automated; all these steps get stored as data in any files of a disk.
Virtualization lets us run our applications on fewer physical servers. In virtualization, each application and OS live in a separate software container called VM. Where VMs are completely isolated, all computing resources, such as CPUs, storage, and networking, are pooled together, and they are delivered dynamically to each VM by a software called hypervisor.
However, running multiple VMs over the same host leads to degradation in performance. As guest OSs have their own kernel, libraries, and many dependencies running on a single host OS, it takes a large occupation of resources such as processor, hard disk, and, especially, RAM.
Also, when we use VMs in virtualization, the bootup process takes a long time that affects efficiency in the case of real-time applications. In order to overcome such limitations, containerization was introduced.
How did containerization overcome these issues? Let us discuss this further in this basic Docker Tutorial.
Become a master of DevOps by enrolling in this online DevOps Training in London!
In the next section of this step-by-step Docker tutorial, let us understand what exactly is containerization.
What is containerization?
Containerization is a technique where virtualization is brought to the level of an OS. In containerization, we virtualize OS resources. It is more efficient as there is no guest OS consuming host resources. Instead, containers utilize only the host OS and share relevant libraries and resources, only when required. The required binaries and libraries of containers run on the host kernel leading to faster processing and execution.
In a nutshell, containerization (containers) is a lightweight virtualization technology acting as an alternative to hypervisor virtualization. Bundle any application in a container and run it without thinking of dependencies, libraries, and binaries!
Now, let us look into its advantages:
- Containers are small and lightweight as they share the same OS kernel.
- They do not take much time, only seconds, to boot up.
- They exhibit high performance with low resource utilization.
Now, let us understand the difference between containerization and virtualization in this Docker container tutorial.
Go for the best DevOps Course in New York to get a better understanding of DevOps!
Containerization vs virtualization
As we have been introduced to containerization and virtualization, we know that both let us run multiple OSs on a host machine.
Now, what are the differences between containerization and virtualization? Let us check out the below table to understand the differences.
|Virtualizes hardware resources
||Virtualizes only OS resources
|Requires the complete OS installation for every VM
||Installs the container only on a host OS
|A kernel is installed for every virtualized OS
||Uses only the kernel of the underlying host OS
In the case of containerization, all containers share the same host OS. Multiple containers get created for every type of application making them faster but without wasting the resources, unlike virtualization where a kernel is required for every OS and lots of resources from the host OS are utilized.
We can easily figure out the difference from the architecture of containers given below:
In order to create and run containers on our host OS, we require software that enables us to do so. This is where Docker comes into the picture!
Now, in this tutorial, let us understand the Docker architecture.
Docker uses a client-server architecture. The Docker client consists of Docker build, Docker pull, and Docker run. The client approaches the Docker daemon that further helps in building, running, and distributing Docker containers. Docker client and Docker daemon can be operated on the same system; otherwise, we can connect the Docker client to the remote Docker daemon. Both communicate with each other by using the REST API, over UNIX sockets or a network.
The basic architecture in Docker consists of three parts:
- Docker Client
- Docker Host
- Docker Registry
- It is the primary way for many Docker users to interact with Docker.
- It uses command-line utility or other tools that use Docker API to communicate with the Docker daemon.
- A Docker client can communicate with more than one Docker daemon.
In Docker host, we have a Docker daemon and Docker objects such as containers and images. First, let us understand the objects on the Docker host, then we will proceed toward the functioning of the Docker daemon.
- Docker objects:
- What is a Docker image? A Docker image is a type of recipe or template that can be used for creating Docker containers. It includes steps for creating the necessary software.
- What is a Docker container? A type of virtual machine that is created from the instructions found within the Docker image. It is a running instance of a Docker image that consists of the entire package required to run an application.
- Docker daemon:
- Docker daemon helps in listening requests for the Docker API and in managing Docker objects such as images, containers, volumes, etc. Daemon issues to build an image based on a user’s input, and then saves it in the registry.
- In case we do not want to create an image, then we can simply pull an image from the Docker hub, which might be built by some other user. In case we want to create a running instance of our Docker image, then we need to issue a run command that would create a Docker container.
- A Docker daemon can communicate with other daemons to manage Docker services
- Docker registry is a repository for Docker images that are used for creating Docker containers.
- We can use a local or private registry or the Docker hub, which is the most popular social example of a Docker repository.
Now that we are through the Docker architecture and understand how Docker works, let us get started with the installation and workflow of Docker and implement important Docker commands.
If you have doubts or queries related to DevOps, get them clarified from DevOps experts on our DevOps Community!
For installing Docker on Windows and macOS, the process is quite simple. All we have to do is download and install Docker from Download Docker, which includes Docker client, Docker machine, Compose (Mac only), Kitematic, and VirtualBox.
On the other hand, in the case of Linux, there are several steps that we need to follow. Let us check them out.
To install Docker on the Ubuntu box, first, we need to update its packages. To do so, type the below command in the terminal:
sudo apt-get update
As we are using this command on sudo, after we hit Enter, it will ask for a password. Provide the password and then follow the steps given further in this Docker tutorial.
Now, we must install its recommended packages. For that, just type the below-mentioned command:
sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual
Now, we have successfully installed the prerequisites for Docker. Press “y” to continue further.
sudo apt-get install docker- engine
Let us move forward in this docker tutorial and install the Docker engine.
The Docker installation process is complete now. Use the below-mentioned command to verify if Docker is installed correctly.
sudo service docker start
You will get an output as start: Job is already running: docker
This means that Docker has been started successfully.
Running a Container
After the installation of Docker, we should be able to run the containers. If we do not have a container to run, then Docker will download the image in order to build the container from the Docker hub and then will build and run it.
We can run a simple “hello-world” container to cross-check if everything is working properly. For that, run the below-mentioned command:
docker run hello-world
Hello from Docker!
This message shows that the installation appears to be working correctly.
Now, let us move forward in this Docker tutorial to understand the Docker workflow.
Typical Local Workflow
Docker’s typical local workflow allows users to create images, pull images, publish images, and run containers.
Let us understand this typical local workflow from the diagram below:
Dockerfile, here, consists of the configuration and the name of the image pulled from a Docker registry such as a Docker hub. This file basically helps in building an image from it, which includes the instructions about container configuration, or it can be image pulling from a Docker registry.
Let us understand this process in a little detailed way:
- It basically involves building an image from a Dockerfile that consists of instructions about container configuration or image pulling from a Docker registry.
- When this image is built in our Docker environment, then we should be able to run the image, which, further, creates a container.
- In our container, we can do any operations such as:
- Stopping the container
- Starting the container
- Restarting the container
- These runnable containers can be started, stopped, or restarted just like how we operate a virtual machine or a computer.
- Whatever manual changes, such as configurations or software installations, are made, these changes in a container can be committed to making a new image, which can, further, be used for creating a container from it later.
- At last, when we want to share our image with our team or the world, we can easily push our image into the Docker registry.
- One can easily pull this image from the Docker registry using the pull command.
Interested in becoming a DevOps expert? Go for the online DevOps Course in Toronto!
Pulling an Image from the Docker Registry
The easiest way to obtain an image, to build a container from, is to find an already prepared image from Docker’s official website.
We can choose from various common software, such as MySQL, Node.js, Java, Nginx, or WordPress, on the Docker hub as well as from the hundreds of open-source images made by common people around the globe.
For example, if we want to download the image for MySQL, then we can use the pull command:
docker pull mysql
In case we want the exact version of the image, then we can use:
docker pull mysql:5.5.45
REPOSITORY TAG IMAGE VIRTUAL SIZE<none> <none> 4b9b8b27fb42 214.4 MB
mysql 5.5.45 0da0b10c6fd8 213.5 MB
When we run this command, we can observe the created image with the repository name <none>.
Now, to add the identity of the repository, we can use the following command:
docker build –t test-intellipaat .
After -t, we can add any name of our choice to identify our repository.
REPOSITORY TAG IMAGE ID VIRTUAL SIZEtest-intellipaat latest 4b9b8b27fb42 214.4 MB
mysql 5.5.45 0da0b10c6fd8 213.5 MB
Now, in this Docker Tutorial, we shall customize an image manually by installing software or by changing configurations. After completion, we can run the Docker commit command to create an image of the running container.
Running an Image
In order to run a Docker image, all we need to do is use the run command followed by our local image name or the one we retrieved from the Docker hub.
Usually, a Docker image requires some added environment variables, which can be specified with the -e option. For long-running processes, such as daemons, we also need to use the -d option.
To start the “test-intellipaat” image, we need to run the command shown below, which configures the MySQL root user’s password as documented in the Docker hub MySQL repository’s documentation:
docker run -e MYSQL_ROOT_PASSWORD=root+1 –d test-intellipaat
To check the container running, use the command below:
This command lists all of our running processes, images, the name they are created from, the command that is run, ports that software are listening on, and the name of the container.
CONTAINER ID IMAGE COMMAND30645F307114 test-intellipaat “/entrypoint.sh mysql”PORTS NAMES
We can figure out, from the above output, that the name of the container is shubham_rana, which is an auto-generated one.
When we want to explicitly name the container, the best practice is to use the –name option that inserts the name of our choice at the container startup:
docker run –name intellipaat-sql -e MYSQL_ROOT_PASSWORD=root+1 -d est-mysql
We can easily name our container with this command.
Interested in getting an industry-recognized certification in DevOps? Enroll in Intellipaat’s DevOps Course in Bangalore now!
Stopping and Starting Containers
Once we have our Docker container up and running, we can use it by typing the docker stop command with the container name as shown below:
docker stop intellipaat-sql
In case we want to run our container again from the state in which we shut it down, we can use the start command as our entire container is written on a disk:
docker start intellipaat-sql
Now, let us see how we can tag an image.
Tagging an Image
Once we have our image up and running, we can tag it with a username, image name, and version number before we push it into the repository by using the docker tag command:
docker tag intellipaat-sql javajudd/est-mysql:1.0
Now, in this Docker Tutorial, let us see how we can push an image into the repository.
Pushing an Image into the Repository
Now, we are ready to push our image into the Docker hub for anyone to use it via a private repository.
- First, go to https://hub.docker.com/ and create a free account
- Next, login to the account using the login command:
- Then, input the username, password, and email address that we are registered with
- Finally, push our image, with our username, image, and version name, by using the push command
Within a few minutes, we will receive a message about our repository stating that our repository has been successfully pushed.
When we go back to our Docker hub account, we will see that there is a new repository as shown below:
DevOps Engineers are among the highest paid professionals in the technology domain. So why wait? Join DevOps Training in Hyderabad today!
We have already seen, in this Docker tutorial, how to list the running containers using the ps command, but now what we want is to list all the containers, regardless of their state. Well, to do that, all we have to do is add the -a option as shown below:
docker ps -a
Now, we can easily distinguish between which container we want to start with and which container we want to remove.
After using a container, we would usually want to remove it rather than having it lying around consuming disk space.
We can use the rm command to remove a container as shown below:
docker rm intellipaat-sql
We already know how to list all the locally cached images by using the images command. These cached images can occupy a significant amount of space, so in order to free up some space by removing unwanted images, we can use the rmi command as shown below:
docker rmi intellipaat-sql
Now, we know how to remove cached images, but what about the unwanted and unnamed images that we may end up generating during the debugging cycle of creating a new image? These images are denoted with the name <none>. We can remove them all by using the following command:
docker rmi $(docker images -q -f dangling=true)
Knowing which ports are exposed by a container beforehand makes our work a lot easier and faster, e.g., Port 3306 is for accessing a MySQL database and Port 80 is for accessing a web server. Using the port command, as shown below, we can display all exposed ports:
docker port intellipaat-sql
To display processing in a container, we can use the top command in Docker, which is very similar to the top command in Linux.
docker top intellipaat-sql
To execute commands in a running container, we can use the exec command.
For example, if we want to list the contents of the root of the hard drive, we can use the exec command as shown below:
docker exec intellipaat-sql ls /
We can gain access to the bash shell if we wish to ssh as root into the container. To do so, we can use the following command:
docker exec -it my-est-mysql bash
Note: All communications between Docker clients and Docker daemons are secure since they are already encrypted.
Get in touch with Intellipaat for a comprehensive DevOps Training and be a certified DevOps Engineer!
The run command is one of the most complicated commands of all the Docker commands. By using this command, we can perform various tasks such as configuring security and managing network settings and system resources such as memory, file systems, and CPU. We can visit the following link to see and understand how to do all of the above and more, by using the run command.
Read more on Docker and get started with Docker for Windows now!
A Dockerfile contains all the instructions, e.g., the Linux commands to install and configure the software. Dockerfile creation, as we already know, is the primary way of generating a Docker image. When we use the build command to create an image, it can refer to a Dockerfile available on our path or to a URL such as the GitHub repository.
The instructions in a Dockerfile are executed in the same order as they are found in the Dockerfile.
There can also be comments starting with the # character in the Dockerfile.
The following table contains the list of instructions available:
||The first instruction in the Dockerfile, it identifies an image to inherit from
||This instruction provides visibility as well as credit to the author of the image
||This instruction executes a Linux command to install and configure
||The final script or application which is used to bootstrap the container and make it an executable application
||This instruction uses a JSON array to provide default arguments to the ENTRYPOINT
||This instruction contains the name/value metadata about the image
||This instruction sets the environment variables
||This instruction copies files into the container
||This instruction is basically an alternative to the COPY instruction
||This sets a working directory for RUN, CMD, ENTRYPOINT, COPY, and/or ADD instructions
||The ports on which the container listens
||This instruction is to create a mount point
|| An instruction to run RUN, CMD, and/or ENTRYPOINT instructions
Docker machine is a command-line utility that is used to manage one or more local machines, which are usually run in separate VirtualBox instances, or remote machines that are hosted on Cloud providers, e.g., Amazon Web Services, Microsoft Azure, etc.
How to create a Local Machine?
Docker Toolbox comes with a default Docker machine named default. This is just to give us a taste of it and to get us started with, but we may need multiple machines later on to segment the different containers that are running. To do that, we can use the following command:
docker-machine create –d virtualbox intellipaat
This command will create a local machine by using a VirtualBox image named intellipaat.
If we want to list the machines that we have configured, we can run the following command:
Starting and Stopping Machines
We can start the Docker machine that we have created by using the following command:
docker-machine start intellipaat
Now that the Docker machine has started, we will have to configure the Docker command line, with which the Docker daemon should interact. We can use the following commands to do this:
docker –machine env intellipaat
eval “$(docker-machine env intellipaat)”
Now, to stop a machine, use the following command:
docker-machine stop intellipaat
Note: These start and stop commands will start and stop our VirtualBox VMs, and we can watch the state of the VMs changing while we run the commands, if we have the VirtualBox manager open.
From this Docker Tutorial, we provided a detailed understanding of Docker concepts, such as its workflow, its need, and useful Docker commands, along with Docker images and containers.
While, here, we covered quite a bit of Docker’s core functionality, there is still a lot to know. If you are looking forward to learn Docker, then you must go for a structured DevOps Training provided by Intellipaat, where you will work on various case-based scenarios, along with exhaustive topic-wise assignments, hands-on sessions, and various industry-based projects that will prepare you to grab a DevOps job in any reputed MNC.
If you are willing to enter into the DevOps domain or upskill yourself with this domain, then you must go with this DevOps Certification Training as it will help you to understand the most important tools and services that must be learned and practiced to become a successful and productive team member at your workplace.
Happy learning! 😊