No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick...

45
No Docker Required Tools to Build Container Images Patrick Harböck and Martin Höfling June, 7 th 2019 (Big Techday) 1

Transcript of No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick...

Page 1: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

No Docker Required

Tools to Build Container Images

Patrick Harböck and Martin HöflingJune, 7th 2019 (Big Techday)

1

Page 2: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Who is using Docker? In Development? In Production?

2

Page 3: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

What is Docker?

/usr/bin/python/usr/lib/python/site-packages/flask/myapp

Container Image Builder Container Runtime

3

Page 4: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Docker Images

4

Page 5: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Container Images● Manifest / Metadata

○ Default configuration for creating containers

○ Content hashes of layers to ensure integrity● Layers

○ File system packed with tar

○ Multiple layers → root file system for containers

5

Layer1: 7d97e98f8af71Layer2: e703abc8f639e

ENV | WORKDIR | USER | CMD

Page 6: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Container Image Format Evolution

6

2013 2014 2015 2016 2017

Image Spec v1 Registry v2 Image Spec v1.2

OCI Image Spec v1

Page 7: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

OpenContainerInitiative

7

Page 8: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

DEMO: Build aContainer Imagefrom Scratch

8

Page 9: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

DEMO: Build a Container Image from Scratch

9

main.go layer.tar config.json docker-image.tar

docker load&&

docker run

Page 10: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

10

Page 11: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

DEMO: Build a Container Image from Scratch

11

main.go layer.tar config.json docker-image.tar

docker load&&

docker run

● No elevated privileges required

● No Dockerfile

● No docker build

Page 12: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

What’s wrong with building images via Docker?

12

Page 13: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

è Securityè Scalability

è Flexibility13

Page 14: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

è Securityè Scalability

è Flexibility14

Page 15: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

How Docker builds container images

15

Filesystem Layer

RUN apt install python-flask

RUN python setup.py

Page 16: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

How Docker builds container images

● docker build uses Docker containers● Docker containers require isolation● Docker requires elevated privileges● Build pipelines / developers can access Docker

è Security nightmare on shared infrastructure

“First of all, only trusted users should be allowed tocontrol your Docker daemon.”https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface

16

Page 17: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

DEMO: Host Access via privileged container

Host (Minikube VM)

Docker Daemon

Docker-in-Docker Daemon(as Kubernetes Pod)

17

Page 18: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Docker in Docker Kubernetes Pod SpecapiVersion: v1kind: Podmetadata:

name: dindspec:

hostname: dind-podcontainers:

- name: dindimage: docker:dindsecurityContext:

privileged: Trueports:

- containerPort: 237518

Page 19: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

19

Page 20: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

DEMO: Host Access via privileged container

Host

Docker Daemon

Docker-in-Docker Daemon

20

Page 21: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Security Risks?

● Privileged Docker-in-Docker → full host access

● Mounting or exposing Docker socket → full host access

● Base image runs as container root → larger vulnerability surface

→ Easy to break and lose container isolation

21

Page 22: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Remark: Hermetic Builds and Reproducibility

→ Hermetic: sandboxed build process

→ Reproducible builds result in verifiable artifacts

22

Page 23: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

è Securityè Scalability

è Flexibility23

Page 24: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Caching

● Allows scaling up CI/CD pipelines

● Reuse base layers across different branches and builds

● Reproducible builds improve caching

24

Page 25: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Build Pipeline

25

Github• source code

repository

docker build• docker-in-docker• privileged pod

Docker Registry• pull cache• push images

Scalability Issues● One Docker daemon does not scale for parallel builds

● No distributed caching support

Page 26: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

è Securityè Scalability

è Flexibility26

Page 27: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Flexibility

● How restricted is the build process and image definition?

○ Can developers use any tools and languages they want?

○ How well does it integrate into an existing development pipeline?

27

Source Code ??? Container Image

Page 28: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Dockerfile based Tools● Extract base layer(s)● Run a command in sub container or directly● Snapshot Filesystem

ü Generic- Problem: Supported Dockerfile Features?

- USER – run commands as specific user

- Multistage builds

- Root vs. non-root

