Introduction to Docker
#TechEdDevCon
Ondrej Sika
[email protected]
@ondrejsika
TechEd,
Prague, 17. 5. 2018
Slide 2
Slide 2 text
Agenda
- What is a Docker?
- Usage
- Containers
- Images
- Docker Registry
- Composites
- Swarm
Slide 3
Slide 3 text
What is Docker?
Slide 4
Slide 4 text
What is Docker?
Docker is an open-source project
that automates the deployment of
applications inside software
containers. ...
Docker containers wrap up a piece
of software in a complete filesystem
that contains everything it needs to
run: code, runtime, system tools,
system libraries – anything you can
install on a server.
Slide 5
Slide 5 text
Containers vs virtualization
Slide 6
Slide 6 text
Virtualization
A VM is an abstraction of physical
hardware. Each VM has a full server
hardware stack from virtualized
BIOS to virtualized network
adapters, storage, and CPU.
That stack allows run any OS on
your host but it takes some power.
Slide 7
Slide 7 text
Containers
Containers are abstraction in linux
kernel, just proces, memory,
network, … namespaces.
Containers run in same kernel as
host - it is not possible use different
OS or kernel version, but containers
are much more faster than VMs.
Slide 8
Slide 8 text
History of
containers
- 1979 - Chroot
- 2000 - BSD Jails
- 2001 - VServer
- 2004 - Solaris Containers
- 2005 - OpenVZ
- 2006 - Process Containers
- 2008 - LXC
- 2013 - Docker
Slide 9
Slide 9 text
Advantages - Performance
- Management
- Containers distribution
Slide 10
Slide 10 text
Disadvantages - Security
- One kernel / "Linux only"
Slide 11
Slide 11 text
Usage
- Almost everywhere
- Development, Testing,
Production
- Better deployment process
- Separates running applications
Slide 12
Slide 12 text
Work with
Cluster managements
- Swarm (Native)
- Open Stack
- Kubernetes
Slide 13
Slide 13 text
Installation
Slide 14
Slide 14 text
https://docs.docker.com/engine/installation/
Slide 15
Slide 15 text
Test the installation
docker run hello-world
...
Hello from Docker!
This message shows that your installation
appears to be working correctly.
Slide 16
Slide 16 text
Basic Usage
Slide 17
Slide 17 text
Image and
Container
An image is an inert, immutable, file
that's essentially a snapshot of a
container. Images are created with
the build command, and they'll
produce a container when started
with run. Images are stored in a
Docker registry..
Slide 18
Slide 18 text
Help
docker
docker help
docker help
# eg. docker help run, docker help help
Slide 19
Slide 19 text
System wide info
docker version # print version
docker system info # system vide information
docker system df # docker disk usage
docker system prune # cleanup unused data
Slide 20
Slide 20 text
Docker Images
docker image ls # list all images
docker image ls -q # quiet output, just IDs
docker image rm # remove image
docker image pull # pull from registry
docker image push # push to registry
docker tag image # add new tag
Slide 21
Slide 21 text
Docker Run
docker run []
# Eg.:
docker run hello-world
docker run debian:8 cat /etc/os-release
docker run debian:9 cat /etc/os-release
docker run -ti debian
Slide 22
Slide 22 text
Common Docker Run Params
--name
-d - run as daemon
-ti - map TTY a STDIN (for bash eg.)
-e = - set ENV variable
-h - set hostname
-u - run command by specific user
--rm - remove container after stop
List containers
docker ps - list running containers
docker ps -a - list all containers
docker ps -a -q - list IDs of all containers
# Eg.:
docker rm -f $(docker ps -a -q)
Slide 25
Slide 25 text
Docker Exec
docker exec
-d - run command as daemon
-e = - set ENV variable
-ti - map TTY a STDIN (for bash eg.)
-u - run command by specific user
# Eg.:
docker exec my-debian ls
Docker Inspect
Get lots of information about
container in JSON.
docker inspect
Slide 28
Slide 28 text
Docker Volumes
Volumes are persistent data storage
for containers.
Volumes can be shared between
containers and data are written
directly to host.
Slide 29
Slide 29 text
Volumes
docker run -ti -v /data debian
docker run -ti -v my-volume:/data debian
docker run -ti -v $(pwd)/my-data:/data debian
Slide 30
Slide 30 text
Port Forwarding Docker can forward specific port
from container to host
Slide 31
Slide 31 text
Port forwarding
docker run -p :
# eg.:
docker run -ti -p 8080:80 nginx
Slide 32
Slide 32 text
Own Images
Slide 33
Slide 33 text
Dockerfile
Dockerfile is preferred way to
create images.
Dockerfile defines each layer of
image by some command.
To make image use command
docker build
Slide 34
Slide 34 text
Dockerfile
FROM - define base image
MAINTAINER - set maintainers name &
email
RUN - run command and save as layer
COPY - copy file or
directory to image layer
ADD - instead of copy,
archives added by add are extracted
Slide 35
Slide 35 text
Dockerfile
ENV - set ENV variable
USER - switch user
WORKDIR - change working directory
VOLUME - define volume
ENTRYPOINT - command, run on container
starts
CMD - parameters for entrypoint
Slide 36
Slide 36 text
.dockerignore
Ignore files for docker build process.
Similar to .gitignore
Slide 37
Slide 37 text
.dockerignore
# comment
*/temp*
*/*/temp*
temp?
Slide 38
Slide 38 text
Build Image from Dockerfile
docker build -t
docker build -f -t
docker tag
# eg.:
docker build -t my-image .
Dockerfile
FROM python:3.6-alpine
COPY requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY . /app
CMD python app.py
Slide 43
Slide 43 text
https://github.com/ondrejsika/teched18
Slide 44
Slide 44 text
Run
docker build -t teched18 .
docker run --name redis -d redis:alpine
docker run -p 80:80 --link redis teched18
Slide 45
Slide 45 text
Docker Registry
Slide 46
Slide 46 text
What is
Docker Registry?
A service responsible for hosting and
distributing images.
The default registry is the Docker
Hub.
GitLab contains Docker Registry
from version 8.
Slide 47
Slide 47 text
What is
Docker Hub?
Docker Hub is default public docker
registry.
You can host unlimited free images.
Docker Hub is source of our base
images.
Slide 48
Slide 48 text
Docker registry
docker login - Login to Docker Registry
docker logout - Logout from Docker Registry
docker pull - download image from registry
docker push - upload image to registry
Slide 49
Slide 49 text
Image name format
# Official image from Docker Inc on Docker Hub
debian
# Image in user namespace on Docker Hub
ondrejsika/debian
# Image on own Docker Registry (reg.istry.cz)
reg.istry.cz/debian
Slide 50
Slide 50 text
Docker Compose
Slide 51
Slide 51 text
What is
Docker Compose?
Compose is a tool for defining and
running multi-container Docker
applications.
With Compose, you use a Compose
file to configure your application's
services.
Run Compose
docker-compose up
docker-compose up -d
# Missing images will be downloaded or
builded
Slide 71
Slide 71 text
Compose Up Arguments
-d - run as daemon
--force-recreate - always create new cont.
--build - build on every run
--no-build - don't build, even images not
exist
--remove-orphans
--abort-on-container-exit
Remove Compose
docker-compose down
# stop and remove compose
Slide 74
Slide 74 text
Docker Swarm
Slide 75
Slide 75 text
What is
Docker Swarm?
A native clustering system for
Docker. It turns a pool of Docker
hosts into a single, virtual host using
an API proxy system. It is Docker's
first container orchestration project
that began in 2014. Combined with
Docker Compose, it's a very
convenient tool to manage
containers.