Commercial in Confidence
Dec 2018Page 1 http://thinkingcraftsman.in
Object Oriented Containers:
Applying SOLID Principles to
Docker/Container design
Nitin BhideDec 2018
Commercial in Confidence
Dec 2018Page 2 http://thinkingcraftsman.in
Shipping Container Changed Transportation
Malcom McLean after spending hours in late 1937 queuing at
a Jersey City pier to unload his truck, he realized that it would
be quicker simply to hoist the entire truck body on board.
From this incident, came his decision eighteen years later to
buy a war-surplus tanker and equip it to carry 33-foot-long
containers.
This insight started the silent resolution in transportation called
‘Shipping Container’. Shipping Containers changed ‘scale’ of
transportation operations.
By 2014, 20+ million containers in service.
Commercial in Confidence
Dec 2018Page 3 http://thinkingcraftsman.in
Now Docker Containers
are changing
“Scale” of Software Operations
Commercial in Confidence
Dec 2018Page 4 http://thinkingcraftsman.in
What is ‘Docker/Container’ ?
Docker performs operating-system-level
virtualization, also known as "containerization". It
was first released in 2013
Docker is used to run software packages called
"containers".
Containers are isolated from each other and
bundle their own application, tools, libraries and
configuration files;
Commercial in Confidence
Dec 2018Page 5 http://thinkingcraftsman.in
What Problems Does the
Docker/Container Solve ?
Commercial in Confidence
Dec 2018Page 6 http://thinkingcraftsman.in
Lets look at typical cloud deployment scenario
Code checkout
the automated tests run in
the Dev environment,
creating a QA environment
that matches Dev,
deploying code into it,
running all the tests,
deploying and migrating into
a fresh staging environment
that matches QA, load
testing,
the compiling packaging of
the code for deployment;
preparing new server
instances;
loading and configuring the
operating system, databases,
and applications; making all
the changes to the networks,
firewalls, and load balancers;
testing to make sure the
deployment completed
successfully.
Commercial in Confidence
Dec 2018Page 7 http://thinkingcraftsman.in
Now Imagine that
you have to support
500+ developers and QA Enginers
to deploy their changes on 100+ servers and
you are expecting at least 10 such deploys every day
Commercial in Confidence
Dec 2018Page 8 http://thinkingcraftsman.in
Docker helps you deploy at SCALE
Docker allows you to ‘bundle’ specific version of OS, third party libraries, your
own application, tools, and configuration files, network and storage in a
‘isolated container’
You have to just copy the container image on a computer and instantiate the
container instance from the image
Now the deploying your application on 100 servers is
just copy the container on each server.
(Of course, this is a really really simplified version)
Commercial in Confidence
Dec 2018Page 9 http://thinkingcraftsman.in
Docker Architecture
Refer - https://docs.docker.com/engine/docker-overview/#docker-architecture
Commercial in Confidence
Dec 2018Page 10 http://thinkingcraftsman.in
What is Docker (Container) ?
Containers can communicate with each other through
well-defined channels.
All containers are run by a single operating system
kernel and are thus more lightweight than virtual
machines.
Containers are created from "images" that specify their
precise contents.
Images are often created by combining and modifying
standard images downloaded from public repositories
Commercial in Confidence
Dec 2018Page 11 http://thinkingcraftsman.in
Problems that Containers are Solving
Managing Complexity
Managing Scale
Containers NOT really
needed for 3-4 people
development team deploying
on 1 or 2 servers
Commercial in Confidence
Dec 2018Page 12 http://thinkingcraftsman.in
Problems that OO Programming Solves
Managing Complexity
Managing Scale
We don’t use ‘classes’ for
small simple shell scripts
OO Design help in
developing large
scale/complex software
Commercial in Confidence
Dec 2018Page 13 http://thinkingcraftsman.in
Containers and OO Programming is about managing
‘Systems of made up of Objects’
In case Docker,
these are ‘individual containers’
In case of OO Programming,
these are ‘classes and modules’
Commercial in Confidence
Dec 2018Page 14 http://thinkingcraftsman.in
OO Design Vs Container Design
OO Principles Solve
‘Scale/Complexity’ in Software
Development
OO Concepts are Developed in 1960s
and 1970s
SOLID Design Principles are
articulated by Robert Martin in 2000
OO Best Practices are well
established and with lot of theoretical
and experimental background
Docker/Containers try to Solve
‘Scale/Complexity’ in Software
Deployment
Containers are new. Docker is
released in 2013
Docker Best Practices are still being
established
Must less theoretical and
experimental background
Commercial in Confidence
Dec 2018Page 15 http://thinkingcraftsman.in
Can We apply OO Design Principles to Docker Design ??
Will it result in ‘better’ Container Design ?
Commercial in Confidence
Dec 2018Page 16 http://thinkingcraftsman.in
For example
Docker
Google Recommends Following Best Practice
as ‘Highly Important’
Package a single app per container
• Because a container is designed to have
the same lifecycle as the app it hosts, each
of your containers should contain only one
app.
OO Programming
First principle in SOLID design principles
Single Responsibility Principle
• A class should have one, and only one,
reason to change.
I think the reason “containers are
designed to have same lifecycle as
the app” is to ‘enforce’ the SRP
Commercial in Confidence
Dec 2018Page 17 http://thinkingcraftsman.in
Lets See what we can learn
with other OO Principles and
apply it Docker
Commercial in Confidence
Dec 2018Page 18 http://thinkingcraftsman.in
OO Principle - Encapsulation
Parnas in his Seminal Paper “On the criteria to be used in decomposing systems into modules”
(published 1972) says
We have tried to demonstrate by these examples that it is almost always incorrect to
begin the decomposition of a system into modules on the basis of a flowchart. We
propose instead that one begins with a list of difficult design decisions or design
decisions which are likely to change. Each module is then designed to hide such a
decision from the others.
Encapsulation is about Hiding Potential Change
and NOT about Hiding Data
Commercial in Confidence
Dec 2018Page 19 http://thinkingcraftsman.in
Encapsulation – with Containers
Docker Container is about “Encapsulating” the entire packaging and
dependencies required for running a specific application
For example,
Your application requires Python 3.4 and your database server also uses
python but requires Python 3.3.
Host
Docker Engine
Container 2
Python 3.2
Postgres Database
Container 1
Python 3.3
Your Application
Commercial in Confidence
Dec 2018Page 20 http://thinkingcraftsman.in
OO Principle – Open/Closed Principle
“Software entities (classes, modules, functions, etc.)
should be
open for extension, but closed for modification”
Adding new functionality will involve no change in the existing code.
The new functionality will be implemented as new class or new
method
Commercial in Confidence
Dec 2018Page 21 http://thinkingcraftsman.in
Open/Closed Principle – with Docker
Docker image is ‘immutable’ (closed for modification)
• once built, it’s unchangeable, and if you want to make changes,
you’ll get a new image as a result.
• Basic design decision of Docker
• Benefits
• Immutability lowers rollback times because you can probably
keep the old version running for a little longer and switch traffic in
case of problems.
• Brings in Predictability and Portability
Commercial in Confidence
Dec 2018Page 22 http://thinkingcraftsman.in
Open/Closed Principle – with Docker
Docker image is Multi Layered i.e. inherited
(Open for extension)
• You can create new docker images from a
parent image
• It allows you create ‘predictable’ deployment
as the ‘base image’ can be common across
various pieces of deployment
• Developer, QA Engineer and Production
images can be ‘derived’ from common base
image with just different settings/environment
variables
Commercial in Confidence
Dec 2018Page 23 http://thinkingcraftsman.in
OO Principle – Liskov Substitution & Inheritance
In class hierarchies,
subclasses should be substitutable for their base classes
• All code operating with a pointer or reference to the base class should
behave the same when made to work with an object of derived class
• Inheriting classes should not perform any actions that will invalidate the
assumptions made by the base class
• The derived class should not expect anything more & must not guarantee
anything less than that of the base class.
Commercial in Confidence
Dec 2018Page 24 http://thinkingcraftsman.in
LSP & Inheritance – with Docker
Common Docker Use cases are possible
ONLY if you are following LSP
• Deployment and Rollback
• Fast Deployment by Swapping
Containers
• A/B Testing
Host
App
Ver 1
App
Ver 2
Port 80
Port 8080` Port 8080`
Commercial in Confidence
Dec 2018Page 25 http://thinkingcraftsman.in
OO Principle – Dependency Inversion
• High-level modules should not depend
on low-level modules. Both should
depend on abstractions.
• Abstractions should not depend on
details. Details should depend on
abstractions.
Print
FilePrinter
PDFFileWriterTextFileWriter
Commercial in Confidence
Dec 2018Page 26 http://thinkingcraftsman.in
Dependency Inversion : With Docker
• Docker Clients CANNOT directly communicate with
Containers
• Docker clients will communicate to Docker Daemon
and Daemon will communicate with Containers
• This way both ‘Containers’ and ‘Clients’ depend on
‘Docker Daemon’ and NOT directly on each other
Docker Design Decision :
• Containers should NOT directly depend on each
other.
• Containers should communicate using service
registry
Best Practices
Docker Client
Docker
Daemon
Container 2Container 1
Commercial in Confidence
Dec 2018Page 27 http://thinkingcraftsman.in
Dependency Inversion : with Docker
From OpenShift Docker Guideline
Use Services for Inter-image Communication
For cases where your image needs to communicate with a service provided by
another image, such as a web front end image that needs to access a database
image to store and retrieve data, your image should consume an OpenShift service.
Services provide a static endpoint for access which does not change as containers
are stopped, started, or moved.
Docker instance themselves do not provide this capability, OpenShift is providing it.
You may have to use something ‘ZooKeeper’ or other Service Registration and
Discovery layer
Commercial in Confidence
Dec 2018Page 28 http://thinkingcraftsman.in
Food for Thought
Commercial in Confidence
Dec 2018Page 29 http://thinkingcraftsman.in
Can we map to Docker/containers to Other OO Ideas ?
• Other OO Principles
• Acyclic Dependency Principle
• The Stable Dependencies Principle
• The Stable Abstractions Principle
• The Release Reuse Equivalency Principle
• Guideline like “Prefer composition over inheritance”
• Design Patterns e.g. Proxy and Mediator
• Refactoring monolithic deployments to modular deployments
Commercial in Confidence
Dec 2018Page 30 http://thinkingcraftsman.in
Object Oriented Docker/Containers
Nitin Bhide
Thinking Craftsman/Passionate Programmer
Website : http://thinkingcraftsman.in
E-mail: nitinbhide@thinkingcraftsman.in

