You are on page 1of 87

Docker Mastery

Muhammad Rizwan – Corvit Systems


Stack Overflow Survey Reconfirms Developers Love Docker

Docker is the #1 most wanted and #2 most loved developer


tool, and helps millions of developers
build, share and run any app, anywhere –
on-prem or in the cloud.
What is Docker?
Docker is a platform or ecosystem
around creating and running containers.

Docker Docker Docker Docker Docker Docker


Client Server Machine Images Hub Compose
What is Docker?

A platform for building, running


and shipping applications in a consistent manner
Why use Docker?
Docker makes it really easy to install and run software
without worrying about setup or dependencies.

Docker is here and there’s no point hiding.


In fact, if you want the best jobs working on the best technologies,
you need to know Docker and containers.
Why use Docker?
Installing Software
Download installer This is what
Docker is trying to fix
Run installer
Get an error message
Flow during installation
Troubleshoot issue
Rerun installer
Get another error!
Why use Docker?
Why use Docker?
- One or more files are not included as part of your deployment
so your application is not completely deployed
- Software version mismatch
- Different configuration settings
Docker is just for Developers?
If you think Docker is just for developers, prepare to have your world
turned upside-down.
If you think developers are going to do that, you’re dreaming.
Ops will need to build and run high-performance production-grade
Docker infrastructures. If you’ve got an Ops focus and you’re not
skilled-up on Docker, you’re in for a world of pain.
Docker

Docker allows teams to securely build, share


and run applications across the
entire software supply chain.

Build and Ship any Application Anywhere


Conquer App Complexity with Docker
- Keep it Simple
- Move Fast
- Collaborate
Developing with Docker
Developing apps today requires so much more than writing code.
Multiple languages, frameworks, architectures,
and discontinuous interfaces between tools for each lifecycle stage
creates enormous complexity.
Docker simplifies and accelerates your workflow, while giving
developers the freedom to innovate with their choice of tools,
application stacks, and deployment environments for each project.
What is a Container?

An isolated environment for


running an application
What is a Container?
Use containers to Build, Share and Run your applications.

A container is a standard unit of software that packages up code and all


its dependencies so the application runs quickly and reliably from one
computing environment to another.
A docker container image is a lightweight, standalone, executable
package of software that includes everything needed to run an
application: code, runtime, system tools, system libraries and settings.
Docker Image & Container

Container

Container Instance of an image


Image Runs a program
Container
Single file with all the
deps and config Container
required to run a program
Docker Container
Virtual Machine

An abstraction of a machine
(physical hardware)
Hypervisors
- Virtual Box
- VMware
- Hyper-V (windows only)
Virtual Machine

Run applications in
isolation
Virtual Machine – Problems
- Each VM needs a full-blown OS
- Slow to start
- Resource intensive
Docker Containers vs Virtual Machine
Docker Containers
- Allow running multiple apps in
isolation
- Are lightweight
- Use OS of the host
- Start quickly
- Need less hardware resources
The bad old days
Applications run businesses.
If applications break, businesses break. Sometimes they even go bust.
These statements get truer every day!
Docker Advantages
- Docker for Everyone
- Environment Isolation
- OS Independent Apps
- Rapid Development & Deployment
- Scalability & Flexibility Made Easier
- No More Security Issues
- Ship Anytime, Anywhere
- Dependency Management Made Easier
Docker Architecture
The Docker daemon
The Docker daemon (dockerd) listens for Docker API requests and
manages Docker objects such as images, containers, networks, and
volumes.
A daemon can also communicate with other daemons to manage
Docker services.
The Docker client
The Docker client (docker) is the primary way that many Docker users
interact with Docker.
When you use commands such as docker run, the client sends these
commands to dockerd, which carries them out.
The docker command uses the Docker API. The Docker client can
communicate with more than one daemon.
Docker Client & Docker Server
- Docker Client (Docker CLI)
- Tool that we are going to issue commands to

- Docker Server (Docker Daemon)


