An Introduction to Docker
1
Ruben Taelman - @rubensworks
imec - Ghent University
So what is Docker?
Platform for running software in isolated containers
2
So what is Docker?
Platform for running software in isolated containers
3
Traditional webserver
NGINX
PostgreSQL
Node
So what is Docker?
Platform for running software in isolated containers
4
Traditional webserver Docker webserver
NGINX
PostgreSQL
Node
NGINX
PostgreSQL
Node
So what is Docker?
Platform for running software in isolated containers
5
Traditional webserver Docker webserver
NGINX
PostgreSQL
Node
NGINX
PostgreSQL
Node
✓Linux ✓Windows ✓Mac
(Reproducible) software evaluations
Easily installable and disposable software
Running different software versions simultaneously
What to use Docker for?
6
Overview
Basics
Networks
Convenience tools
7
Overview
Basics
Networks
Convenience tools
8
Virtual machines vs containers
(Docker) containers don’t need separate OS’s, so they are more lightweight.
9
Docker workflow
10
Dockerfile Image Container
Build Run
Description of an image Built image
Available for reuse
Instantiated image
Dockerfile
Layered image descriptions
11
Debian
Node JS
LDF server
12
# Inherit from the Node 4.2.2 image
FROM node:4.2.2
# Copy the server files
ADD . /app
# Install the node module
RUN cd /app && npm install
# Expose the default port
EXPOSE 3000
# Run base binary
ENTRYPOINT ["node", "bin/ldf-server"]
# Default command
CMD ["--help"]
> cat Dockerfile
RUN Executed when the image is being built
Zero or more RUN’s per Dockerfile
Typically used for installing software
Result will be stored in image
CMD Executed when the image is being run/instantiated
Only one CMD per Dockerfile
Default command to run when starting the container
Can be overridden by user
! Difference between RUN and CMD !
13
Build image to your local repository
Building a Dockerfile to an image
14
> docker build -t <image-name> <path-to-dir>
All your available images
Your local image repository
15
> docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ldf-server latest db3a7fb38cb2 10 minutes ago 683.3 MB
stuff latest do89d002jbsd 1 week ago 102.2 MB
http://hub.docker.com
Reuse existing images
16
Search the Docker hub, and download images to your local repo
Or use the command-line...
17
> docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s... 4958 [OK]
ubuntu-upstart Upstart is an event-based replacement for ... 67 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 47 [OK]
> docker pull ubuntu
12.04: Pulling from library/ubuntu
ba2b457ecfb2: Pull complete
26180b0fe8fb: Pull complete
edd7c1974a70: Pull complete
57bca5139a13: Pull complete
library/ubuntu:12.04: The image you are pulling has been verified.
Status: Downloaded newer image for ubuntu:12.04
Instantiate an image
Running an image
18
> docker run -it --rm ldf-server
Important options:
-i
-t
--rm
-d
-p 8080:80
-v /mydir:/targetdir
...
Keep STDIN open
Allocate new terminal
Remove container after stopping (default: stopped state)
Run in background
Map host port to container port
Map local directory to container directory
List running containers
List running + stopped containers
Stop container
Remove container
List images
Remove image
Image and container management
19
docker ps
docker ps -a
docker stop <container-hash-or-name>
docker rm <container-hash-or-name>
docker images
docker rmi <image-name>
Attach an image (not guaranteed to have a CLI)
Using detached containers
20
> docker attach <container-hash>
Open shell in container (useful for debugging)
> docker exec -it <container-hash> /bin/bash
CTRL+p CTRL+q to detach
Overview
Basics
Networks
Convenience tools
21
Connect microservices
using virtual Docker networks
22
Docker webserver
NGINX
PostgreSQL
Node
Managing networks
23
> docker network ls
NETWORK ID NAME DRIVER
7fca4eb8c647 bridge bridge
9f904ee27bf5 none null
cf03ee007fb4 host host
Default
Dummy network
Same as host network
> docker network create --driver bridge <network-name>
> docker network inspect <network-name>
> docker run --network=<network-name> <image-name>
using Docker swarm
Swarm
Running containers on multiple hosts
24
Manager
Node 1Node 0 Node 2
Scale applications, by running containers N times
Load balancing
Automatic management
Deploying applications in clusters
25
Scale applications, by running containers N times
Load balancing
Automatic management
Deploying applications in clusters
26
Overview
Basics
Networks
Convenience tools
27
Docker Compose
Declaratively define multi-container applications in docker-compose.yml
28
> docker-compose up -d
> docker-compose stop
> docker-compose rm
29
version: '2'
services:
server:
image: linkeddatafragments/server.js
ports:
- "3000:3000"
volumes:
- ${DATA_DIR}:/data
- ./config-server.json:/tmp/config.json
command: /tmp/config.json 3000 ${LDF_WORKERS}
server-cache:
image: nginx:stable
ports:
- "80:80"
volumes:
- ./nginx-default:/etc/.../default:ro
links:
- server
> cat docker-compose.yml
Docker Machine
Install Docker Engine on any host
Easily SSH to any host to control it
30
Methods for measuring CPU, memory usage, I/O …
Third-party alternatives: Datadog, CAdvisor, Scout ...
Monitoring
31
> docker stats
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O
1285939c1fd3 0.07% 796 KiB / 64 MiB 1.21% 788 B / 648 B 3.568 MB / 512 KB
9c76f7834ae2 0.07% 2.746 MiB / 64 MiB 4.29% 1.266 KB / 648 B 12.4 MB / 0 B
d1ea048f04e4 0.03% 4.583 MiB / 64 MiB 6.30% 2.854 KB / 648 B 27.7 MB / 0 B
Overview
Basics
Networks
Convenience tools
32

