ch09 docker-introduction
ch09 docker-introduction
https://www.youtube.com/watch?v=ZNdc4-yFTeA
https://github.com/docker/labs/blob/master/slides/docker-java-dockercon-2017.pdf
1
What is Docker?
▪Open source project and company
3
2
Containers & Images
Running Your First Container
3
What Are Linux Containers?
Linux containers are a way to create isolated environments that can run code while sharing a
single operating system.
Each container is
Container 1 Container 2 Container 3
completely isolated
from the others
App App App
Physical Server + OS
A computer somewhere - could even be the laptop
or desktop computer you’re using right now!
4
Why Docker?
Managing Linux containers is hard.
Docker is a tool that makes it much easier to manage Linux containers.
Docker Engine
Physical Server + OS
5
6
6
How Can Docker Help Me?
There are many different ways people can use Docker.
Developers
Deployment
7
Installing Docker
The simplest way to install Docker is to download one of the official Docker applications.
Applications
Docker for Mac - Community Edition
Docker for Windows - Community Edition
Installation Instructions
Linux AWS
8
Containers & Images
An image is a blueprint for creating a container.
Image Container
Pre-built images
available in Docker
Store (and Docker
Hub)
9
Dockerfiles
Automating the Creation of Custom Images
10
The Problem: Creating Containers Is Clunky
Creating containers from the command line works, but it quickly gets a little clunky the more
customization that you need to do.
Image
1 open port 80
Container
2 update package manager
3 download a package
Image Container
Dockerfile
1 open port 80
12
FROM ubuntu
FROM openjdk
7
19
Volumes
Working With Data in Containers
13
Getting Data Into Containers
If the image you’re building a container with doesn’t already contain application files, you’ll
need an extra step to get them into your container.
14
The Problem: Containers Don’t Persist Data
Our containers aren’t really doing much right now because we don’t have a way to get data in them.
Modified data
is gone!
Stop
Start Modify
container
container files in
container
15
The Solution: Data Volumes
Data volumes expose files on your host machine to the container.
Host Container
Volume
Data is
still there!
Image
Anywhere
5
18
8
20
https://docs.docker.com/engine/reference/builder/
Docker Workflow
9
21
Image Layers - OpenJDK
~ > docker image ls openjdk
REPOSITORY TAG IMAGE ID CREATED SIZE
openjdk latest d23bdf5b1b1b 5 days ago 643 MB
~ > docker image history openjdk
IMAGE CREATED CREATED BY SIZE
COMMENT
d23bdf5b1b1b 5 days ago /bin/sh -c /var/lib/dpkg/info/ca-certifica... 419 kB
<missing> 5 days ago /bin/sh -c set -x && apt-get update && a... 352 MB
<missing> 5 days ago /bin/sh -c #(nop) ENV CA_CERTIFICATES_JAV... 0 B
<missing> 5 days ago /bin/sh -c #(nop) ENV JAVA_DEBIAN_VERSION... 0 B
<missing> 5 days ago /bin/sh -c #(nop) ENV JAVA_VERSION=8u111 0 B
<missing> 5 days ago /bin/sh -c #(nop) ENV JAVA_HOME=/usr/lib/... 0 B
<missing> 5 days ago /bin/sh -c { echo '#!/bin/sh'; echo 's... 87 B
<missing> 5 days ago /bin/sh -c #(nop) ENV LANG=C.UTF-8 0 B
<missing> 5 days ago /bin/sh -c echo 'deb http://deb.debian.org... 55 B
<missing> 5 days ago /bin/sh -c apt-get update && apt-get insta... 1.29 MB
<missing> 5 days ago /bin/sh -c apt-get update && apt-get insta... 123 MB
<missing> 5 days ago /bin/sh -c apt-get update && apt-get insta... 44.3 MB
<missing> 6 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 6 days ago /bin/sh -c #(nop) ADD file:89ecb642d662ee7... 123 MB
10
22
Docker for AWS/Azure
▪Amazon Web Services
– EKS
– Integrated with AutoScaling, ELB, and EBS.
▪Azure
– Integrated with VM Scale Sets for autoscaling, Azure Load
Balancer, Azure Storage
▪docker.com
11
23
Docker for Mac/Windows
▪Native application and UI
▪Auto update capability
▪No additional software required, e.g. VirtualBox
– OSX: xhyve VM using Hypervisor.framework
– Windows: Hyper-V VM
▪Download: docker.com/getdocker
▪Requires Yosemite 10.10+ or Windows 10 64-bit
12
24
25
26
27
28
29
30
FROM python:3.6-alpine Docker default runs as root!
RUN adduser -D microblog You should alway create user for your
container!
WORKDIR /home/microblog
COPY requirements.txt requirements.txt
RUN apk add --no-cache --update gcc musl-dev libffi-dev openssl-dev
RUN python3 -m venv venv
RUN venv/bin/pip3 install --upgrade pip
RUN venv/bin/pip3 install -r requirements.txt
RUN venv/bin/pip3 install gunicorn
COPY app app
COPY migrations migrations
COPY microblog.py config.py run.py boot.sh ./
RUN chmod +x boot.sh
ENV FLASK_APP run.py
RUN chown -R microblog:microblog ./
USER microblog
EXPOSE 5000
ENTRYPOINT ["./boot.sh"] 31
Docker Compose
▪Defining and running multi-container applications
▪Configuration defined in one or more files
– docker-compose.yml (default)
– docker-compose.override.yml (default)
– Multiple files specified using -f
▪Deployed as Docker Stack
▪Great for dev, staging, and CI
13
32
Docker Compose - One Service
version: “3”
services:
db:
image: couchbase
volumes:
- ~/couchbase:/opt/couchbase/var
ports:
- 8091:8091
- 8092:8092
- 8093:8093
- 11210:11210
docker-compose up -d
GET
POST
PUT
DELETE
CRUD
using
N1QL
15
34
Docker Compose - Two Services
version: “3”
services:
db:
image: arungupta/couchbase:travel
ports:
- 8091:8091
- 8092:8092
- 8093:8093
- 11210:11210
web:
image: arungupta/couchbase-wildfly-javaee:travel
environment:
- COUCHBASE_URI=db
ports:
- 8080:8080
- 9990:9990
docker-compose.yml
web:
ports:
- 9080:8080
docker-compose.override.yml
17
36
Dev/Prod with Compose
db-dev: docker-compose.yml
image: arungupta/couchbase
ports:
- . . .
web:
image: arungupta/wildfly
environment:
- COUCHBASE_URI=db-dev:8093
ports:
- 8080:8080 docker-compose up -d
web: production.yml
environment:
- COUCHBASE_URI=db-prod:8093
ports: docker-compose up
- 80:8080 -f docker-compose.yml
db-prod: -f production.yml
image: . . . -d 18
37
Docker Compose Common Use Cases
Use Case Command
Dev Setup docker-compose up
Local/remote host DOCKER_HOST, DOCKER_TLS_VERIFY,
DOCKER_CERT_PATH
Single/multiple hosts Integrated with Swarm
Multiple isolated environments docker-compose up -p <project>
Automated test setup docker-compose up
mvn test
docker-compose down
Dev/Prod Impedance mismatch docker-compose up -f docker-compose.yml -f
production.yml
19
38
Docker 1.13
▪Deploy Compose services to Swarm
▪CLI restructured
▪Clean-up commands
▪Monitoring commands
▪Build improvements
▪Improved CLI backwards compatibility
▪Docker for AWS/Azure for Production
20
39
Docker 1.13 - Compose v3
21
40
Docker 1.13 - CLI Restructured
Management Commands:
checkpoint Manage checkpoints
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
22
41
Docker 1.13 - Cleanup Commands
▪docker system df and docker system cleanup
docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 15 1 5.081 GB 4.498 GB (88%)
Containers 1 0 130.1 kB 130.1 kB (100%)
Local Volumes 7 0 110.1 MB 110.1 MB (100%)
23
42
Docker 1.13 - Monitoring Commands
▪docker service logs and Prometheus endpoint
24
43
Docker 1.13 - Build Improvements
25
44
Swarm Mode
▪New in 1.12
▪Natively managing a cluster of Docker Engines called a Swarm
▪Docker CLI to create a swarm, deploy apps, and manage swarm
– Optional feature, need to be explicitly enabled
▪No Single Point of Failure (SPOF)
▪Declarative state model
▪Self-organizing, self-healing
▪Service discovery, load balancing and scaling
▪Rolling updates
26
45
Swarm Mode: Initialize
31
50
https://github.com/docker/labs/blob/master/swarm-mode/quickstart/buildswarm-node-vbox-setup.sh
Swarm Mode: Protocols
Raft Consensus Group
primary secondary secondary Container
Swarm Worker Swarm Worker Swarm Worker Swarm Worker Swarm Worker
Gossip Network
Strongly consistent
Replicated (Raft based)
Extremely fast (in-memory reads) 51
Swarm Mode in Production
33
52
https://blog.online.net/2016/07/29/docker-swarm-an-analysis-of-a-very-large-scale-container-system/
Secure by Default
34
53
Swarm Mode: Replicated Service
36
55
Swarm Mode: Routing Mesh
Load
Balancer
X
38
57
Swarm Mode: Desired != Actual
39
58
Swarm Mode: Reconcile
40
59
Swarm Mode: Container Failure
X
41
60
Swarm Mode: Desired != Actual
42
61
Swarm Mode: Reconcile
43
62
Swarm Mode: Scale
X For Debugging
X OS Update.
DOCKER_OPTS="--label=wildfly.storage=ssd"
50
69
Swarm Mode: Constraints
Node
Constraint Default or custom tags node, operatinsystem, kernelversion, …
Container
Affinity “Attraction” between containers -e affinity:container=<name>/<id>, image, …
55
74
Optimal Utilization of Resources
couchbase.mds=query
couchbase.mds=query couchbase.mds=index couchbase.mds=data
couchbase.mds=query couchbase.mds=index
couchbase.mds=index couchbase.mds=data
couchbase.mds=data
couchbase.mds=index couchbase.mds=data
couchbase.mds=data
couchbase.mds=data
replicas=1 replicas=3
57
http://blog.couchbase.com/2016/september/docker-service-swarm-mode-couchbase-cluster 76
Docker Lifecycle
Dockerfile
FROM …
CMD …
58
77
Distributed Application Bundle
docker-compose.yml
version: "2"
services:
Distributed
db: docker-compose docker CONTAINER
CONTAINER
Application CONTAINER
… build deploy Stack
Bundle
web:
…
59
git push 3
clone
Create Run Test Docker Hub
Image
4 5 6
61
http://blog.couchbase.com/2016/september/deployment-pipeline-docker-jenkins-java-couchbase 80
References
▪Slides: github.com/docker/labs/tree/master/slides
▪Workshop: github.com/docker/labs/tree/master/java
▪Docs: docs.docker.com
64
82