+ - 0:00:00
Notes for current slide
Notes for next slide

@mariolet                                                     github.com/l0rd

periodic

Containers Patterns

There are a thousand ways to use containers

Who am I?

mario

@mariolet                                                     github.com/l0rd

Containers usages

Everybody use containers

dupond1

There are many ways to use containers

dupond2

Containers Patterns Catalog

Development Patterns

  • Copy Sources
  • Mount Sources
  • Dockerize Your Tools
  • ONBUILD Image
  • Dependencies First Dockerfile
  • Source 2 Image

Distribution Patterns

  • Build From Scratch
  • Containers Launcher

Runtime Patterns

  • Mount Sources
  • Docker Socket Mount
  • Containers Launcher
  • Build From Scratch
  • Host Spoofing
  • ENTRYPOINT and CMD combined
  • Exec Form ENTRYPOINT
  • Source 2 Image
  • Sidecar Container
  • Ambassador Container

@mariolet                                                     github.com/l0rd

Containers Patterns in Practice

periodic

periodic

@mariolet                                                     github.com/l0rd

Mount Sources

MS

Example

Mount 2048

Docker Image
2048

Source code
https://github/l0rd/containerspatterns/MS/

Run command

docker run -p 8080:80 \
-v $(pwd):/usr/local/apache2/htdocs/ \
httpd

MS

Example

Details

Mount Sources

cp-MS

MS

Example

Details

Usages

Mount Sources

  • Development and Runtime Pattern
  • Source folder is bind mounted when running the container
  • Pattern particularly suited for dynamic languages
  • Not recommended for production
  • No need to rebuild or restart container when sources are updated
  • Build tools are included in the image
  • The same image can be used to compile and run the application

@mariolet                                                     github.com/l0rd

Dockerize Your Tools

MS

DYT

Example

Dockerize maven

Docker Image
maven:3.3.3-jdk-8

Source code
https://github/l0rd/containerspatterns/DYT

Run command

# Make the alias of the dockerized tool
alias mvn="docker run \
-v $(pwd):/usr/src \
-v ~/.m2:/root/.m2 \
-w /usr/src \
maven:3.3.3-jdk-8 \
mvn"
# Run the tool
mvn -version

MS

DYT

Example

Details

Dockerize Your Tools

cp-DYT

MS

DYT

Example

Details

Usages

Dockerize Your Tools

  • Development pattern
  • A tool is packaged and distributed as a Docker image
  • Allow to run multiple versions of the same tool
  • The tool version and installation is described in a Dockerfile
  • Files can be shared between the container and the host with volumes
  • alias command can be used to make it easier to run

@mariolet                                                     github.com/l0rd

Containers Launcher

MS

DYT

CL

Example

Start an app and its DB with a single docker run

Docker Image
containerslanguages/rust-launcher

Source code
https://github/l0rd/containerspatterns/CL

Run command

docker run -v $(pwd):/src/ \
-v /var/run/docker.sock:/var/run/docker.sock \
containerslanguages/rust-launcher

MS

DYT

CL

Example

Details

Containers Launcher

cp-CL-1

MS

DYT

CL

Example

Details

Containers Launcher



cp-CL-2

MS

DYT

CL

Example

Details

Containers Launcher



cp-CL-3

MS

DYT

CL

Example

Details

Usages

Containers Launcher

  • Runtime Pattern
  • The Docker socket is bind mounted when the container is started
  • Use to compose multiple containers without Docker compose or similar

@mariolet                                                     github.com/l0rd

Host Spoofing

DYT

CL

HS

Example

Get host info from a container

Docker Image
alpine

Source code
https://github/l0rd/containerspatterns/HS

Run command

docker run --net=host \
-v /:/hostfs/ \
--pid=host \
--uts=host \
--ipc=host \
-v $(pwd):/src/ \
alpine sh -c ". /src/print_host_info.sh"

DYT

CL

HS

Example

Details

Host Spoofing



cp-HS

DYT

CL

HS

Example

Details

Usages

Host Spoofing

  • Runtime Pattern
  • Run commands inside a container to inspect or alter the Docker host
  • Access to host network, filesystem, processes, users etc...
  • Break container isolation
  • Won't work when security hardening the Docker install

@mariolet                                                     github.com/l0rd

Source To Image

CL

HS

S2I

Example

Multi-stage build to package a Java App

Docker Images

  • maven:3.5-jdk-8 (build)
  • openjdk:8-jre (run)

Source code
https://github/l0rd/containerspatterns/S2I

