Présentation

Historique

  • Créé par Solomon Hykes (français)
  • Idée est venu de l'industrie du transport
  • Première version en mars 2013

Principe

Principe

Avantages

  • Configuration simplifiée

  • Environnement consistant

  • Application facilement isolée

  • Déploiement rapide

  • Virualisation plus légère

Inconvénients

  • Basé sur du Linux

  • Complexité supplémentaire

  • Outils de monitoring et management

  • Sécurité des images publiques

Conteneur VS VM

Sur les différents OS

Linux

Windows

Mac

Image

Présentation

Présentation

Dockerfile

Dockerfile

Dockerfile

Gestion globale

Lister les images

$ docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED           SIZE
gitlab/gitlab-ce                  latest              123dce7befbc        4 days ago        1.216 GB
aquasys/java                      latest              8296b08b5566        5 days ago        640.9 MB
lolhens/artifactory               latest              8f7705ed061d        2 weeks ago       504 MB
java                              8                   96cddf5ae9f1        2 weeks ago       640.9 MB
aquasys/sbt                       latest              bf9d748f4603        3 months ago      176.7 MB
rabbitmq                          latest              7d35f955ee99        4 months ago      177.8 MB
1science/sbt                      latest              244262a4d9ad        4 months ago      176.7 MB
hseeberger/scala-sbt              latest              3cd03e0fbc45        4 months ago      770.3 MB
postgres                          9.4                 fad840dae449        7 months ago      263.1 MB

Construire une image

$ docker build -t aquasys/sbt .
Sending build context to Docker daemon 61.44 kB
Step 1 : FROM 1science/sbt
 ---> 244262a4d9ad
Step 2 : EXPOSE 9000
 ---> Running in 3e8db64a4788
 ---> 42db24e06919
Removing intermediate container 3e8db64a4788
Step 3 : EXPOSE 9999
 ---> Running in 4a24f1a499ab
 ---> 3871c32cc872
Removing intermediate container 4a24f1a499ab
Step 4 : WORKDIR /app
 ---> Running in c1a12b9f4c13
 ---> 1e8abfdb5e22
Removing intermediate container c1a12b9f4c13
Step 5 : CMD sbt -jvm-debug 9999 run
 ---> Running in b5ed1a3873dd
 ---> 983192e06946
Removing intermediate container b5ed1a3873dd
Successfully built 983192e06946

Supprimer une image

docker rmi aquasys/sbt
Untagged: aquasys/sbt:latest
Deleted: sha256:983192e06946a1aafe93b013ab384eab75de204ba96d568ff2897b4105f351ba
Deleted: sha256:1e8abfdb5e22e978c2c0cfd6267d97a8b5e7208a34b8e7c9831f5cde6ddac7b7
Deleted: sha256:3871c32cc872c2acf1f9723df41933a3e46676e3224980e7b60902f85e490a22
Deleted: sha256:42db24e06919cb95937dbfbfb76076bd9fc2bd109c9d81f187b431492c138542

Docker Hub

Rechercher une image

$ docker search sinatra
NAME                                   DESCRIPTION              STARS     OFFICIAL   AUTOMATED
training/sinatra                       Sinatra training image   0                    [OK]
marceldegraaf/sinatra                  Sinatra test app         0
mattwarren/docker-sinatra-demo                                  0                    [OK]
luisbebop/docker-sinatra-hello-world                            0                    [OK]
subwiz/sinatra                                                  0
bmorearty/sinatra                                               0

Récupérer une image

$ docker pull centos

Using default tag: latest
latest: Pulling from library/centos
f1b10cd84249: Pull complete
c852f6d61e65: Pull complete
7322fbe74aa5: Pull complete
Digest: sha256:90305c9112250c7e3746425477f1c4ef112b03b4abe78c612e092037bfecc3b7
Status: Downloaded newer image for centos:latest

Partager une image

$ docker push ouruser/sinatra

The push refers to a repository [ouruser/sinatra] (len: 1)
Sending image list
Pushing repository ouruser/sinatra (3 tags)

Conteneur

Présentation

Présentation

One process per container

Gestion globale

Liste des conteneurs

$ docker ps -a
CONTAINER ID    IMAGE                    COMMAND                CREATED        STATUS                    
PORTS                                                            NAMES
2179702eb174    gitlab/gitlab-ce:latest  "/assets/wrapper"      4 minutes ago  Up 4 minutes
0.0.0.0:22->22/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:9000->80/tcp   gitlab
1f8dfad56928    rabbitmq                 "/docker-entrypoint.s" 3 months ago   Exited (0) 5 days ago     
4369/tcp, 5671-5672/tcp, 25672/tcp                               some-rabbit

