Docker dotcloud PaaS Français Mars 2013 OpenSource Ecrit en GO
Vocabulaire : Image vs Container Container = 1 instance d une Image. IMAGE to CONTAINER => run CONTAINER to IMAGE => build
Vocabulaire : Local vs Repo Tout fonctionne comme GIT (commit, push, pull) - REPO IMAGE to LOCAL IMAGE => pull - CONTAINER to LOCAL IMAGE => commit - LOCAL IMAGE -> REPO IMAGE => push Tout à un ID Tout à une version (on commit la différence)
Container VS Virtual Machine Docker VS VMWare, VirtualBox,.. Performance (pas de virtualisation) Isolation (namespace, cgroup) OS + Application (build complet) Plus simple que LXT (linux container), Libvirt
Avantage user Time from dev to prod (image, container) Size (diff image Git, Debian 89Mb!) Mount shared folder (host-container) Network (NAT) Communication inter-container (security)
Install sur MAC download virtualbox vagrant boot2docker boot2docker ip l ip de la vm (nécessaire pour PAT) docker version sanity check boot2docker init crée une VM (login docker, tcuser ) boot2docker start export DOCKER_HOST=tcp://192.168.59.103:2376 export DOCKER_CERT_PATH=/Users/pa/.boot2docker/certs/boot2docker-vm export DOCKER_TLS_VERIFY=1 boot2docker stop boot2docker download boot2docker start mise à jour
Install sur Ubuntu apt-get install docker
RUN sans daemon, sans terminal docker run hello-world hello-world est ici une image docker run learn/tutorial /bin/echo hello world docker run learn/tutorial apt-get install -y ping run fait un pull si besoin docker ps docker non stoppé, daemon docker ps -l last, dernier container en cours docker ps -a tous les containers docker inspect id info bas niveau du container docker images list des images en local docker images docker rmi hello-world docker rmi learn/tutorial docker images supprime image en local docker ps -a docker rm id docker ps -a supprime le container
RUN sans daemon, avec terminal docker run -t -i ubuntu:14.04 /bin/bash charge l image ubuntu:14.04 instancie l image passe en mode interactif (autrement pas d output des commandes) branche un terminal tty (sinon pas très user friendly) execute /bin/bash exit (ctrl+d), sans -t pas de teminal sans -i pas interactif, passer en mode interactif
RUN en mode daemon docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done" passer en mode daemon return un containerid docker logs namecontainer ou id docker logs -f namecontainer ou id docker stop namecontainer ou id stop mais ne détruit pas docker restart namecontainer ou id relance la commande
Tips : --name Simplifier vous la vie docker run -t -i --name essaiubuntu ubuntu:14.04 /bin/bash
CONTAINER avec PAT docker run -d -P training/webapp python app.py -P forward le port sur le host mais on ne le chosit pas docker ps -a aficher le port docker run -d -p 5000:5000 training/webapp python app.py -p forward le port, message si port déjà pris docker port id 5000 pour connaitre le port boot2docker ip http://192.168.59.103:49156/ docker logs -f id si on refait un call du web, on voit les logs monter docker top namecontainer ou id docker inspect namecontainer ou id docker inspect -f '{{.NetworkSettings.IPAddress }}' namecontainer ou id docker restart
Attach Container docker run -d -P --name web training/webapp python app.py docker exec -i -t web bash :) Toujours pas compris à quoi sert la fonction attach : docker attach web
HUB docker images liste images déjà ramenées https://registry.hub.docker.com/ chercher sinatra https://registry.hub.docker.com/u/training/sinatra/ docker search training/sinatra si pas de / alors image officiel docker, normalement le user docker pull training/sinatra ramene l image pour info docker run fait le docker pull si besoin docker images https://registry.hub.docker.com/_/golang/ docker pull golang:1.3.2 on verra plus tard l utilité de ce docker docker login pour se connecter au hub docker, créé un fichier.dockercfg more ~pa/.dockercfg!! docker mais à disposition un container pour faire son repo étrange il est écrit en python :) L image s appelle registry https://registry.hub.docker. com/_/registry/
Commit to repository docker run -t -i training/sinatra /bin/bash gem install json touch onearchi exit docker ps -l docker diff pensive_torvalds docker commit -m="added json gem" -a="pa" d68820a0d5a0 patrickalin/sinatra:v2 id peut être les 3 premiers caractères, retourne l id de l image docker images docker run -t -i patrickalin/sinatra:v2 /bin/bash ls onearchi exit docker push patrickalin/sinatra:v2 https://registry.hub.docker.com/repos/ docker rmi patrickalin/sinatra:v2 supprimer l image que l on vient de créer
Save image/container to file docker images docker save image_id > image.tar docker rmi image_id docker load image_id < image.tar docker ps -a docker export container_id > container.tar docker rm container_id cat container.tar docker import - container_id le container devient une image!
Build from Dockerfile mkdir sinatra cd sinatra touch Dockerfile # This is a comment FROM ubuntu:14.04 MAINTAINER Patrick ALin <patrickalin@gmail.com> RUN touch OneArchi2.txt docker build -t="patrickalin/sinatra:v3". attention le. à la fin :) docker images docker run -t -i patrickalin/sinatra:v3 /bin/bash ls -> on voit notre fichier onearchi2 docker images docker tag 0cf901cc6927 patrickalin/sinatra:v4 changer le tag docker images
Example : NGINX
Network suite docker run -d -p 5000:5000 training/webapp python app.py bind fix docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py bind to interface du host docker run -d -p 127.0.0.1::5000 training/webapp python app.py docker choisi dynamiquement docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py UDP docker port id 5000 list du bind
Container linking : App with DB docker run -d --name db training/postgres docker run -d -P --name web --link db:webdb training/webapp python app.py --link name:alias docker ps --no-trunc awk '{print $NF}' voir les liens link docker exec -ti web /bin/bash Add Environment variables : env, Updating the /etc/hosts file. WEBDB_PORT_5432_TCP=tcp://172.17.0.18:5432 WEBDB_PORT=tcp://172.17.0.18:5432 WEBDB_PORT_5432_TCP_PORT=5432 WEBDB_PORT_5432_TCP_ADDR=172.17.0.18 172.17.0.18 webdb The connection will be secure and private; only the linked web container will be able to talk to the dbcontainer.
Data inter container Mount Example migration, partage de disque entre 2 containers docker run -d -v /dbdata --name db1 training/postgres -v crée le volume dbdata docker exec -i -t db1 bash (répertoire vide, touch onearchi2) docker run -d --name db2 --volumes-from db1 training/postgres volumes-from monte le volume docker exec -i -t db2 bash Mount volume from host Example backup docker run --volumes-from db1 -v $(pwd)/backup:/backup ubuntu tar cvf /backup/backup.tar /dbdata -v host:container
Ex: cross compile GO https://registry.hub.docker.com/_/golang/ sur le host créer un fichier hello.go docker run --rm -it -v "$(pwd)":/usr/src/myfolder -w /usr/src/myfolder golang:1.3.1-cross bash --rm écrase le container si il existe déjà -v monter le folder du host dans le container à /myapp -w move to this folder (work directory) exécue go build -v comme si on était sur le host cross build docker run --rm -v "$(pwd)":/myapp -w /myapp golang:1.3.1 go build -v docker run --rm -v "$(pwd)":/myapp -w /myapp -e GOOS=windows -e GOARCH=386 golang:1.3.2-cross go build -v docker run --rm -v "$(pwd)":/usr/src/myapp -w /usr/src/myapp -e GOOS=darwin -e GOARCH=amd64 golang:1.3.1-cross go build -v -e environment
Example : Clustering Cluster de nginx modifier créer un fichier index.html dans le répertoire html docker run --name data-volume-container -v $PWD/html:/usr/share/nginx/html debian:jessie /bin/bash docker run --name static1 -d -p 80 --volumes-from data-volume-container nginx /usr/sbin/nginx -g "daemon off;" docker run --name static2 -d -p 80 --volumes-from data-volume-container nginx /usr/sbin/nginx -g "daemon off;" docker port static1 80 boot2docker ip http://ip :49159 docker port static2 80 http://ip :49159
Docker UI docker run -d -p 9000:9000 -v /var/run/docker.sock:/docker.sock crosbymichael/dockerui -e /docker.sock
Autres commandes docker image -a voir tout l history docker history image:tag voir le comment docker stop $(docker ps -a -q) docker rm $(docker ps -a -q) supprimer tous les containers docker info où est-ce stocké docker events ce qui se passe en temps réel docker start, pause, unpause, stop start pour se rattacher, les autres comme le nom l indique Et bien d autres...
Others examples docker run --name mysql -e MYSQL_ROOT_PASSWORD=mysecretpassword -d mysql docker run --name="phpmyadmin" -d -P --link mysql:mysql --env=virtual_host=phpmyadmin.example.com maxexcloo/phpmyadmin docker port phpmyadmin docker run -d -P --name php -v "$(pwd)":/var/www/html --link mysql:mysql php:5.6-apache.
Si on stop la VM est-ce que l on a la reprise de docker Shipyard