- Tool that is responsible for creating images, running containers, etc.
Docker Today
Install Docker Desktop on Windows
- WSL 2 backend
- Hyper-V backend and Windows containers

- Link: https://docs.docker.com/desktop/windows/install/#system-requirements
Docker Desktop on
Windows 10
WSL 2 backend
Using the Docker Client
~$ docker version
~$ docker info
~$ docker run hello-world
Using the Docker Client
Your Computer Docker Hub
Single file with
everything needed
docker run hello-world Docker Client hello-word to run one specific
program
redis

Docker Server other image a


Image Cache other image b
other image c
other image d
Using the Docker Client
Your Computer Docker Hub
Single file with
everything needed
docker run hello-world Docker Client hello-word to run one specific
program
redis

Docker Server other image a


Image Cache other image b
hello-word other image c
other image d
docker run -it redis
Operating System
Processes running
on your computer Chrome Ansible NodeJS Spotify

Running program issues


System Call System Call System Call request to kernel to
interact with a piece of
hardware

Kernel

CPU Memory Hard Disk


Operating System – Kernel

A kernel manages applications and


hardware resources
Operating System
Needs Python v2 Needs Python v3

Chrome NodeJS

System Call System Call System Call

Kernel

Python v2 Hard Disk


Operating System
Needs Python v2 Needs Python v3

Chrome NodeJS

System Call System Call System Call

Kernel
Segment of HD Python v2 Python v3 Segment of HD
for Chrome Hard Disk for Node
Operating System
Needs Python v2 Needs Python v3

Chrome NodeJS

System Call to read HD

Kernel Which process is making this system call?

Segment of HD Python v2 Python v3 Segment of HD


for Chrome Hard Disk for Node
Linux Operating System

Namespacing Processes Hard drive Network


Isolating resources per
process (or group of
Users Hostnames Inter Process Com.
processes)

Control Groups Memory CPU Usage HD I/O


(cgroups)
Network Bandwidth
Limit amount of
resources used per
process
Container
Needs Python v3

Chrome NodeJS

System Call to read HD


Container

Kernel Which process is making this system call?

Python v2 Python v3 Segment of HD


Hard Disk for Node
Container *
Container

Chrome

Kernel

Portion of each Hard Drive CPU


made available to
process Network RAM
Container *
Container

Running
Process
Image
FS Snapshot Startup Command
Kernel
Chrome Python > Run Chrome

Network CPU RAM

Hard Drive
Container *
Container

Running
Process
Image
FS Snapshot Startup Command
Kernel
Chrome Python > Run Chrome

Network CPU RAM

Chrome Python
Container *
Container

Running
Chrome Process
Image
FS Snapshot Startup Command
Kernel
Chrome Python > Run Chrome

Network CPU RAM

Chrome Python
Docker Container

Running Running Running


Process Process Process

Linux Kernel

Linux Virtual Machine

MacOS / Windows

Your computers hardware


Creating & Running a Container from an Image
Try to create
and run a
container

docker run <image name>

Reference Name of
the Docker image to use
Client for this
container
Manipulating Containers with the Docker CLI
~$ docker run hello-world Container

Running
hello-world Process
Image
FS Snapshot Startup Command
Kernel
hello-world > Run hello-world

Network CPU RAM

hello-world Hard drive segment


for this process
Creating & Running a Container from an Image

Try to create
and run a Default command
container override

docker run <image name> command!

Reference Name of
the Docker image to use
Client for this
container
Manipulating Containers with the Docker CLI
~$ docker run busybox echo hi
~$ docker run busybox ls
Manipulating Containers with the Docker CLI
~$ docker run busybox ls Container

Running
ls Process
Busybox Image
FS Snapshot Startup Command Kernel
bin dev etc
> ??? Network CPU RAM
home proc root
bin dev etc
Hard drive segment
home proc root for this process
Listing Running Containers
~$ docker ps
List all running
containers

docker ps

Reference
the Docker
Client
Listing Running Containers
~$ docker ps
~$ docker run busybox ping google.com
~$ docker ps
~$ docker ps --all
Container Lifecycle
~$ docker run

