What is Docker?

Docker is a container platform that allows developers to build, test and deploy applications easily and quickly. A developer defines the application and everything that is required to run that application like it's dependencies and stuff inside a Dockerfile that is used then to build a Docker Image that defines a Docker container and inside that container the application will be running. Basically Docker is a shipping container system for code.

Why use Docker?

Docker can help you ship your code faster and gives you more control over your applications. You can deploy applications on containers which make it easier for them to be deployed, easily scaled and perform rollbacks (rolling updates and stuff) and find issues. Using Docker helps in saving money by utilizing resources. Docker based applications can be easily moved from local development machines to production deployments. You can use Docker for Microservices that way you can scale and maintain and update applications with almost zero downtime which was the problem with monolithic applications.

Docker+is+a+shipping+container+system+for+code.jpg

How Does it work?

Containerization vs Virtualization

container-vms.jpg
When you run an application on a virtual machine you can't do it without installing an Operating System because virtual machines require it's own OS thus a hypervisor to run an app. A Hypervisor is basically used to create multiple machines on a host operating system and it manages VMs. These virtual machines have their own operating system and they don't use the host operating system. They also have some space and memory allocated to them. In the software world, containerization is one of the most efficient ways to deploy application. A container contains application with its own operating environment. It can be placed on any host machine without worrying about it's dependencies or some special configuration to run the application. Virtual machine is hardware virtualization whereas containerization is OS virtualization. Essentially containerization is a lighter approach to virtualization.

Architecture of Docker

docker-image.png
Docker uses the client-server architecture. The Docker client communicates with the Docker daemon via the Docker CLI. Docker daemon is the component which does the heavy lifting of building, running and distributing your Docker containers.

To download and install Docker on your local machine, refer to: docs.docker.com

Docker client

The Docker client is the primary way to communicate with the Docker daemon. Basically this is what you use to interact with the server (Docker daemon). When you run a command using docker the client sends the command to the daemon and it carries that. Docker client can interact with more than one daemon.

Docker daemon

Docker daemon is basically a server that listens to the API requests made by the client and manages Docker objects such as images, containers, networks and volumes.

Docker registries

These are just online registries where Docker images are stored. Folks create images and push them to public registry for other folks to use. Docker Hub is a public registry that anyone can use. When you pull an image, Docker by default looks for it in the public registry and saves the image on your local system on DOCKER_HOST. You can also store images on your local machine or push them to the public registry.

Dockerfile

Dockerfile is a file that contains steps to create Docker images. It's like a recipe to with all the necessary ingredients and steps to make your dish. This file cna be used to create Docker images. Images can be pulled to create containers in any environment. These images can also be stored on online registries like Docker hub. When you run Docker image you get docker containers and inside these containers your applications run cause these containers will have the applications with all their dependencies.

  • Create a file named 'Dockerfile'
  • By default on building, docker searches for 'Dockerfile' $docker built -t myfirstimg:1.0 .
  • During building the image, the commands that are in RUN section of the 'Dockerfile' will get executed. $ docker run ImageID
  • The commands that are in CMD section will get executed when you create a container out of that image.

    Dockerfile Example

    FROM ubuntu 
    MAINTAINER sourav kumar <sourav@gmail.com> 
    RUN apt-get update 
    CMD [“echo”, “Hey there, I am Sourav”]
    

    Docker Image

    Docker Image is a file that defines a Docker container. Is is similar to a snapshot of a virtual machine. A container that moves from one Docker environment to other Docker environments with the same OS will work without any changes because the image contains all the files and dependencies needed to execute the code. Docker image is run to create Docker container. Docker images are immutable. The files making up the image can not be changed. Images are stored locally or in remote locations like docker registry.

    Basic commands

    $ docker pull ubuntu:18.04 (18.04 is tagged version (explained below))
    $ docker images (Lists Docker Images)
    $ docker run image (creates a container out of an image)
    $ docker rmi image (deletes a Docker Image if no container is using it)
    $ docker rmi $(docker images -q) (deletes all Docker images)
    
    Docker can build images automatically by following the instructions given in the Dockerfile. Also a single image can be used to create multiple containers.

Docker-Architecture-1024x406.png
Images are built in layers. Each layer is an immutable file, but is a collection of files and directories. The last layer can be used to write out data to. Layers receive an ID, calculated via a SHA 256 hash of the layer contents. Thus, if the layer contents change- Notice the IMAGE ID below and the Hash Values given above, the first 12 characters of the hash are equal to the IMAGE ID, the SHA 256 hash changes as well. Note: The Image ID listed by docker commands (ie ‘docker images’) is the first 12 characters of the hash value. These hash values are referred to by ‘tag’ names.

Listing some hash values of Docker images

$ docker images -q --no-trunc
sha256:3556258649b2ef23a41812be17377d32f568ed9f45150a26466d2ea26d926c32
sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1
sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e

Notice something that the IMAGE ID below and the Hash Values given above, the first 12 characters of the hash values are equal to the IMAGE ID

$ docker images
REPOSITORY   TAG     IMAGE ID        
ubuntu       18.04   3556258649b2   
centos       latest  9f38484d220f    
hello-world  latest  fce289e99eb9

The format of a full tag name is: [REGISTRYHOST/][USERNAME/]NAME[:TAG] For Registry Host ‘registry.hub.docker.com’ is inferred. For ‘:TAG’ — ‘latest’ is default, and inferred Example: registry.hub.docker.com/mongo:latest

Containers

containers-rsa.jpg
A container is nothing but a running instance of an Image. This is the thing inside which your applications are running. You can manage containers using Docker API or CLI. You can connect more than one network to a certain container, attach storage to it for example RAM and memory or whatever and even create a new Image out of it based on its current state. If we delete a container the data will be lost!

Basic commands

$ docker run ImageName/ID (checks locally for image, if not available it will go to registry and then go to DOCKER_HOST)
$ docker start ContainerName/ID
$ docker kill ContainerName/ID (Stops a running container) 
$ docker ps (list all containers)
$ docker rm ContainerName/ID (Deletes a stopped container)
$ docker rm $(docker ps -a -q) (Delete all stopped containers)

That's it. That's more than enough to get you up and running with Docker and containers and stuff. You can also check out other resources if you get stuck somewhere. I think the best way to learn is by doing so get some hands on experience first then learn the theories on the go.

What's Next?

Let's say you are running a large number of containers and it's getting difficult to manage all of then. Solution? The Orchestration System. It helps in automating application scaling, management, deployment and stuff. One of the very popular and widely used Container Orchestration Engines is Kubernetes (K8s).

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