Docker Tutorial - Mars 2019
Docker Tutorial - Mars 2019
Docker Tutorial - Mars 2019
Docker Tutorial
Anthony Baire
This tutorial is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 France License
1 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Summary
1. Introduction
3. Inputs/Outputs
6. Security considerations
2 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Part 1.
Introduction
3 / 83
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/
4 / 83
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
5 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
• a build system
• images may be build from sources
• using a simple DSL (Dockerfile)
6 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
7 / 83
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
8 / 83
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)
9 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Part 2.
Managing containers
• create/start/stop/remove containers
• inspect containers
• interact, commit new images
10 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
11 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
2
send SIGTERM to the main process + SIGKILL 10 seconds later
3
-f allows removing running containers (= docker kill + docker rm)
12 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
13 / 83
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 14 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
15 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
16 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
17 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
user (-u)
$ docker run debian whoami
root
$ docker run -u nobody debian whoami
nobody
19 / 83
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
20 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
21 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Common rm idioms
Launch an throwaway container for debugging/testing purpose
$ docker run --rm -t -i debian
root@4b71c9a39326:/#
23 / 83
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)
25 / 83
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)
27 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
28 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
29 / 83
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
30 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
9
since v1.9.0, links are superseded by user-defined networks
33 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Legacy links
deprecated feature
34 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
36 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
36 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
36 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
36 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
36 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
36 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
37 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
publish example
38 / 83
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
39 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
40 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
40 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Part 4.
Managing docker images
41 / 83
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)
10
possibly multiple times
42 / 83
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
43 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
44 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
45 / 83
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
46 / 83
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
47 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
48 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
11
https://github.com/a-ba/docker-utils/
49 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Transferring images
50 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Part 5.
Docker builder
51 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
52 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Build an image
docker build [ -t tag ] path
The command:
12
unwanted files may be excluded if they match patterns listed in
.dockerignore
53 / 83
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
54 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Dockerfile format
https://docs.docker.com/reference/builder/
55 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
# shell form
RUN apt-get update # equivalent to: RUN [”/bin/sh”, ”−c”, ”apt−get update”]
56 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
instruction description
ARG name[=value] build-time variables
ON BUILD instruction instruction run when building
a derived image
58 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Builder cache
59 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
15
see also https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/
60 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
# install the files in a tmp dir and make an archive that we can deploy elsewhere
RUN cd /opt/src && make install DESTDIR=/tmp/dst \
&& cd /tmp/dst && tar czvf /tmp/myapp.tgz .
CMD ["myapp"]
61 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Part 6.
Security
• host/container isolation
• container/container isolation
• other security considerations
62 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Security strategies
Docker containers are not really sandboxed from the host machine.
They talk with the same kernel. You may want to consider
strategies to reduce the risks of privilege escalation.
Container/Host isolation
Container/Container isolation
64 / 83
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)
67 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
CVE-2019-5736
runc through 1.0-rc6, as used in Docker before 18.09.2 and other products, allows
attackers to overwrite the host runc binary (and consequently obtain host root access)
by leveraging the ability to execute a command as root within one of these types of
containers: (1) a new container with an attacker-controlled image, or (2) an existing
container, to which the attacker previously had write access, that can be attached
with docker exec. This occurs because of file-descriptor mishandling, related to
/proc/self/exe.
68 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
69 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Container/Container isolation
20
http://lwn.net/Articles/689453
70 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
71 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Part 7.
Docker Ecosystem
• infrastructure
• docker machine (provisioning)
• docker swarm (clustering)
• swarm mode (clustering)
• underlying projects (moby, containerd, infrakit, ...)
• container deployment & configuration
• docker compose
• image distribution
• docker distribution (registry)
• docker notary (content trust, image signing)
72 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Docker Machine
abstraction for provisionning and using docker hosts
73 / 83
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
75 / 83
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
• The Orchestration Wars
76 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
77 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
2. sell Docker EE
78 / 83
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
79 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
• under the hood the base building blocs (runc, containerd) are
open and the competitors cooperate to keep them standard.
80 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Apache Mesos
• predates Docker
• hard to configure
81 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
Kubernetes (k8s)
82 / 83
Intro Containers I/O Images Builder Security Ecosystem Future
83 / 83