Docker Tutorial PDF
Docker Tutorial PDF
Docker Tutorial
Anthony Baire
This tutorial is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 France License
1 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Part 1.
Introduction
2 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
“Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications.
Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service
for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components
and eliminates the friction between development, QA, and production environments. As a result, IT can ship faster
and run the same app, unchanged, on laptops, data center VMs, and any cloud.”
source: https://www.docker.com/whatisdocker/
3 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
• a container manager
• lightweight virtualisation
(host and guest systems share the same kernel)
• based on linux namespaces and cgroups
• massively copy-on-write
• immutable images
• instant deployment
• suitable for micro-services (one process, one container)
→ immutable architecture
4 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
• a build system
• images may be build from sources
• using a simple DSL (Dockerfile)
5 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
6 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
In practice
A docker image is an immutable snapshot of the filesystem
A docker container is
• a temporary file system
• layered over an immutable fs (docker image)
• fully writable (copy-on-write1 )
• dropped at container’s end of life (unless a commit is made)
• a network stack
• with its own private address (by defaut in 172.17.x.x)
• a process group
• one main process launched inside the container
• all sub-process SIGKILLed when the main process exits
1
several possible methods: overlayfs (default), btrfs, lvm, zfs, aufs
7 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Installation
https://docs.docker.com/engine/installation/
Native installation:
Docker Machine:
• a command for provisionning an managing docker nodes
deployed:
• in a local VM (virtualbox)
• remotely (many cloud API supported)
8 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Part 2.
Managing containers
• create/start/stop/remove containers
• inspect containers
• interact, commit new images
9 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
10 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
command description
docker create image [ command ] create the container
docker run image [ command ] = create + start
docker start container. . . start the container
docker stop container. . . graceful2 stop
docker kill container. . . kill (SIGKILL) the container
docker restart container. . . = stop + start
docker pause container. . . suspend the container
docker unpause container. . . resume the container
docker rm [ -f3 ] container. . . destroy the container
2
send SIGTERM to the main process + SIGKILL 10 seconds later
3
-f allows removing running containers (= docker kill + docker rm)
11 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
4
or run
12 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...] Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
-a, --attach=[] Attach to STDIN, STDOUT or STDERR -a, --attach=false Attach STDOUT/STDERR and forward signals
--add-host=[] Add a custom host-to-IP mapping (host:ip) --help=false Print usage
--blkio-weight=0 Block IO (relative weight), between 10 and 1000 -i, --interactive=false Attach container's STDIN
--cpu-shares=0 CPU shares (relative weight)
--cap-add=[] Add Linux capabilities
--cap-drop=[] Drop Linux capabilities
--cgroup-parent= Optional parent cgroup for the container
--cidfile= Write the container ID to the file Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
--cpu-period=0 Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota=0 Limit CPU CFS (Completely Fair Scheduler) quota Stop a running container.
--cpuset-cpus= CPUs in which to allow execution (0-3, 0,1) Sending SIGTERM and then SIGKILL after a grace period
--cpuset-mems= MEMs in which to allow execution (0-3, 0,1)
--device=[] Add a host device to the container --help=false Print usage
--disable-content-trust=true Skip image verification -t, --time=10 Seconds to wait for stop before killing it
--dns=[] Set custom DNS servers
--dns-opt=[] Set DNS options
--dns-search=[] Set custom DNS search domains
-e, --env=[] Set environment variables
--entrypoint= Overwrite the default ENTRYPOINT of the image Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
--env-file=[] Read in a file of environment variables
--expose=[] Expose a port or a range of ports Restart a container
--group-add=[] Add additional groups to join
-h, --hostname= Container host name --help=false Print usage
--help=false Print usage -t, --time=10 Seconds to wait for stop before killing the container
-i, --interactive=false Keep STDIN open even if not attached
--ipc= IPC namespace to use
--kernel-memory= Kernel memory limit
-l, --label=[] Set meta data on a container
--label-file=[] Read in a line delimited file of labels Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
--link=[] Add link to another container
--log-driver= Logging driver for container Kill a running container
--log-opt=[] Log driver options
--lxc-conf=[] Add custom lxc options --help=false Print usage
-m, --memory= Memory limit -s, --signal=KILL Signal to send to the container
--mac-address= Container MAC address (e.g. 92:d0:c6:0a:29:33)
--memory-reservation= Memory soft limit
--memory-swap= Total memory (memory + swap), '-1' to disable swap
--memory-swappiness=-1 Tuning container memory swappiness (0 to 100)
--name= Assign a name to the container Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
--net=default Set the Network for the container
--oom-kill-disable=false Disable OOM Killer Remove one or more containers
-P, --publish-all=false Publish all exposed ports to random ports
-p, --publish=[] Publish a container's port(s) to the host -f, --force=false Force the removal of a running container (uses SIGKILL)
--pid= PID namespace to use --help=false Print usage
--privileged=false Give extended privileges to this container -l, --link=false Remove the specified link
--read-only=false Mount the container's root filesystem as read only -v, --volumes=false Remove the volumes associated with the container
--restart=no Restart policy to apply when a container exits
--security-opt=[] Security Options
--stop-signal=SIGTERM Signal to stop a container, SIGTERM by default
-t, --tty=false Allocate a pseudo-TTY
-u, --user= Username or UID (format: <name|uid>[:<group|gid>]) Usage: docker pause [OPTIONS] CONTAINER [CONTAINER...]
--ulimit=[] Ulimit options
--uts= UTS namespace to use Pause all processes within a container
-v, --volume=[] Bind mount a volume
--volume-driver= Optional volume driver for the container --help=false Print usage
--volumes-from=[] Mount volumes from the specified container(s)
-w, --workdir= Working directory inside the container 13 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
14 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
15 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
16 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
user (-u)
$ docker run debian whoami
root
$ docker run -u nobody debian whoami
nobody
18 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
hostname (-h)
$ docker run debian hostname
830e47237187
$ docker run -h my-nice-container debian hostname
my-nice-hostname
19 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
20 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Common rm idioms
22 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
command description
docker ps list running containers
docker ps -a list all containers
docker logs [ -f6 ] container show the container output
(stdout+stderr)
docker top container [ ps options ] list the processes running
inside the containers
docker diff container show the differences with
the image (modified files)
docker inspect container. . . show low-level infos
(in json format)
6
with -f, docker logs follows the output (à la tail -f)
23 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
command description
docker attach container attach to a running container
(stdin/stdout/stderr)
docker cp container:path hostpath|- copy files from the container
docker cp hostpath|- container:path copy files into the container
docker export container export the content of
the container (tar archive)
docker exec container args. . . run a command in an existing
container (useful for debugging)
docker wait container wait until the container terminates
and return the exit code
docker commit container image commit a new docker image
(snapshot of the container)
24 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Part 3.
Inputs/Outputs
• Data volumes (persistent data)
• mounted from the host filesystem
• named volumes (interal + volume plugins)
• Devices
• Links
• Publishing ports (NAT)
26 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
27 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
28 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Named pipe
$ mkfifo /tmp/fifo
$ docker run -d -v /tmp/fifo:/fifo debian sh -c 'echo blah blah> /fifo'
ff0e44c25e10d516ce947eae9168060ee25c2a906f62d63d9c26a154b6415939
$ cat /tmp/fifo
blah blah
Unix socket
$ docker run --rm -t -i -v /dev/log:/dev/log debian
root@56ec518d3d4e:/# logger blah blah blah
root@56ec518d3d4e:/# exit
$ sudo tail /var/log/messages | grep logger
Jan 21 08:07:59 halfoat logger: blah blah blah
29 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
7
https://docs.docker.com/engine/tutorials/dockervolumes/
30 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
8
since v1.9.0, links are superseded by user-defined networks
32 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Legacy links
deprecated feature: do not use!
33 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
35 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
35 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
35 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
35 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
35 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
35 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
36 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
publish example
37 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
publish example
bind to all host addresses
$ docker run -d -p 80:80 nginx
52c9105e1520980d49ed00ecf5f0ca694d177d77ac9d003b9c0b840db9a70d62
bind to 127.0.0.1
$ docker run -d -p 127.0.0.1:80:80 nginx
4541b43313b51d50c4dc2722e741df6364c5ff50ab81b828456ca55c829e732c
38 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
39 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
39 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Part 4.
Managing docker images
40 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Docker images
• immutable
• copy-on-write storage
• for instantiating containers
• for creating new versions of the image (multiple layers)
9
possibly multiple times
41 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
command description
docker images list all local images
docker history image show the image history
(list of ancestors)
docker inspect image. . . show low-level infos
(in json format)
docker tag image tag tag an image
docker commit container image create an image
(from a container)
docker import url|- [tag] create an image
(from a tarball)
docker rmi image. . . delete images
42 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
43 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Image tags
A docker tag is made of two parts: “REPOSITORY:TAG”
The TAG part identifies the version of the image. If not provided,
the default is “:latest”
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
debian 8 835c4d274060 2 weeks ago 122.6 MB
debian 8.0 835c4d274060 2 weeks ago 122.6 MB
debian jessie 835c4d274060 2 weeks ago 122.6 MB
debian rc-buggy 350a74df81b1 7 months ago 159.9 MB
debian experimental 36d6c9c7df4c 7 months ago 159.9 MB
debian 6.0.9 3b36e4176538 7 months ago 112.4 MB
debian squeeze 3b36e4176538 7 months ago 112.4 MB
debian wheezy 667250f9a437 7 months ago 115 MB
debian latest 667250f9a437 7 months ago 115 MB
debian 7.5 667250f9a437 7 months ago 115 MB
debian unstable 24a4621560e4 7 months ago 123.6 MB
debian testing 7f5d8ca9fdcf 7 months ago 121.8 MB
debian stable caa04aa09d69 7 months ago 115 MB
debian sid f3d4759f77a7 7 months ago 123.6 MB
debian 7.4 e565fbbc6033 9 months ago 115 MB
debian 7.3 b5fe16f2ccba 11 months ago 117.8 MB
45 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Local tags may have arbitrary names, however the docker push
and docker pull commands expect some conventions
46 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
47 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
10
https://github.com/a-ba/docker-utils/
48 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Transferring images
49 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Part 5.
Docker builder
50 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
51 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Build an image
docker build [ -t tag ] path
The command:
11
unwanted files may be excluded if they match patterns listed in
.dockerignore
52 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Dockerfile example
# base image: last debian release
FROM debian:wheezy
# install nginx
RUN apt-get -y install nginx
# Tell the docker engine that there will be somenthing listening on the tcp port 80
EXPOSE 80
53 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Dockerfile format
https://docs.docker.com/reference/builder/
54 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
55 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Builder cache
56 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
12
see also https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/
57 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Part 6.
Security
• host/container isolation
• container/container isolation
• other security considerations
58 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Security options
Container/Host isolation
Container/Container isolation
59 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
60 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
User namespaces
• useful for:
• preventing fs-based attacks (eg: root user inside the container
creates a setuid executable in an external volume)
• isolating docker users from each other (one docker daemon for
each user, with uids remapped to different ranges)
63 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Container/Container isolation
17
http://lwn.net/Articles/689453
64 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
65 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Part 7.
Docker Ecosystem
• infrastructure
• docker machine (provisioning)
• docker swarm (clustering)
• swarm mode (clustering)
• infrakit (automated self-healing infrastructure monitoring and
provisioning)
• container deployment & configuration
• docker compose
• image distribution
• docker distribution (registry)
• docker notary (content trust, image signing)
66 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Docker Machine
abstraction for provisionning and using docker hosts
67 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Docker Swarm
manage a cluster of hosts running docker
Docker Inc. folks are misleading: the name
swarm is actually used for two different products:
Docker Compose
configure and deploy a collection of containers
69 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Part 8.
The Future is Now
• swarm mode (since v1.12)
• plugins (since v1.13)
• experimental features
• Docker EE & time-based releases
70 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
71 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Experimental features
as of v1.1318
• service logs
https://github.com/docker/docker/blob/master/docs/reference/commandline/service_logs.md
18
https://sreeninet.wordpress.com/2017/01/27/docker-1-13-experimental-features/
72 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
2. sell Docker EE
73 / 74
Intro Containers I/O Images Builder Security Ecosystem Future
Time-based release
since march 2017 (docker v17.03.0-ce)
• Docker CE
• open source
• edge version released every month
• stable version released every 3 months
• security upgrades during 4 months
• Docker EE
• proprietary
• stable version released every 3 months
• security upgrades during 1 year
74 / 74