FROM ubuntu:18.04

RUN apt-get updateRUN apt-get install –y nginx

COPY nginx.conf /etc/nginx/

EXPOSE 8080

ENTRYPOINT [‘/usr/bin/nginx’]

28

Page 29: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Tool PrimaryMaintainer Security Scalability Flexibility

docker build Docker

Page 30: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

● Focus on scalability,

performance, extensibility

● Experimental support

in newer Docker versions

● Optional rootless mode

30

Tool PrimaryMaintainer Security Scalability Flexibility

BuildKit Docker Dockerfile

Page 31: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

● Secure and flexible

builds of OCI images

● Intended as a

Docker replacement

together with Podman

31

Tool PrimaryMaintainer Security Scalability Flexibility

Buildah Red Hat Dockerfile

Page 32: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

● Designed for Kubernetes

● Compatible with

○ AppArmor / SELinux

○ gVisor

● Focus on security

and performance

● Reproducible builds

32

Tool PrimaryMaintainer Security Scalability Flexibility

Kaniko Google Dockerfile

Page 33: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

● Focus on security

and performance

● Dockerfile support with

opinionated modifications

● Distributed caching of layers

33

Tool PrimaryMaintainer Security Scalability Flexibility

Makisu Uber Dockerfile

Page 34: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Tailored image construction

● Tailored for a distinct language and build-system

● The actual build is not performed in a (child) container● The build result is often combined with a base image

○ e.g. Python interpreter + virtualenv + application

No arbitrary command

execution requiredLimited flexibility

34

Page 35: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

● Maven / Gradle plugin● Distroless Java base image

Builds are

ü Minimal

ü Reproducible

ü Fast (caching)

35

Tool PrimaryMaintainer Security Scalability Flexibility

Jib Google Java only

Page 36: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

● Supports Python, Node.js,Java, C/C++, Go, Rust, …

Builds are

ü Minimal

ü Reproducible

ü Fast (caching)

- Complex rules written in Starlark36

Tool PrimaryMaintainer Security Scalability Flexibility

Bazel Google Starlark rules

Page 37: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

37

Tool PrimaryMaintainer Security Scalability Flexibility

OpenShiftSource-to-Image Red Hat Common stacks

Page 38: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

38

Tool PrimaryMaintainer Security Scalability Flexibility

Cloud Native Buildpacks Heroku / Pivotal / CNCF Common stacks

Page 39: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Tool PrimaryMaintainer Security Scalability Flexibility

docker build Docker Dockerfile

BuildKit Docker Dockerfile

Buildah Red Hat Dockerfile

Kaniko Google Dockerfile

Makisu Uber Dockerfile

Jib Google Java only

Bazel Google Starlark rules

OpenShift Source-to-Image Red Hat Common stacks

Cloud Native Buildpacks Heroku / Pivotal / CNCF Common stacks

Page 40: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

What shouldI use now?

40

Page 41: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Use case: Small Team

● No strict security requirements for team isolation

● Teams have full access to CI infrastructure

è DockerStill a valid choice

è BuildahFlexible, only parts Dockerfile syntax supported securely

è BuildKitAre you feeling adventurous? Potential transition path for Docker

41

Page 42: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Use case: Multiple teams, Provided K8s infrastructure● Cannot modify K8s infrastructure, no privileged containers, no container nesting● Teams are isolated, e.g. on namespace level

è Kaniko, e.g. combined with Skaffold or Knative

■ Shared volume caching(e.g. on Google Cloud Platform)

è Makisu: with Knative

■ Fine grained cache control

42

Page 43: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Use case: No Dockerfile required

e.g. Java only, Container Native Team

è Bazel

è Jib

è Cloud Native Buildpacks

43

No classical ops pattern!

apt-get install python-dev

Page 44: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Docker-lessInfrastructure?

https://phippy.io 44

Page 45: No Docker Required - TNG€¦ · No Docker Required Tools to Build Container Images Patrick Harböckand Martin Höfling June, 7th2019 (Big Techday) 1. ... Build pipelines / developers

Re-evaluate your container build process!

Martin Höfling / Patrick Harböck, TNG45