Adventures in docker compose

Post on 13-Apr-2017

227 views 0 download

Transcript of Adventures in docker compose

Adventures in docker-compose

with Node.js

Whoami

Giovanni Lela

CTO@LinkMe

cofounder@MeanMilan

@lambrojos

http://github.com/lambrojos

Things no developer/architect/CTO likes in during development

● recognizing wrong choices when it is too late● mantaining different development

environments● targeting that VERY specific version

of something● spending hours on configuring the

dev environment

Fighting back

● Create approximation of systems and architectures

● Isolate yourself from the host● Infrastructure as code● Automate like there is no to tomorrow

Isolated work environments and teamwork

● If you are working on a backend application front end people can avoid writing tons of mocks or use a shared staging environment

● In case of error the application state can be debugged easily

Vagrant

Vagrant made this approach popular by virtualizing machines on real hypervisors,● Very accurate replicas● Performance issues when running

complex architectures● Provisioning looks intense

Docker

● You don’t have virtual machines

● The OS kernel is shared in virtualized environment called containers.

● The virtualized processes are essentially native, do not require the overhead of virtual machines and still provide a high level of isolation from the host OS

Docker in a nutshell

Some docker terms

● Image: a readonly template for creating containers - they are defined with simple files called Dockerfiles

● Container: A Docker container is a runnable instance of a Docker image

Some more docker terms

● Registry: a repository of docker images

● Volumes: (aka data volumes): folders that bypass docker’s union filesystem. We can use them to map folders from the host machine to the container

Containers and layers

Example Dockerfile

A dockerfile is like a recipe to build an imageEach line corresponds to a new layer in the image.

FROM node:7

RUN npm install -g knex nodemon mocha istanbul node-gyp snyk jsdoc

WORKDIR /src

Docker CLI is semi hard

Docker CLI can have quite a lot of optionsSetting up a serious multi container environment can be very hard

docker run -d -v ~/nginxlogs:/var/log/nginx -p 5000:80 -i nginx -- entrypoint /bin/bash example/nginx

Enter docker compose

Compose is a tool for defining and running multi-container Docker applications.

Enter docker-compose

Docker compose helps by defining and coordinating multiple containers.

With Compose, you define a multi-container application in a .yml single file, then spin your application up in a single command which does everything that needs to be done to get it running.

Example (docker-compose.yml)

mysql: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: passwordsarecool MYSQL_DATABASE: node volumes: - ./conf/mysql/conf.d:/etc/mysql/conf.d ports: - "3306"

web: build: . ports: - "80" - "443" - "22" volumes: - ./:/src links: - mysql

Key points

● We are not provisioning a set of machines, we are defining a set of services

● Then isolating them in containers● And then choosing how they interact

Cool things

Services and middlewares are super easy to install: as they can be installed right from the docker registry, the version we want, with the image

Basic configuration is achieved with env vars, advanced configuration by mapping volumes

Approximating the production environment

● a good approximation of the production environment is the key for true happiness

● integration tests can be run on local machines for cheap - spot architectural issues very very early

Even more goodies

Continous integration is super easy to setup: at its most basic form it can be reduced to something like:

docker-compose run myservice npm test

Testing in docker-compose

Allows you test against real services not mocks- all tests can be integration tests.No CI provider lock-in (as long docker compose is supported)Lightweight - you can spin up a lot services and simulate complex architectures

Production?

Can be also used to setup a quick and dirty staging environment.

Just use the -d flag

Docker swarm?

Issues and not so cool things

It’s all fun and games as long as you work on linux

● On mac and windows it used to run on a virtual machine

● Now it’s better because it runs on hyper-V on windows and xhyve on MAC (which is still a virtualization)

Issues

On mac and windows installing and updating docker has been more time consuming than expected

The first docker-compose up run is always quite painful

SSH keys

What if you need your ssh credentials inside your container? I learned to hate:

● ssh passphrases● windows permissions

Other nasty stuff

/r/n

docker updates

mac os filesystem case sensitieves

Code time