Build command

docker build -t s2i .

Run command

docker run -t --rm s2i

CL

HS

S2I

Example

Details

Source To Image

cp-S2I

CL

HS

S2I

Example

Details

Usages

Source To Image

  • One unique Dockerfile for build and run
  • Build tools are not in the final image
  • Existed since a long time in OpenShift, recently integrated in Docker (17.05)
  • Combine 2 patterns (Copy Source and Copy Executable)
  • Suited for static programming languages
  • Allow to use the Docker Hub as a CI platform

@mariolet                                                     github.com/l0rd

Sidecar Container

HS

S2I

SC

Example

SC for PID and FS

Docker Images httpd and ubuntu

Source code
https://github/l0rd/containerspatterns/SC

Run command

# Run apache httpd in the background
cid=$(docker run -dit -p 8080:80 \
-v /usr/local/apache2/htdocs/ httpd:2.4)
# Run a sidecar container that updates index.html
docker run --volumes-from ${cid} -ti --rm ubuntu \
sh -c "echo I am the sidecar >> /usr/local/apache2/htdocs/index.html"
# Run a sidecar container that shares the same PID namespace
docker run --pid=container:${cid} -ti --rm ubuntu \
bash -c "echo -n pid 1 is \$(ps -p 1 -o comm=), killing it...;
kill 1;
echo done."

HS

S2I

SC

Example

Details

Sidecar Container

cp-CS

HS

S2I

SC

Example

Details

Usages

Sidecar Container

  • Provide extra functionalities to a running container
  • Popular for serverless architectures
  • Not all namespace are sharable (e.g. userns/uts/filesystem)

@mariolet                                                     github.com/l0rd

Conclusion

periodic

periodic

@mariolet                                                     github.com/l0rd

Copy Sources

A development pattern

@hguemar, @mariolet, @mjbright

CS

Pattern

cp-CS

@hguemar, @mariolet, @mjbright

CS

Pattern

Copy Sources Docker

  • development pattern
  • Sources are copied inside the image
  • Simplest development pattern
  • A new image should be built for every code change
  • Build tools are included in the image
  • Usually a different image is used to run the application

@hguemar, @mariolet, @mjbright

CS

Pattern

Example

Copy 2048 Docker

Docker Image
2048

Source code
https://github/l0rd/containerspatterns/CS/

Build and run commands

docker build -t 2048 .
docker run -d -p 8080:80 2048

In this pattern it's particularly important to separate in 2 distinct steps:

  • fetching the dependencies
  • build of the application

@mariolet                                                     github.com/l0rd

Dockerize Your Tools

A development pattern

@hguemar, @mariolet, @mjbright

CS

MS

DYT

Pattern

cp-DYT

@hguemar, @mariolet, @mjbright

CS

MS

DYT

Pattern

Dockerize Your Tools

  • Development pattern
  • A tool is packaged and distributed as a Docker image
  • Allow to run multiple versions of the same tool
  • The tool version and installation is described in a Dockerfile
  • Files can be shared between the container and the host with volumes
  • alias command can be used to make it easier to run

@hguemar, @mariolet, @mjbright

CS

MS

DYT

Pattern

Example

Dockerize maven

Docker Image
maven:3.3.3-jdk-8

Source code
https://github/l0rd/containerspatterns/DYT

Run command

# Make the alias of the dockerized tool
alias mvn="docker run \
-v $(pwd):/usr/src \
-v ~/.m2:/root/.m2 \
-w /usr/src \
maven:3.3.3-jdk-8 \
mvn"
# Run the tool
mvn -version

@mariolet                                                     github.com/l0rd

Docker Socket Mount

A runtime pattern

@hguemar, @mariolet, @mjbright

CS

MS

DYT

DSM

Pattern



cp-DSM

@hguemar, @mariolet, @mjbright

CS

MS

DYT

DSM

Pattern

Docker Socket Mount

  • Runtime Pattern
  • The Docker socket is bind mounted when the container is started
  • Allow to manage containers from another container
  • Usages:
    • Container monitoring tools
    • CI/CD tools
    • Containers launchers

@hguemar, @mariolet, @mjbright

CS

MS

DYT

DSM

Pattern

Example

Docker Socket Mount

Docker Image
containerslanguages/golang

Source code
https://github/l0rd/containerspatterns/DSM

Run command

docker run -v /var/run/docker.sock:/var/run/docker.sock \
containerslanguages/golang

@mariolet                                                     github.com/l0rd

Build From Scratch

