Docker Tutorial PDF
Docker Tutorial PDF
Docker Tutorial
Anthony Baire
Universit
e de Rennes 1 / UMR IRISA
This tutorial is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 France License
1 / 67
Intro Containers I/O Images Builder Orchestration Security
Part 1.
Introduction
2 / 67
Intro Containers I/O Images Builder Orchestration Security
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 / 67
Intro Containers I/O Images Builder Orchestration Security
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 / 67
Intro Containers I/O Images Builder Orchestration Security
a build system
images may be build from sources
using a simple DSL (Dockerfile)
5 / 67
Intro Containers I/O Images Builder Orchestration Security
6 / 67
Intro Containers I/O Images Builder Orchestration Security
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 containers 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 / 67
Intro Containers I/O Images Builder Orchestration Security
Installation
https://docs.docker.com/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 / 67
Intro Containers I/O Images Builder Orchestration Security
Part 2.
Managing containers
create/start/stop/remove containers
inspect containers
interact, commit new images
9 / 67
Intro Containers I/O Images Builder Orchestration Security
10 / 67
Intro Containers I/O Images Builder Orchestration Security
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 / 67
Intro Containers I/O Images Builder Orchestration Security
4
or run
12 / 67
Intro Containers I/O Images Builder Orchestration Security
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 / 67
Intro Containers I/O Images Builder Orchestration Security
14 / 67
Intro Containers I/O Images Builder Orchestration Security
15 / 67
Intro Containers I/O Images Builder Orchestration Security
16 / 67
Intro Containers I/O Images Builder Orchestration Security
user (-u)
$ docker run debian whoami
root
$ docker run -u nobody debian whoami
nobody
18 / 67
Intro Containers I/O Images Builder Orchestration Security
hostname (-h)
$ docker run debian hostname
830e47237187
$ docker run -h my-nice-container debian hostname
my-nice-hostname
19 / 67
Intro Containers I/O Images Builder Orchestration Security
20 / 67
Intro Containers I/O Images Builder Orchestration Security
Common rm idioms
22 / 67
Intro Containers I/O Images Builder Orchestration Security
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 (`
a la tail -f)
23 / 67
Intro Containers I/O Images Builder Orchestration Security
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 / 67
Intro Containers I/O Images Builder Orchestration Security
Part 3.
Inputs/Outputs
External volumes (persistent data)
Devices
Links
Publishing ports (NAT)
26 / 67
Intro Containers I/O Images Builder Orchestration Security
27 / 67
Intro Containers I/O Images Builder Orchestration Security
28 / 67
Intro Containers I/O Images Builder Orchestration Security
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 / 67
Intro Containers I/O Images Builder Orchestration Security
7
since v1.9.0, links are superseded by user-defined networks
31 / 67
Intro Containers I/O Images Builder Orchestration Security
Link Example
32 / 67
Intro Containers I/O Images Builder Orchestration Security
34 / 67
Intro Containers I/O Images Builder Orchestration Security
34 / 67
Intro Containers I/O Images Builder Orchestration Security
34 / 67
Intro Containers I/O Images Builder Orchestration Security
34 / 67
Intro Containers I/O Images Builder Orchestration Security
34 / 67
Intro Containers I/O Images Builder Orchestration Security
34 / 67
Intro Containers I/O Images Builder Orchestration Security
35 / 67
Intro Containers I/O Images Builder Orchestration Security
publish example
36 / 67
Intro Containers I/O Images Builder Orchestration Security
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
37 / 67
Intro Containers I/O Images Builder Orchestration Security
Part 4.
Managing docker images
38 / 67
Intro Containers I/O Images Builder Orchestration Security
Docker images
immutable
copy-on-write storage
for instantiating containers
for creating new versions of the image (multiple layers)
8
possibly multiple times
39 / 67
Intro Containers I/O Images Builder Orchestration Security
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
40 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
41 / 67
Intro Containers I/O Images Builder Orchestration Security
42 / 67
Intro Containers I/O Images Builder Orchestration Security
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
43 / 67
Intro Containers I/O Images Builder Orchestration Security
Local tags may have arbitrary names, however the docker push
and docker pull commands expect some conventions
44 / 67
Intro Containers I/O Images Builder Orchestration Security
45 / 67
Intro Containers I/O Images Builder Orchestration Security
9
https://github.com/a-ba/docker-utils/
46 / 67
Intro Containers I/O Images Builder Orchestration Security
Transferring images
47 / 67
Intro Containers I/O Images Builder Orchestration Security
Part 5.
Docker builder
48 / 67
Intro Containers I/O Images Builder Orchestration Security
49 / 67
Intro Containers I/O Images Builder Orchestration Security
Build an image
docker build [ -t tag ] path
The command:
10
unwanted files may be excluded if they match patterns listed in
.dockerignore
50 / 67
Intro Containers I/O Images Builder Orchestration Security
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
51 / 67
Intro Containers I/O Images Builder Orchestration Security
Dockerfile format
https://docs.docker.com/reference/builder/
52 / 67
Intro Containers I/O Images Builder Orchestration Security
53 / 67
Intro Containers I/O Images Builder Orchestration Security
Builder cache
54 / 67
Intro Containers I/O Images Builder Orchestration Security
Part 6.
Orchestration
Docker Machine
Docker Swarm
Docker Compose
other tools
55 / 67
Intro Containers I/O Images Builder Orchestration Security
Docker Machine
abstraction for provisionning and using docker hosts
56 / 67
Intro Containers I/O Images Builder Orchestration Security
Docker Swarm
manage a cluster of docker hosts
57 / 67
Intro Containers I/O Images Builder Orchestration Security
Docker Compose
manage a collection of containers
58 / 67
Intro Containers I/O Images Builder Orchestration Security
Other tools
Shipyard
Kubernetes (Google)
Fleet (CoreOS)
59 / 67
Intro Containers I/O Images Builder Orchestration Security
Part 7.
Security
host/container isolation
container/container isolation
other security considerations
60 / 67
Intro Containers I/O Images Builder Orchestration Security
Security options
Container/Host isolation
Container/Container isolation
61 / 67
Intro Containers I/O Images Builder Orchestration Security
62 / 67
Intro Containers I/O Images Builder Orchestration Security
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)
65 / 67
Intro Containers I/O Images Builder Orchestration Security
Container/Container isolation
66 / 67
Intro Containers I/O Images Builder Orchestration Security
67 / 67