Adventures in docker compose

27
Adventures in docker-compose with Node.js

Transcript of Adventures in docker compose

Page 1: Adventures in docker compose

Adventures in docker-compose

with Node.js

Page 2: Adventures in docker compose

Whoami

Giovanni Lela

CTO@LinkMe

cofounder@MeanMilan

@lambrojos

http://github.com/lambrojos

Page 3: Adventures in docker compose

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

Page 4: Adventures in docker compose

Fighting back

● Create approximation of systems and architectures

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

Page 5: Adventures in docker compose

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

Page 6: Adventures in docker compose

Vagrant

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

complex architectures● Provisioning looks intense

Page 7: Adventures in docker compose

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

Page 8: Adventures in docker compose

Docker in a nutshell

Page 9: Adventures in docker compose

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

Page 10: Adventures in docker compose

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

Page 11: Adventures in docker compose

Containers and layers

Page 12: Adventures in docker compose

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

Page 13: Adventures in docker compose

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

Page 14: Adventures in docker compose

Enter docker compose

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

Page 15: Adventures in docker compose

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.

Page 16: Adventures in docker compose

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

Page 17: Adventures in docker compose

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

Page 18: Adventures in docker compose

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

Page 19: Adventures in docker compose

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

Page 20: Adventures in docker compose

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

Page 21: Adventures in docker compose

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

Page 22: Adventures in docker compose

Production?

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

Just use the -d flag

Docker swarm?

Page 23: Adventures in docker compose

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)

Page 24: Adventures in docker compose

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

Page 25: Adventures in docker compose

SSH keys

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

● ssh passphrases● windows permissions

Page 26: Adventures in docker compose

Other nasty stuff

/r/n

docker updates

mac os filesystem case sensitieves

Page 27: Adventures in docker compose

Code time