Docker

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

Virtualization vs Containers

Virtualization

Virtualization is the process of virtually running an OS on a host machine.

  • Been around since 1960 by IBM

  • VMs are easy to migrate from one machine to another

  • Very resource intensive

Containers

A container is a running instance of a container image. Multiple containers can be executed on a single machine. By default, the containers are isolated from both the host device and other containers. On host operating systems that are not Linux-based, Docker effectively runs Linux in a VM, then runs the containers within the VM. This is done transparently.

  • Containers use the host’s RAM directly, rather than how VMs block off resources like CPU, memory, swap.

  • When you build a container, only the bits of the application image that change need to be dealt with.

  • You will not likely install many components within a single container

    • LAMP application: one container with just MySQL, another with Apache and PHP

    • Application can be scaled by running additional Apache/PHP containers and more MySQL containers in a cluster configuration

  • From a container point of view, the container is a whole and dedicated computer; there is no direct communication with the host OS

  • Containers share the host’s Linux kernel

  • It can export ports; i.e. localhost:8080 the container will respond to the host

  • Containers have driven the concept of microservices

  • Scalable

    • Horizontal: multiple containers running the same app

      • e.g. create a container running a HTTP server and create a server farm by duplicating these containers
    • Vertical: dedicate more host resources to the container system

Microservices

The microservices architecture emphasizes building and runing containers that focus on a single aspect of the overall application. When all the containers are running, you have your complete application. The containers can be complex, such as a full-blown database server, or simple, such as a short shell script. Ideally, the containers you implement for your application will be simple, short, and focused. Each microservice you write should be simple to debug since you don’t need many lines of code.

Inter-container communication

  • Sockets

    • Direct messaging, however, creation of more sockets may be required
  • Filesystem

    • Containers read and writes to files to detect changes

    • Must constantly check (poll) for changes

    • Not recommended: slow performance

  • Database

    • Poll the DB records for changes

    • Also requires a lot of business logic and debugging

  • HTTP

    • Stateless; mesh of open sockets not required

    • Human readable

    • Can close / persist the connection as the protocol permits

  • MQTT

    • MQTT (Message Queue Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. The protocol usually runs over TCP/IP; however, any network protocol that provides ordered, lossless, bi-directional connections can support MQTT

Docker Hub

docker run hello-world

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.

To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/

For more examples and ideas, visit: https://docs.docker.com/get-started/


References

Virtual Machines Diagram - Google Drive

Docker Daemon Diagram - Google Drive

Docker for Developers | Packt

Docker Docs

What is Docker in 5 minutes - YouTube