Object Oriented Containers - Applying SOLID Principles to Docker/Container Design

  • 1.
    Commercial in Confidence Dec2018Page 1 http://thinkingcraftsman.in Object Oriented Containers: Applying SOLID Principles to Docker/Container design Nitin BhideDec 2018
  • 2.
    Commercial in Confidence Dec2018Page 2 http://thinkingcraftsman.in Shipping Container Changed Transportation Malcom McLean after spending hours in late 1937 queuing at a Jersey City pier to unload his truck, he realized that it would be quicker simply to hoist the entire truck body on board. From this incident, came his decision eighteen years later to buy a war-surplus tanker and equip it to carry 33-foot-long containers. This insight started the silent resolution in transportation called ‘Shipping Container’. Shipping Containers changed ‘scale’ of transportation operations. By 2014, 20+ million containers in service.
  • 3.
    Commercial in Confidence Dec2018Page 3 http://thinkingcraftsman.in Now Docker Containers are changing “Scale” of Software Operations
  • 4.
    Commercial in Confidence Dec2018Page 4 http://thinkingcraftsman.in What is ‘Docker/Container’ ? Docker performs operating-system-level virtualization, also known as "containerization". It was first released in 2013 Docker is used to run software packages called "containers". Containers are isolated from each other and bundle their own application, tools, libraries and configuration files;
  • 5.
    Commercial in Confidence Dec2018Page 5 http://thinkingcraftsman.in What Problems Does the Docker/Container Solve ?
  • 6.
    Commercial in Confidence Dec2018Page 6 http://thinkingcraftsman.in Lets look at typical cloud deployment scenario Code checkout the automated tests run in the Dev environment, creating a QA environment that matches Dev, deploying code into it, running all the tests, deploying and migrating into a fresh staging environment that matches QA, load testing, the compiling packaging of the code for deployment; preparing new server instances; loading and configuring the operating system, databases, and applications; making all the changes to the networks, firewalls, and load balancers; testing to make sure the deployment completed successfully.
  • 7.
    Commercial in Confidence Dec2018Page 7 http://thinkingcraftsman.in Now Imagine that you have to support 500+ developers and QA Enginers to deploy their changes on 100+ servers and you are expecting at least 10 such deploys every day
  • 8.
    Commercial in Confidence Dec2018Page 8 http://thinkingcraftsman.in Docker helps you deploy at SCALE Docker allows you to ‘bundle’ specific version of OS, third party libraries, your own application, tools, and configuration files, network and storage in a ‘isolated container’ You have to just copy the container image on a computer and instantiate the container instance from the image Now the deploying your application on 100 servers is just copy the container on each server. (Of course, this is a really really simplified version)
  • 9.
    Commercial in Confidence Dec2018Page 9 http://thinkingcraftsman.in Docker Architecture Refer - https://docs.docker.com/engine/docker-overview/#docker-architecture
  • 10.
    Commercial in Confidence Dec2018Page 10 http://thinkingcraftsman.in What is Docker (Container) ? Containers can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and are thus more lightweight than virtual machines. Containers are created from "images" that specify their precise contents. Images are often created by combining and modifying standard images downloaded from public repositories
  • 11.
    Commercial in Confidence Dec2018Page 11 http://thinkingcraftsman.in Problems that Containers are Solving Managing Complexity Managing Scale Containers NOT really needed for 3-4 people development team deploying on 1 or 2 servers
  • 12.
    Commercial in Confidence Dec2018Page 12 http://thinkingcraftsman.in Problems that OO Programming Solves Managing Complexity Managing Scale We don’t use ‘classes’ for small simple shell scripts OO Design help in developing large scale/complex software
  • 13.
    Commercial in Confidence Dec2018Page 13 http://thinkingcraftsman.in Containers and OO Programming is about managing ‘Systems of made up of Objects’ In case Docker, these are ‘individual containers’ In case of OO Programming, these are ‘classes and modules’
  • 14.
    Commercial in Confidence Dec2018Page 14 http://thinkingcraftsman.in OO Design Vs Container Design OO Principles Solve ‘Scale/Complexity’ in Software Development OO Concepts are Developed in 1960s and 1970s SOLID Design Principles are articulated by Robert Martin in 2000 OO Best Practices are well established and with lot of theoretical and experimental background Docker/Containers try to Solve ‘Scale/Complexity’ in Software Deployment Containers are new. Docker is released in 2013 Docker Best Practices are still being established Must less theoretical and experimental background
  • 15.
    Commercial in Confidence Dec2018Page 15 http://thinkingcraftsman.in Can We apply OO Design Principles to Docker Design ?? Will it result in ‘better’ Container Design ?
  • 16.
    Commercial in Confidence Dec2018Page 16 http://thinkingcraftsman.in For example Docker Google Recommends Following Best Practice as ‘Highly Important’ Package a single app per container • Because a container is designed to have the same lifecycle as the app it hosts, each of your containers should contain only one app. OO Programming First principle in SOLID design principles Single Responsibility Principle • A class should have one, and only one, reason to change. I think the reason “containers are designed to have same lifecycle as the app” is to ‘enforce’ the SRP
  • 17.
    Commercial in Confidence Dec2018Page 17 http://thinkingcraftsman.in Lets See what we can learn with other OO Principles and apply it Docker
  • 18.
    Commercial in Confidence Dec2018Page 18 http://thinkingcraftsman.in OO Principle - Encapsulation Parnas in his Seminal Paper “On the criteria to be used in decomposing systems into modules” (published 1972) says We have tried to demonstrate by these examples that it is almost always incorrect to begin the decomposition of a system into modules on the basis of a flowchart. We propose instead that one begins with a list of difficult design decisions or design decisions which are likely to change. Each module is then designed to hide such a decision from the others. Encapsulation is about Hiding Potential Change and NOT about Hiding Data
  • 19.
    Commercial in Confidence Dec2018Page 19 http://thinkingcraftsman.in Encapsulation – with Containers Docker Container is about “Encapsulating” the entire packaging and dependencies required for running a specific application For example, Your application requires Python 3.4 and your database server also uses python but requires Python 3.3. Host Docker Engine Container 2 Python 3.2 Postgres Database Container 1 Python 3.3 Your Application
  • 20.
    Commercial in Confidence Dec2018Page 20 http://thinkingcraftsman.in OO Principle – Open/Closed Principle “Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” Adding new functionality will involve no change in the existing code. The new functionality will be implemented as new class or new method
  • 21.
    Commercial in Confidence Dec2018Page 21 http://thinkingcraftsman.in Open/Closed Principle – with Docker Docker image is ‘immutable’ (closed for modification) • once built, it’s unchangeable, and if you want to make changes, you’ll get a new image as a result. • Basic design decision of Docker • Benefits • Immutability lowers rollback times because you can probably keep the old version running for a little longer and switch traffic in case of problems. • Brings in Predictability and Portability
  • 22.
    Commercial in Confidence Dec2018Page 22 http://thinkingcraftsman.in Open/Closed Principle – with Docker Docker image is Multi Layered i.e. inherited (Open for extension) • You can create new docker images from a parent image • It allows you create ‘predictable’ deployment as the ‘base image’ can be common across various pieces of deployment • Developer, QA Engineer and Production images can be ‘derived’ from common base image with just different settings/environment variables
  • 23.
    Commercial in Confidence Dec2018Page 23 http://thinkingcraftsman.in OO Principle – Liskov Substitution & Inheritance In class hierarchies, subclasses should be substitutable for their base classes • All code operating with a pointer or reference to the base class should behave the same when made to work with an object of derived class • Inheriting classes should not perform any actions that will invalidate the assumptions made by the base class • The derived class should not expect anything more & must not guarantee anything less than that of the base class.
  • 24.
    Commercial in Confidence Dec2018Page 24 http://thinkingcraftsman.in LSP & Inheritance – with Docker Common Docker Use cases are possible ONLY if you are following LSP • Deployment and Rollback • Fast Deployment by Swapping Containers • A/B Testing Host App Ver 1 App Ver 2 Port 80 Port 8080` Port 8080`
  • 25.
    Commercial in Confidence Dec2018Page 25 http://thinkingcraftsman.in OO Principle – Dependency Inversion • High-level modules should not depend on low-level modules. Both should depend on abstractions. • Abstractions should not depend on details. Details should depend on abstractions. Print FilePrinter PDFFileWriterTextFileWriter
  • 26.
    Commercial in Confidence Dec2018Page 26 http://thinkingcraftsman.in Dependency Inversion : With Docker • Docker Clients CANNOT directly communicate with Containers • Docker clients will communicate to Docker Daemon and Daemon will communicate with Containers • This way both ‘Containers’ and ‘Clients’ depend on ‘Docker Daemon’ and NOT directly on each other Docker Design Decision : • Containers should NOT directly depend on each other. • Containers should communicate using service registry Best Practices Docker Client Docker Daemon Container 2Container 1
  • 27.
    Commercial in Confidence Dec2018Page 27 http://thinkingcraftsman.in Dependency Inversion : with Docker From OpenShift Docker Guideline Use Services for Inter-image Communication For cases where your image needs to communicate with a service provided by another image, such as a web front end image that needs to access a database image to store and retrieve data, your image should consume an OpenShift service. Services provide a static endpoint for access which does not change as containers are stopped, started, or moved. Docker instance themselves do not provide this capability, OpenShift is providing it. You may have to use something ‘ZooKeeper’ or other Service Registration and Discovery layer
  • 28.
    Commercial in Confidence Dec2018Page 28 http://thinkingcraftsman.in Food for Thought
  • 29.
    Commercial in Confidence Dec2018Page 29 http://thinkingcraftsman.in Can we map to Docker/containers to Other OO Ideas ? • Other OO Principles • Acyclic Dependency Principle • The Stable Dependencies Principle • The Stable Abstractions Principle • The Release Reuse Equivalency Principle • Guideline like “Prefer composition over inheritance” • Design Patterns e.g. Proxy and Mediator • Refactoring monolithic deployments to modular deployments
  • 30.
    Commercial in Confidence Dec2018Page 30 http://thinkingcraftsman.in Object Oriented Docker/Containers Nitin Bhide Thinking Craftsman/Passionate Programmer Website : http://thinkingcraftsman.in E-mail: nitinbhide@thinkingcraftsman.in