docker run = docker create + docker start


Container Lifecycle
Try to create
the container

Create a
docker create <image name>
Container
Reference Name of image to use
the Docker Client for this container
Try to start
the container
Start a
docker start <container id>
Container
Reference ID of the container to
the Docker Client start
Container Lifecycle – Create a Container
Container
~$ docker create
Running
Process

Image
FS Snapshot Startup Command Kernel
hello-world > Run hello-world
Network CPU RAM

hello-world Hard drive segment


for this process
Container Lifecycle – Start a Container
Container
~$ docker start
Running
hello-world
Process

Image
FS Snapshot Startup Command Kernel
hello-world > Run hello-world
Network CPU RAM

hello-world Hard drive segment


for this process
Container Lifecycle
~$ docker create hello-world
~$ docker start –a <container id>
Restarting Stopped Containers
~$ docker run busybox echo hi there
~$ docker ps --all
~$ docker start -a <container id>
~$ docker start -a <container id> echo by (Error)
Removing Stopped Containers
~$ docker system prune
~$ docker ps –all
Retrieving Log Outputs
~$ docker create busybox echo hi there
~$ docker start <container id>
~$ docker logs <container id>

Get Logs

Get logs from


a container
docker logs <container id>

Reference ID of the container


the Docker Client to look at
Stopping Containers
~$ docker create busybox ping google.com
~$ docker start <container id>
~$ docker logs <container id>
~$ docker ps
~$ docker stop <container id>

Stop or Kill
a Container
docker stop <container id>

Reference Stop container ID of the container


the Docker Client to look at
Stopping Containers
~$ docker stop Container

Running
stop SIGTERM ping google.com
Process

Kernel
Network CPU RAM

bin dev etc


Hard drive segment
home proc root for this process
Stopping Containers
Container

Running
stop SIGKILL ping google.com
Process

Kernel
~$ docker start <c_id> Network CPU RAM

~$ docker kill <c_id> bin dev etc


Hard drive segment
home proc root for this process
Development Workflow

Application Image
Dockerfile
Development Workflow – Image
- A cut-down OS
- A runtime environment
- Application files
- Third-party libraries
- Environment variables
Development Workflow – Container

Image Container
Development Workflow – Container
~$ docker run hello-world Container

Running
hello-world Process
Hello-World Image
FS Snapshot Startup Command
Kernel
hello-world > Run hello-world

Network CPU RAM

hello-world Hard drive segment


for this process
Development Workflow

Dev Test / Prod


Registry – Docker Hub
Docker in Action - Build
~$ build
Get a head start on your coding by leveraging Docker images to
efficiently develop your own unique applications on Windows and Mac.
Create your multi-container application using Docker Compose.
Integrate with your favorite tools throughout your development
pipeline - Docker works with all development tools you use including
VS Code, CircleCI and GitHub.
Package applications as portable container images to run in any
environment consistently from on-premises Kubernetes to AWS ECS,
Azure ACI, Google GKE and more.
Deploying Docker containers on Amazon ECS
Amazon ECS
Automated CI-CD Deployment

Deploy code into containers running on Amazon ECS


using AWS Developer Tools.
Deploying Docker containers on Azure
Google (GKE) - Continuous delivery pipeline
Docker Images

Hello-World Image Busybox Image


FS Snapshot Startup Command FS Snapshot Startup Command

hello-world > Run hello-world busybox stuff > ???


Creating Docker Images

Docker Docker Usable


Dockerfile
Client Server Image!

Configuration to define
how our container
should behave
Creating Docker Images

Creating a Dcokerfile

Specify a base image

Run some commands to


install additional programs

Specify a command to run on


container startup
Creating Docker Images

Create an docker Image and runs node server

Goal
Creating Docker Images
~$ mkdir node-image
~$ cd node-image
~$ node-image > code .

~$ app.js
~$ Dockerfile
Creating Docker Images

~$ node-image > docker build -t node-image .


~$ node-image > docker image ls
~$ node-image > docker run node-image
Creating Docker Images
Creating Docker Images

You might also like