A Distribution and runtime pattern

@hguemar, @mariolet, @mjbright

...

DSM

CL

BFS

Pattern



cp-BFS

@hguemar, @mariolet, @mjbright

...

DSM

CL

BFS

Pattern

Build From Scratch

  • Distribution and Runtime Pattern
  • The base image is the smallest possible: Scratch
  • Use to make ridiculously small images
  • Works well with statically linked applications (Go, Rust, C etc...)

@hguemar, @mariolet, @mjbright

...

DSM

CL

BFS

Pattern

Example

Go HTTP server built from scratch Docker Docker

Docker Image
emilevauge/tictac

Source code
https://github/emilevauge/tictac/

Run command

docker build -t tictac .
# Compare tictac binary size with tictac docker image size

@mariolet                                                     github.com/l0rd

ENTRYPOINT and CMD Combined

A runtime pattern

@hguemar, @mariolet, @mjbright

...

BFS

HS

ECC

Pattern



cp-ECC

@hguemar, @mariolet, @mjbright

...

BFS

HS

ECC

Pattern

ENTRYPOINT and CMD Combined Docker

  • Runtime Pattern
  • Instructions ENTRYPOINT and CMD are used together
  • ENTRYPOINT is the fixed part of the command
  • CMD is the variable part (usually the parameters)

@hguemar, @mariolet, @mjbright

...

BFS

HS

ECC

Pattern

Example

Asciiart generator with ENTRYPOINT and CMD Docker

Docker Image
ecc

Source code
https://github/l0rd/containerspatterns/ECC

Run command

docker run -ti --rm ecc
docker run -ti --rm ecc -f lean docker

@mariolet                                                     github.com/l0rd

Exec Form ENTRYPOINT

A runtime pattern

@hguemar, @mariolet, @mjbright

...

BFS

HS

EFE

Pattern



cp-EFE

@hguemar, @mariolet, @mjbright

...

BFS

HS

EFE

Pattern

Exec Form ENTRYPOINT Docker

  • Runtime Pattern
  • JSON is used to define the command and its parameters
  • It's the alternative to the Shell Form (/bin/sh -c on Linux or cmd /S /C on Windows)
  • No varialbe substitution and the command is PID 1
  • Unix signals are notified directly to the program (not the shell)

@hguemar, @mariolet, @mjbright

...

BFS

HS

EFE

Pattern

Example

Exec and Shell form compared Docker

Docker Image
httpd

Source code
https://github/l0rd/containerspatterns/EFE/

Build and Run commands

docker build -t httpd-exec -f Dockerfile.exec .
docker build -t httpd-shell -f Dockerfile.shell .
docker run -i -P --rm httpd-exec
# Stop it using ^C
docker run -i -P --rm httpd-shell
# (Try to) stop it using ^C

@mariolet                                                     github.com/l0rd

ONBUILD Images

A development pattern

@hguemar, @mariolet, @mjbright

...

HS

EFE

OBI

Pattern



cp-OBI

@hguemar, @mariolet, @mjbright

...

HS

EFE

OBI

Pattern

ONBUILD Images Docker

  • Development pattern
  • Build behaviour inherited from base image
  • Avoid duplicate code in Dockerfiles
  • Can make Dockerfile difficult to read

@hguemar, @mariolet, @mjbright

...

HS

EFE

OBI

Pattern

Example

Build a Java app with ONBUILD Docker

Docker Image
obi-java

Source code
https://github/l0rd/containerspatterns/OBI

Build/Run commands

docker build -t obi-java .
docker run --rm obi-java

@mariolet                                                     github.com/l0rd

Dependencies First Dockerfile

A development pattern

@hguemar, @mariolet, @mjbright

...

HS

EFE

DFD

Pattern

cp-DFD

@hguemar, @mariolet, @mjbright

...

HS

EFE

DFD

Pattern

Dependencies First Dockerfile Docker

  • Development pattern
  • Dependencies should not be fetched at every change in source code
  • In Dockerfile dependency list should be copied before source code

@hguemar, @mariolet, @mjbright

...

HS

EFE

DFD

Pattern

Example

Packaging a python application with Docker Docker

Docker Image dfd

Source code
https://github/l0rd/containerspatterns/dfd
https://github/polyfunc/flask-todolist

Build command

docker build -t dfd .
docker build -t dfd-orig -f Dockerfile.orig .
touch onefile
docker build -t dfd .
docker build -t dfd-orig -f Dockerfile.orig .

Who am I?

mario

Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow