Lecture 13
Lecture 13
AC295
AC295
Pavlos Protopapas
Institute for Applied Computational Science, Harvard
1
Outline
1. Recap
2. Motivation / Tutorial
3. What is a Container
4. Tutorial: Building & Running Containers using Docker
5. Why use Containers?
2
Virtual Environments
Pros Cons
• Reproducible research • Difficulty setting up your environment
• Explicit dependencies • Not isolation
• Improved engineering collaboration • Does not always work across different
OS
3
Virtual Machines
Pros Cons
• Full autonomy • Uses hardware in local machine
• Very secure • Not very portable since size of
• Lower costs VMs are large
4
Wish List
5
What is a CONTAINER
6
Environments vs Virtualization vs Containerization
7
Tutorial
8
What is a Container
Kernel
9
What Makes Containers so Small?
Container = User Space of OS
• User space refers to all of the code in an operating system that lives
outside of the kernel
10
How to run a docker container
11
What is the difference between an image and container
12
Inside the Dockerfile
FROM: This instruction in the Dockerfile tells the daemon, which
base image to use while creating our new Docker image. In the
example here, we are using a very minimal OS image called alpine
(just 5 MB of size). You can also replace it with Ubuntu, Fedora,
Debian or any other OS image.
RUN: This command instructs the Docker daemon to run the given
commands as it is while creating the image. A Dockerfile can have
multiple RUN commands, each of these RUN commands create a
new layer in the image.
How can you run multiple containers from the same image?
Yes, you could think of an image as instating a class.
14
Docker Image as Layers
When we execute the build command, the daemon reads the Dockerfile
and creates a layer for every command.
15
Image Layering
A application sandbox
- Each container is based on an image that holds necessary
config data
Container - When you launch a container, a writable layer is added on
(Writable, running application) top of the image
Layered Image 2
A static snapshot of the container configuration
- Layer images are read-only
- Each image depends on one or more parent images
Layered Image 1
16
Image Layering - Example
Docker layers for a container running debian and a python environment using
Pipenv
Pipenv Sync
Install Pipenv
Upgrade Pip
Debian Linux
Kernel
17
Some Docker Vocabulary
Docker Image
The basis of a Docker container. Represent a full application Images
How you store
your application
Docker Container
The standard unit in which the application service resides and executes Containers
How you run your
application
Docker Engine
Creates, ships and runs Docker containers deployable on a physical or
virtual, host locally, in a datacenter or cloud service provider
18
Tutorial
19
Tutorial
20
Tutorial: Docker commands
docker --version
21
Tutorial: Docker commands
docker container ls
22
Tutorial: Docker commands
docker image ls
23
Tutorial: Docker commands
24
Tutorial
25
Tutorial
docker container ls
docker image ls
26
Tutorial
27
Tutorial
docker container ls
docker image ls
28
Docker Image as Layers
>docker build -t hello_world_cmd -f Dockerfile_cmd .
….
Step 3/4 : ENTRYPOINT ["/bin/echo", "Hello"] Step3: Instruction 3
---> Running in 52c7a98397ad
Removing intermediate container 52c7a98397ad
---> 7e4f8b0774de
Step 4/4 : CMD ["world"] Step4: Instruction 4
---> Running in 353adb968c2b
Removing intermediate container 353adb968c2b
---> a89172ee2876
Successfully built a89172ee2876
Successfully tagged hello_world_cmd:latest
30
Docker Image as Layers
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello_world_cmd latest a89172ee2876 7 minutes ago 96.7MB
ubuntu latest 4e2eef94cd6b 3 weeks ago 73.9MB
31
Why Layers
Why build an image with multiple layers when we can just build it in a single layer?
Let’s take an example to explain this concept better, let us try to change the Dockerfile_cmd we
created and rebuild a new Docker image.
> docker build -t hello_world_cmd -f Dockerfile_cmd .
Sending build context to Docker daemon 34.3kB
Step 1/4 : FROM ubuntu:latest
---> 4e2eef94cd6b
Step 2/4 : RUN apt-get update
---> Using cache Have seen this before. Use
---> cfc0c414a914 cache
Step 3/4 : ENTRYPOINT ["/bin/echo", "Hello"]
---> Using cache
---> 7e4f8b0774de
Step 4/4 : CMD ["world"]
---> Using cache
---> a89172ee2876
Successfully built a89172ee2876
Successfully tagged hello_world_cmd:latest
As you can see that the image was built using the existing layers from our previous docker image
builds. If some of these layers are being used in other containers, they can just use the existing layer
instead of recreating it from scratch.
32
Why use Containers?
33
Monolithic Architecture
Server
HTML / REST / JSON
Storefront UI Module
Browser Apps
Catalog Module
Reviews Module
Database
Orders Module
REST / JSON
Mobile Apps
34
Monolithic Architecture
Server
HTML / REST / JSON
Storefront UI Module
Browser Apps
Catalog Module
Reviews Module
Database
Orders Module
REST / JSON Oracle
Mobile Apps
Java
35
Monolithic Architecture - Advantages
1. Simple to develop because all the tools and IDEs support the
applications by default
2. Easy to deploy because all components are packed into one
bundle
3. Easy to scale the whole application
36
Monolithic Architecture - Disadvantages
37
Applications have changed dramatically
Today
A decade ago
Apps are constantly being developed
Apps were monolithic
Built on a single stack (e.e. .NET or Java) Build from loosely coupled components
Long lived Newer version are deployed often
Deployed to a single server Deployed to a multitude of servers
38
Applications have changed dramatically
Today
A decade ago
Apps are constantly being developed
Apps were monolithic
Built on a single stack (e.e. .NET or Java) Build from loosely coupled components
Long lived Newer version are deployed often
Deployed to a single server Deployed to a multitude of servers
Data Science
Apps are being integrated with various
data types/sources and models
39
Today: Microservice Architecture
REST / JSON
Database
REST / JSON
API Service Reviews Module
Cloud Store
Mobile Apps
Orders Module
Database
REST / JSON
Recommendation
Edge Device Apps Module
Models
40
Software Development Workflow (no Docker)
Windows
Node.js
Python
Linux
Node.js
Python
Mac
Node.js
Python
OS Specific installation in
every developer machine
41
Software Development Workflow (no Docker)
Windows
Node.js
Python
Node.js GitHub
Python
Mac
Every team member moves
code to source control
Node.js
Python
OS Specific installation in
every developer machine
42
Software Development Workflow (no Docker)
Windows
Node.js
Python
Build Server
Mac
Every team member moves
code to source control Build server needs to be
Node.js
Python installed with all required
softwares/frameworks
43
Software Development Workflow (no Docker)
Windows
Node.js
Python
Build Server
Production / Test Servers
Linux Source Control
Linux
Linux Linux
Node.js GitHub
Python
Mac
Every team member moves
code to source control Build server needs to be
Node.js
Python installed with all required Production server needs to
softwares/frameworks be installed with all required
softwares/frameworks
Production build is performed
by pulling code from source Production server will be
control different OS version than
OS Specific installation in
development machines
every developer machine
44
Software Development Workflow (with Docker)
Windows
Linux
Mac
GitHub
Mac
Every team member moves
code to source control
Build Server
GitHub
Mac
Every team member moves
code to source control Build server only needs
Docker installed
Build Server
Production/ Test Servers
Linux Source Control
Linux
Linux Linux
GitHub
Mac
Every team member moves
code to source control Build server only needs
Docker installed Production server only needs
Docker installed
Docker images are built for a
release and pushed to Production server pulls
Development machines only container registry Docker images from
needs Docker installed container registry and runs
them
Containers need to be setup
only once
48
Comparison
49
THANK YOU
50