Docker

Docker is an operating system level virtualization platform for developers, operators, enterprises to develop, deploy, and run applications with containers. The use of containers for application deployment is called containerization.

Main features of containers are:

Flexible: Most of the complex applications can be containerized.
Lightweight: Containers are like normal applications that leverage and share the host kernel.
Interchangeable: Updates and upgrades can be deployed on-the-fly.
Portable: Images can be build locally and deploy to the cloud, and run anywhere.
Scalable: Containers can be replicated and distributed
Stackable: Containers can be stacked vertically and on-the-fly.

Images and containers

A container is is a runtime instance of an image,what the image becomes in memory when executed. A container is similar to object in object oriented programming and an image is similar to a class. A container is launched by running an image. An image is an executable package that includes everything needed to run an application – the code, a runtime, libraries, environment variables, and configuration files. Containers can communicate each other using TCP/IP. Data processing in a container is not persistent,volumes are used with containers as underlying data layers for data persistence.

Containers and virtual machines

Containers are lightweight as it run natively on linux as a discrete process similar to other executables but a virtual machine runs as a guest operating system with virtual access to host resources through a hypervisor. VMs occupy an environment with more resources than most applications need.

Docker installation using Yum in Centos

Step1 : Install yum-utils, device-mapper-persistent-data and lvm2. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data and lvm2 are required by the devicemapper storage driver.
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Step2 : Add docker repositary
sudo yum-config-manager –add-repo https://download.docker.com/linux/centos/docker-ce.repo

The edge and test repositories are added by default by disabled. You can enable them with the following comamnds if you like to use those.
sudo yum-config-manager –enable docker-ce-edge
sudo yum-config-manager –enable docker-ce-test

Step3 : Install the latest version of Docker CE
sudo yum install docker-ce

Check the installed version

[root@myhost ~]# docker --version
Docker version 18.03.1-ce, build 9ee9f40

Step4: Start Docker.
sudo systemctl start docker

Verify that docker is installed correctly by running the hello-world image.

[root@myhost ~]#sudo docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

Useful docker commands

Build an image from the Dockerfile in the current directory and tag the image
docker build -t myapp:1.0 .

List all images that are locally stored with the Docker engine
docker images

Delete an image from the local image store
docker rmi myapp:3.4

Pull an image from a registry
docker pull <imagename>:<version>

Retag a local image with a new image name and tag
docker tag localimagename:3.4 myrepo/newimagename:3.4

Running a docker container
docker run
Options:
–rm remove container automatically after it exits
-it connect the container to terminal
–name <name> name the container
-p <portnumber>:<portnumber> expose port 5000 externally and map to port 80
-v <path>:<path> create a host mapped volume inside the container
imagename:<version> the image from which the container is instantiated
command: the command to run inside the container

Example

docker run –rm -it –name myappname -p 5000:80 -v /dev:/code myimage:3.4 /bin/sh

Stop a running container through SIGTERM
docker stop myappname

Stop a running container through SIGKILL
docker kill myappname

List the networks
docker network ls

List the running containers
docker ps

Delete all running and stopped containers
docker rm -f $(docker ps -aq)

Print the last 100 lines of a container’s logs
docker logs –tail 100 myappname