52
Docker in everyday development What every dev should know.

Docker in everyday development

Embed Size (px)

Citation preview

Docker in everyday development

What every dev should know.

Justyna Ilczuk,Senior Software / DevOps Engineer

at

Speaker

A scalable backend that helps developers build complex apps

with only front-end code.http://www.syncano.com/

Docker - An open platform for distributed applications for developers and sysadmins.

* Powered by Linux Containers.

Build, Ship and Run Any App, Anywhere

Why?

Why?

Portability rocks!App works on all machines (not only on mine!)

Why?

Isolate and version dependencies on both language and OS level

- better than virtualenv (rvm)

Why?

Build Dev Environment than resembles production environment

- avoid strange bugs from dev/prod mismatch

Why?

Installing dockerized services is trivial

- You need a Postgres in version 9.4? Or redis, or Rabbitmq, Elastic Search?

Just pull an image and start a container.

Still not convinced?

Docker pros

Lightweight● busybox - 2.433 MB● debian - 85.01 MB● nginx - 91.66 MB● redis - 111.2 MB

Docker pros

Unified build processDockerfile

FROM debian:latestRUN apt-get update && apt-get install tmuxCMD tmux

$ docker build -t my_image .

Docker pros

Docker image format, portability, hub$ docker pull organization/image_name$ docker run -it organization/image_name command$ docker push me/my_image

Docker pros

Fast, starting container in 0.2 s

$ time docker run --rm ubuntu /bin/echo hello worldhello worlddocker run --rm ubuntu /bin/echo hello world 0,02s user 0,01s system 9% cpu 0,271 total

Docker pros - summing up

● lightweight containers● unified build process (Dockerfile)● standardized images (Docker hub)● fast, starting container in 0.2 s● combining containers together● distributed apps, microservices

Docker cons

● works only on GNU/Linux (needs vagrant / boot2docker)

● new tool to learn● another layer of complexity & overhead -

vagrant + Docker vs developing natively on your machine

Docker + Vagrant

Containers on VMs

Docker + Vagrant

● Works everywhere● Possible to try new tech, new OSes

○ CoreOS○ Kubernetes○ Mesos○ etcd○ Consul

● Distributed apps on your laptop

https://www.flickr.com/photos/110382334@N05/11303036263

Tools

● Docker CLI● Docker compose (aka fig)● Good old make

Docker CLI - the most important command

Running containers$ docker run --rm ubuntu echo “Hello docker”$ docker run -d -p 8080:80 nginx$ docker run -it -v `pwd`:/app -p 8000:8000 my_app

Docker CLI - checking state

Checking state of containers$ docker ps$ docker images$ docker logs my_container$ docker top my_container

Docker CLI

https://docs.docker.com/reference/commandline/cli/$ docker --help$ man docker

37 commands (for everything and more)

https://www.flickr.com/photos/pasukaru76/9824401426/in/photolist-9DE7Wa-cd6Wxf-dLkpKN-bW4iP-6TfKoP-okpdNP-fY9Cgu-8SBNJB-7vgLWL-a5sj6k-3SUkNL-koVXTM-8vs4F6-EmbdC-6w5kAz-i9E5k-878gAY-e88nnZ-9dDY7N-4uNsCX-7iuh42-fMEpmV-pybGg-bF1XGL-6cyxFq-tauEV-7zWj8-eewWz-a24hkr-4EjNgb-o4yuXd-f4PmxD-9bCZC5-e69XkS-dgEs3y-4jYxpy-6Qfw98-aB4Vgf-4gm9N6-jySVXM-dVJKrK-cu4nm3-jN6RPH-8W5ooc-4pAwWe-8TpRWe-77os7A-5256km-3NdDg2-6KipNW

Containers everywhere

$ docker rmi$ docker rm

Time for a better tool

Docker Compose

Docker Compose

Compose is a tool for defining and running complex applications with Docker.

Docker Compose

● Define your containers in simple yaml file.● Compose container together.● Manage group of containers with single tool.

Demo of Docker Compose

Docker Compose - configdocker-compose.yml

redis: image: redis volumes: - /var/docker/redis:data ports: - "6379"web: build: . ports: - "80:8080" links: - redis:redis volumes: - .:/app environment: - VARIABLE=value

Docker Compose - config

redis: image: redis volumes: - /var/docker/redis:data ports:

- “6379”

Docker Compose - configweb: build: . ports: - "8090:8080" links: - redis:redis volumes: - .:/app environment: - VARIABLE=value

Docker Compose - commands

$ docker-compose build$ docker-compose up$ docker-compose ps

All is rainbows and unicorns now, right?

Compose - good for running containersBut missing dependencies.

Updating images● Developers Bob and Alice work on an App.● They use docker-compose to run App● Bob adds new feature that requires Dependency X to be

baked into Docker image● He updates the requirements.txt file that is used in

Dockerfile and rebuilds his image

Updating images● Cool feature is merged to trunk● Alice pulls changes● Alice starts docker-compose and app breaks, because

of missing dependency in the container

Technical of people problem?

Should Alice remember to run `docker-compose build`, `docker-compose rm` and `docker-compose up` after each pull?

Automate everything. Use smart technologies.

Use Make.

Good ol’ Makefilerun : build

docker-compose up -d

build : .built

.built : requirements.txt Dockerfiledocker-compose buildtouch .built

stop :docker-compose stop

clean : stopdocker-compose rmrm .built

test : .builtdocker-compose run web ./run_tests.sh ${ARGS}

.PHONY : run build clean stop test

Good ol’ Makefilerun : build

docker-compose up -d

build : .built

.built : requirements.txt Dockerfiledocker-compose buildtouch .built

Makefile

● Uses docker-compose● Best for dependencies● Self documenting

Other problems that you might encounter

Ups… something went wrong.

Building image failed.- it stops at layer that failed- starts from the last correct layer (cache)

Ups… something went wrong.

App in containers behaves in a strange way:Logs and errors- logging to stdout- logging to volumes- logging straight to Centralized Logging- logging to sentry

Ups… something went wrong.

https://github.com/slafs/sentry-docker <3

Ups… something went wrong.

Operating on a live patient -Debugging running containers

- no need for ssh- … no need for nsenter- $ docker exec -it my_container bash

Questions?@attilczuk