Docker is an advanced OS virtualization software platform that makes it easier to create, package, deploy and run distributed applications in a Docker container. It helps in simplifying the process and accelerates the workflow with the help of in-built libraries and other dependencies. Docker offers flexibility and portability to run business applications in various locations. In this tutorial, you will learn all the essential concepts of Docker. We have covered each concept that starts from the introduction and will explore all its components of the architecture in detail. Now, let go through the concepts of Docker.
1. Introduction to Docker
In this module, you will learn what is docker, concepts of virtualization and containerization. Docker is a container platform that places your software and builds the applications and packages them by implementing necessary dependencies. These containers are then shipped to run on other machines. It enables the Docker users to create templates called "images" through which you can create lightweight virtual machines known as "containers". Docker lets you automate infrastructure, isolate applications, maintain consistency, and improve resource utilization.
Virtualization is the process of importing a guest operating system on your host operating system. It allows you to run multiple OS on various VMs that run on the same host. It avoids utilizing extra hardware resources and increases productivity, efficiency, and responsiveness. The below picture illustrates the working of VM’s.
The picture depicts three guest operating systems running on a host OS that serves as VM. All the resources such as CPUs, storage, networking are pooled together. This is known as a hypervisor where the resources are delivered dynamically to each VM through software.
The process of bringing virtualization to an operating system level is known as containerization. The container virtualizes OS resources. It utilizes only the host OS and will share only the relevant libraries and resources when required. Containers are small and carry the lightweight process which shares the same OS kernel. It consumes very little time to boot-up and performs high with lower resource utilization. The below picture illustrates the components of a container.
To create and run the containers on your host OS, the software is required and this is where Docker comes into action.
2. Advantages of using Docker
Docker containers can run on any data center, desktop, and cloud environment without any modifications by offering improved and seamless productivity. Docker automatically creates the container based on the application source code. It enables reusing the containers as base images or templates. Docker offers shared container libraries. An open-source registry can be accessed by developers which include thousands of user-contributed containers. It enables tracking the version of a container image and can be rolled back to previous versions.
Docker enables fast and consistent delivery of applications by making the developers work in a standardized environment with the use of local containers that provide the applications and services. Containers are very useful for continuous integration and continuous delivery workflows. The portable workloads and its lightweight process make it easy to manage dynamic workloads by scaling up applications and services in real-time. It provides a feasible and cost-effective preference for hypervisor-based virtual machines. It is also best for high-density environments and for small and medium deployments.
3. Docker Applications
Docker is specially designed for developers and system administrators. Using Docker, developers can build and package an application with all its dependencies into a Docker run container that can run in any environment. On single hardware, Docker can run many containers as they are lightweight and can run directly within the host machine’s kernel.
Developers can work in a standard environment that helps in streamlining the development lifecycle and minimizes the inconsistency between various environments. Docker enables continuous integration and continuous delivery workflows that make the development environment repeatable. At every stage, the team is aware of the development process and works in the same environment. In this tutorial, you will comprehend how Docker helps in sharing the work with other team members using containers. Docker allows you to push the application into a test environment to conduct automated or manual tests efficiently.
The unpredicted circumstances can halt the software development lifecycle and affect the business organization significantly. The Docker enables the functionality to duplicate the file or Docker image to new hardware and retrieve it later in case of any issues. Docker can quickly revert to the last version of the Docker image with the support of the rollback features.
The software can be deployed using Docker without worrying about accidental events. Docker helps during any hardware failure or configuration problem event wherein it becomes crucial to resume the workflow quickly. This tutorial provides an in-depth understanding of backup functionality and disaster recovery.
Delivery of Applications
The user can develop, test, and deploy the Docker applications fastly. Docker enables developers to find bugs at the initial stages of development so that they can be fixed in the development environment and can be deployed again for testing and validation. This makes the deployment easy and quick by pushing the updated software into the production environment.
Docker can run multiple Docker containers in a single environment by providing a highly portable environment. It balances the testing environment and the production environment with the help of code management. Docker enables a consistent environment for code development and deployment.
Docker also simplifies DevOps by standardizing the configuration interface and making it accessible to all the team members. The Docker containers have made the development more scalable and user-friendly. It ensures a standardized interface for all team members.
4. Docker Architecture
The constitutes three phases which comprise Docker build, Docker pulls and Docker run following a client-server architecture. The Docker Architecture constitutes three phases that comprise Docker build, Docker pull and Docker run. The process begins when the client approaches the Docker daemon which helps the user to build, run, and distribute the Docker containers. The Docker client and Docker Daemon both communicate with each other using REST API, over UNIX sockets, or a network.
Docker architecture mainly constitutes three parts as Docker client, Docker Host, and Docker Registry. The Docker client is the primary interaction to users which enables them to use command-line utilities of DockerAPI to communicate with the Docker daemon. It allows communication with more than one Docker daemon. The Docker host constitutes Docker objects and Docker Daemon. The Docker object further contains a Docker image and Docker container. The Docker image is a template that creates the Docker container and this involves the steps to create the required software. The Docker container is a VM created from instructions found within a Docker image. The container will run the instance of a Docker image that consists of the entire package required to run an application. The Docker daemon makes the Docker API listen for the requests and also manages the Docker objects such as images, containers, volumes, etc. The image is built based on the user's input and is saved in the registry. The Docker images are stored in a repository known as Docker Registry. The Docker Registry is used to create Docker containers. You can use a local/private registry of a Docker repository.
5. Docker Installation
You can download Docker from https://docs.docker.com/toolbox/ which comprises Docker client, Docker Machine, Compose, Kitematic and VirtualBox. Let us now install Docker on Ubuntu OS, you have to update its packages to install Docker. The updates are installed by typing the following command on the terminal.
sudo apt-get update
Next, Type the following command in the terminal to install the recommended packages. You have to provide a password for using the “sudo” command.
sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual
Press ‘y’ to continue and then use the following command to install the Docker engine.
sudo apt-get install docker- engine
You can now verify if the Docker is successfully installed on your PC by using the following command.
sudo service docker start
Next, you have to run the container. If you don’t have a container to run, then Docker will download the image and build the container from the Docker hub and will build and run it. Run a simple container as ‘hello-world’ to verify it and to do that, run the following command.
docker run hello-world
Hello from Docker!
The above message confirms that the container was created successfully.
6. Docker Workflow
The users are able to create images, pull images, publish images, and run containers by implementing the workflow of Docker. The below picture illustrates the components in the Docker workflow.
The process begins by building an image from a Dockerfile which consists of instructions about container configuration. After the image is built in the Docker environment, you can run the image that creates a container. You can perform Stop, Start, and Restart operations in your container. Whatever manual configuration changes are made, those changes in a container can be committed to making a new image, which is further used to create a container from it later. You can easily push your images into a Docker registry while sharing the image with your team. You can also pull this image from the Docker registry by applying the pull command.
Pulling an image from Docker registry
You can get an already existing image from Docker’s website. Type the below pull command with the exact version to download an image for MySQL.
docker pull mysql:5.5.45
REPOSITORY TAG IMAGE VIRTUAL SIZE<none>
mysql 5.5.45 0da0b10c6fd8 213.5 MB
Add the identity of the repository by using the following command. In the command after -t, you can specify any name of your choice to identify your repository.
docker build –t test-hkrtrainings .
REPOSITORY TAG IMAGE ID VIRTUAL SIZEtest-hkrtrainings latest 4b9b8b27fb42 214.4 MB
mysql 5.5.45 0da0b10c6fd8 213.5 MB
Running an image
To start the 'test-hkrtrainings' image, you need to run the following command which configures MySQL root user's password in the Docker hub MySQL repository.
docker run -e MYSQL_ROOT_PASSWORD=root+1 –d test-hkrtrainings
Apply the following in order to check the container running.
To check all of your running processes, image, created name, listening ports, container name, use the following command.
CONTAINER ID IMAGE COMMAND30645F307114 test-hkrtrainings “/entrypoint.sh mysql”PORTS NAMES
The name of the container is ‘sanjay_raman’ and is generated automatically.
To name the container explicitly, the best method is to apply the –name option that inserts the name of our choice at container startup:
docker run –name hkrtrainings-sql -e MYSQL_ROOT_PASSWORD=root+1 -d est-mysql
Stopping and starting containers
You can stop the container by typing the docker stop command as follows.
docker stop hkrtrainings-sql
You can start the container by typing the docker start command as follows.
docker start hkrtrainings-sql
Pushing an image into the Repository
Push your image into the Docker hub to make it useful for anyone via a private repository. First, create a free account by visiting https://hub.docker.com/ and create a and then log in to the account by using the login command.
Provide your username, password, and email address that you have registered and push your image by applying the push command, with your username, image, and the version name. You will then receive a message stating that your repository has been successfully pushed. When we go back to your Docker hub account, you will see that there is a new repository as shown below in the below screen.
7. Commands used in Docker
To list all the containers, regardless of their state, add option -a as shown below:
docker ps -a
You can now easily discriminate between which container you would like to start with and which one to remove.
You can remove the container after using it for consuming the disk space. This is implemented by using the rm command as shown below.
docker rm hkrtrainings-sql
You can remove the unwanted images to free up space by applying the rmi command as shown below:
docker rmi hkrtrainings-sql
To remove unwanted and unnamed images that are generated during the debugging cycle, apply the below command to remove those images that are denoted by the name as
docker rmi $(docker images -q -f dangling=true)
The ports can be displayed by using the following command.
docker port hkrtrainings-sql
The top command is used in Docker for displaying the processes in a container.
docker top hkrtrainings-sql
The exec command is applied for executing the running commands in a container.
docker exec hkrtrainings-sql ls /
The run command performs various tasks like configuring security and managing network settings and system resources such as memory, filesystems, and CPU.
8. Docker File
A Dockerfile contains all the instructions that comprise the commands to install and configure an application. The Dockerfile creation is the primary approach that generates a Docker image. Creating an image by using a build command refers to Dockerfile available on your path. The instructions in a Dockerfile are executed in the same order as they are found in the Dockerfile.
FROM: This is the first command in the Dockerfile that identifies an image to inherit from.
MAINTAINER: This command provides visibility as well as credit to the author of the image.
RUN: This command executes a Linux command to install and configure.
ENTRYPOINT: The final script or application which is used to bootstrap the container and make it an executable application.
CMD: This command uses a JSON array to provide default arguments to the ENTRYPOINT.
LABEL: This command contains the name/value metadata about the image.
ENV: This command sets the environment variables.
COPY: This command copies files into the container.
ADD: This command is basically an alternative to the COPY instruction.
WORKDIR: This command sets a working directory for RUN, CMD, ENTRYPOINT, COPY, and/or ADD instructions.
EXPOSE: The ports on which the container listens.
VOLUME: This command is to create a mount point.
USER: An command to run RUN, CMD, and/or ENTRYPOINT instructions.
9. Docker Machine
The local or remote machines that are hosted on cloud providers are managed by a command-line utility known as Docker Machine.
Creating a Local machine
The following command is used to create a local machine by using a VirtualBox image named 'hktrainings'.
docker-machine create –d virtualbox hkrtrainings
The following command is used to list the configured machines.
Starting and stopping machines
The following command starts the Docker machine that is created.
docker-machine start hkrtrainings
The Docker machine starts after executing the above command. You are now supposed to configure the Docker command line with which the Docker daemon should interact. Apply the following command to do this:
docker –machine env hkrtrainings
eval “$(docker-machine env hkrtrainings)”
The following command is used to stop the machine.
docker-machine stop hkrtrainings
10. Docker Services
The Docker services will allow the user to scale containers across multiple Docker daemons, which all work together as a swarm with multiple managers and workers. Docker API enables the communication with each member of a swarm known as a Docker daemon, and all the daemons. Service enables the user to define the expected state like the number of service replicas that must be available at any given time. By default, the service is load-balanced across all worker nodes. To the consumer, the Docker service appears to be a single application. Docker Engine supports swarm mode in Docker 1.12 and higher.
The underlying technology
The technology has several features of the Linux kernel to deliver its functionality.
Docker namespaces provide the isolated workspace called the container. Running the container enables Docker to create a set of namespaces for that container. These namespaces provide a layer of isolation. Each aspect of a container runs in a separate namespace and its access is limited to that namespace.
Docker Engine uses the following namespaces on Linux.
pid: This namespace is used in process isolation(PID: Process ID).
net: This namespace is used in managing the network interfaces (NET: Networking).
ipc: This namespace is used in managing the access to IPC resources (IPC: InterProcess Communication).
mnt: This namespace is used in managing file system mount point(MNT: Mount).
uts: This namespace is used in isolating kernel and version identifiers.(UTS: Unix Timesharing System)>
Docker Engine on Linux also relies on control groups(cgroups). An application can be limited to a specific set of resources by using this cgroup. The control groups allow Docker Engine to share available hardware resources to containers and optionally enforce limits and constraints.
Union file systems
The Union file system is also known as UnionFS. These are the file systems that operate by creating layers and making them very lightweight and fast. UnionFS is used by the Docker Engine to provide the building blocks for containers. Docker Engine can use multiple UnionFS variants, including AUFS, btrfs, vfs, and DeviceMapper.
Docker Engine integrates the namespaces, control groups, and UnionFS into a wrapper called a container format. The default container format is libcontainer. Docker may support other container formats by integrating with technologies such as BSD Jails or Solaris Zones as a future scope.
Containers are the organizational units of Docker. When you create an image and start running it; we are running in a container. The container analogy is used because of the portability of the software we have running in our container. You can move it, in other words, “ship” the software, modify, manage, create or get rid of it, destroy it, similar to what cargo ships can do with real containers. In simple terms, an image is a template, and a container is a copy of that template. You can have multiple containers (copies) of the same image.
Thus we have now reached the end of this tutorial. We had learned all the topics which covered all the core concepts of Docker. We have explained all the concepts of Docker advantages, applications, architecture, installation, workflow, commands, file, machines and services in detail.