Docker Intro

  • 1.
    An Introduction toDocker 1 Ruben Taelman - @rubensworks imec - Ghent University
  • 2.
    So what isDocker? Platform for running software in isolated containers 2
  • 3.
    So what isDocker? Platform for running software in isolated containers 3 Traditional webserver NGINX PostgreSQL Node
  • 4.
    So what isDocker? Platform for running software in isolated containers 4 Traditional webserver Docker webserver NGINX PostgreSQL Node NGINX PostgreSQL Node
  • 5.
    So what isDocker? Platform for running software in isolated containers 5 Traditional webserver Docker webserver NGINX PostgreSQL Node NGINX PostgreSQL Node ✓Linux ✓Windows ✓Mac
  • 6.
    (Reproducible) software evaluations Easilyinstallable and disposable software Running different software versions simultaneously What to use Docker for? 6
  • 7.
  • 8.
  • 9.
    Virtual machines vscontainers (Docker) containers don’t need separate OS’s, so they are more lightweight. 9
  • 10.
    Docker workflow 10 Dockerfile ImageContainer Build Run Description of an image Built image Available for reuse Instantiated image
  • 11.
  • 12.
    12 # Inherit fromthe Node 4.2.2 image FROM node:4.2.2 # Copy the server files ADD . /app # Install the node module RUN cd /app && npm install # Expose the default port EXPOSE 3000 # Run base binary ENTRYPOINT ["node", "bin/ldf-server"] # Default command CMD ["--help"] > cat Dockerfile
  • 13.
    RUN Executed whenthe image is being built Zero or more RUN’s per Dockerfile Typically used for installing software Result will be stored in image CMD Executed when the image is being run/instantiated Only one CMD per Dockerfile Default command to run when starting the container Can be overridden by user ! Difference between RUN and CMD ! 13
  • 14.
    Build image toyour local repository Building a Dockerfile to an image 14 > docker build -t <image-name> <path-to-dir>
  • 15.
    All your availableimages Your local image repository 15 > docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ldf-server latest db3a7fb38cb2 10 minutes ago 683.3 MB stuff latest do89d002jbsd 1 week ago 102.2 MB
  • 16.
  • 17.
    Search the Dockerhub, and download images to your local repo Or use the command-line... 17 > docker search ubuntu NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating s... 4958 [OK] ubuntu-upstart Upstart is an event-based replacement for ... 67 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 47 [OK] > docker pull ubuntu 12.04: Pulling from library/ubuntu ba2b457ecfb2: Pull complete 26180b0fe8fb: Pull complete edd7c1974a70: Pull complete 57bca5139a13: Pull complete library/ubuntu:12.04: The image you are pulling has been verified. Status: Downloaded newer image for ubuntu:12.04
  • 18.
    Instantiate an image Runningan image 18 > docker run -it --rm ldf-server Important options: -i -t --rm -d -p 8080:80 -v /mydir:/targetdir ... Keep STDIN open Allocate new terminal Remove container after stopping (default: stopped state) Run in background Map host port to container port Map local directory to container directory
  • 19.
    List running containers Listrunning + stopped containers Stop container Remove container List images Remove image Image and container management 19 docker ps docker ps -a docker stop <container-hash-or-name> docker rm <container-hash-or-name> docker images docker rmi <image-name>
  • 20.
    Attach an image(not guaranteed to have a CLI) Using detached containers 20 > docker attach <container-hash> Open shell in container (useful for debugging) > docker exec -it <container-hash> /bin/bash CTRL+p CTRL+q to detach
  • 21.
  • 22.
    Connect microservices using virtualDocker networks 22 Docker webserver NGINX PostgreSQL Node
  • 23.
    Managing networks 23 > dockernetwork ls NETWORK ID NAME DRIVER 7fca4eb8c647 bridge bridge 9f904ee27bf5 none null cf03ee007fb4 host host Default Dummy network Same as host network > docker network create --driver bridge <network-name> > docker network inspect <network-name> > docker run --network=<network-name> <image-name>
  • 24.
    using Docker swarm Swarm Runningcontainers on multiple hosts 24 Manager Node 1Node 0 Node 2
  • 25.
    Scale applications, byrunning containers N times Load balancing Automatic management Deploying applications in clusters 25
  • 26.
    Scale applications, byrunning containers N times Load balancing Automatic management Deploying applications in clusters 26
  • 27.
  • 28.
    Docker Compose Declaratively definemulti-container applications in docker-compose.yml 28 > docker-compose up -d > docker-compose stop > docker-compose rm
  • 29.
    29 version: '2' services: server: image: linkeddatafragments/server.js ports: -"3000:3000" volumes: - ${DATA_DIR}:/data - ./config-server.json:/tmp/config.json command: /tmp/config.json 3000 ${LDF_WORKERS} server-cache: image: nginx:stable ports: - "80:80" volumes: - ./nginx-default:/etc/.../default:ro links: - server > cat docker-compose.yml
  • 30.
    Docker Machine Install DockerEngine on any host Easily SSH to any host to control it 30
  • 31.
    Methods for measuringCPU, memory usage, I/O … Third-party alternatives: Datadog, CAdvisor, Scout ... Monitoring 31 > docker stats CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O 1285939c1fd3 0.07% 796 KiB / 64 MiB 1.21% 788 B / 648 B 3.568 MB / 512 KB 9c76f7834ae2 0.07% 2.746 MiB / 64 MiB 4.29% 1.266 KB / 648 B 12.4 MB / 0 B d1ea048f04e4 0.03% 4.583 MiB / 64 MiB 6.30% 2.854 KB / 648 B 27.7 MB / 0 B
  • 32.