Lancement d'un conteneur

$ docker run --detach \
    --hostname gitlab.example.com \
    --publish 443:443 --publish 80:80 --publish 22:22 \
    --name gitlab \
    --restart always \
    --volume /srv/gitlab/config:/etc/gitlab \
    --volume /srv/gitlab/logs:/var/log/gitlab \
    --volume /srv/gitlab/data:/var/opt/gitlab \
    gitlab/gitlab-ce:latest

2179702eb174343b59c1aa9bf2260e34db0ab4ceb64cdf395fbaee28ab358a07

Lancement d'un conteneur - detach

$ docker run --help
...
...
...
-d, --detach                      Run container in background and print container ID

Lancement d'un conteneur - publish

$ docker run --help
...
...
...
-p, --publish value               Publish a container's port(s) to the host (default [])

Lancement d'un conteneur - restart

no Do not automatically restart the container when it exits. This is the default.
on-failure[:max-retries] Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.
Policy Result

Lancement d'un conteneur - restart

always Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely.
unless-stopped Always restart the container regardless of the exit status, but do not start it on daemon startup if the container has been put to a stopped state before.
Policy Result

Accès aux logs

$ docker logs 2179702eb174343b59c1aa9bf2260e34db0ab4ceb64cdf395fbaee28ab358a07
$ docker logs 21797
$ docker logs -f 21797
$ docker logs --help

Usage:	docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --help           Print usage
      --since string   Show logs since timestamp
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps

Redémarrage

$ docker stop gitlab
$ docker start 21797
$ docker restart 21797

Suppression

$ docker rm gitlab
gitlab

Attachement au conteneur

$ docker attach topdemo
top - 02:05:52 up  3:05,  0 users,  load average: 0.01, 0.02, 0.05
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.2%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    373572k total,   355560k used,    18012k free,    27872k buffers
Swap:   786428k total,        0k used,   786428k free,   221740k cached

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1 root      20   0 17200 1116  912 R    0  0.3   0:00.03 top

 top - 02:05:55 up  3:05,  0 users,  load average: 0.01, 0.02, 0.05
 Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
 Cpu(s):  0.0%us,  0.2%sy,  0.0%ni, 99.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
 Mem:    373572k total,   355244k used,    18328k free,    27872k buffers
 Swap:   786428k total,        0k used,   786428k free,   221776k cached

   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
       1 root      20   0 17208 1144  932 R    0  0.3   0:00.03 top


 top - 02:05:58 up  3:06,  0 users,  load average: 0.01, 0.02, 0.05
 Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
 Cpu(s):  0.2%us,  0.3%sy,  0.0%ni, 99.5%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
 Mem:    373572k total,   355780k used,    17792k free,    27880k buffers
 Swap:   786428k total,        0k used,   786428k free,   221776k cached

Attachement au conteneur

$ docker exec -i -t gitlab /bin/bash
root@cccb4ef3c19b:/# ls
RELEASE  bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
assets   boot  etc  lib   media  opt  root  sbin  sys  usr
root@cccb4ef3c19b:/# exit
exit

$

Volumes

Persistance des données 

Affecter un volume

$ docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
$ docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py
$ docker run -d -P --name web -v /src/webapp:/opt/webapp:w training/webapp python app.py
FROM ubuntu
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol

Partager un volume

$ docker create -v /dbdata --name dbstore training/postgres /bin/true
$ docker run -d --volumes-from dbstore --name db1 training/postgres
$ docker run -d --name db3 --volumes-from db1 training/postgres

Backup, restauration

$ docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
$ docker run -v /dbdata --name dbstore2 ubuntu /bin/bash
$ docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash \
-c "cd /dbdata && tar xvf /backup/backup.tar --strip 1"

Docker Compose

Présentation

Syntaxe

 version: '2'
 services:
   web:
     build: .
     ports:
      - "5000:5000"
     volumes:
      - .:/code
     depends_on:
      - redis
   redis:
     image: redis
FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD python app.py

Variables d'environnement

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:

environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET
env_file: .env

env_file:
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env

Docker Swarm

Présentation

En environnement de développement

Avantages

  • Utile pour du micro-service

  • Compilation à la volée

  • Environnement quasi-similaire (dev / prod)

Inconvénients

  • Mise en place

  • Différents OS

  • Formation

Mise en place

Monitoring

Logs

Métriques

docker stats

Health Check

docker

By benjamin tourman

docker

  • 676