Docker

Commandes principales

Conteneur

Création depuis une image

docker run --name cont_name image command

-i | –interactive : Keep STDIN open even if not attached
-t | –tty : Allocate a pseudo tty
–rm : Automatically remove the container when it exits

Attachement d'un volume
docker run --name cont_name --mount source=volume_name,target=/container/path node:8.10.0-stretch

Volume

Création depuis un répertoire de l'hôte

docker volume create --name vol_name -o type=none -o device=/path/to/host/file_or_dir -o o=bind

-o: driver specific options
o: options (comma-separated)

Extraire le contenu d'un volume

docker volume ls
DRIVER    VOLUME NAME
local     osx-big-sur_data

mkdir data

docker run --rm -v osx-big-sur_data:/volume -v `pwd`/data:/target busybox sh -c 'cp -a /volume/* /target/'

Commandes secondaires

Divers

Afficher la date et l'heure de création d'une image ou d'un conteneur

docker inspect -f '{{ .Created }}' IMAGE_OR_CONTAINER

Gestion du réseau

Utilisation du driver macvlan

Cas d'utilisation :

Exposition d'un serveur DNS conténeurisé avec une @IP spécifique et communication possible entre l'hôte et le conteneur.

docker network create -d macvlan --subnet=192.168.0.0/24 --gateway=192.168.0.254 --ip-range 192.168.0.1/27 --aux-address "host=192.168.0.1" -o parent=enp0s3 befe.lan

docker-compose.yml

version: "3.7"

networks:
  befe.lan:
    external: true

services:
  adguard:
    image: adguard/adguardhome:v0.107.26
    networks:
      befe.lan:
        ipv4_address: 192.168.0.2
    ports:
      - 192.168.0.2:53:53/tcp # DNS
      - 192.168.0.2:53:53/udp # DNS
ip link add befe.lan-shim link enp0s3 type macvlan mode bridge
ip addr add 192.168.0.253/32 dev befe.lan-shim
ip link set befe.lan-shim up
ip route add 192.168.0.2/32 dev befe.lan-shim

Pour une configuration persistante :

/etc/network/interfaces.d/befe.lan-shim

auto befe.lan-shim
iface befe.lan-shim inet manual
        pre-up    /bin/ip link  add befe.lan-shim link enp0s3 type macvlan mode bridge
        up        /bin/ip addr  add 192.168.0.253/32 dev befe.lan-shim
        post-up   /bin/ip route add 192.168.0.2/32   dev befe.lan-shim
        post-down /bin/ip route del befe.lan-shim

Gestion du mode graphique

Lancer un conteneur en mode graphique

docker run --rm -it \
  --net=host \
  -e DISPLAY \
  -v /home/me/.Xauthority:/root/.Xauthority \
  gns3/xeyes

Exemples

DockerFile

FROM webdevops/php-apache-dev:debian-8
ENV WEB_PHP_SOCKET  127.0.0.1:9002
ENV fpm.pool.listen 127.0.0.1:9002
EXPOSE 11080
RUN apt-get update
RUN apt-get install -y mysql-client

Construction de l'image

cd path/to/Dockerfile/parent/directory
docker build -t dev_node .

Script de création de container

#!/bin/bash

docker run -it --name app_demo \
--mount source=workspace,target=/data/workspace \
--mount source=vue_templates,target=/root/.vue-templates \
dev_node \
bash -c " \
cd /data/workspace/app-demo && \
ls -l \
"

Lancement du container

docker start -i app_demo

Dockfile - divers

Création d'un utilisateur/group avec ids spécifiques :

RUN addgroup docker-test --gid 1000 && \
	adduser docker-test --debug --no-create-home --disabled-password --gecos "" --uid 1000 --gid 1000

Healthcheck

httpd

healthcheck:
  test: timeout 10s bash -c ':> /dev/tcp/127.0.0.1/80' || exit 1
  interval: 60s
  timeout: 30s
  retries: 3

Registry

Lister les tags d'un dépôt

docker run -t --rm benjaminferon/docker-tags <DEPOT>

Commandes améliorées

ps

docker ps -a --format="table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Command}}\t{{.Status}}\t{{.Ports}}" | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}'

Astuces pour la construction d'images

Prendre en charge Ctrl+C dans un script bash

# Add support for CTRL+C
exit_func() {
  exit 1
}
trap exit_func SIGTERM SIGINT

Divers

Maintenir un container en vie

docker-compose.yml

entrypoint: 'tail -f /dev/null'

Lister les PID des conteneurs

for i in $(docker container ls --format "{{.ID}}"); do docker inspect -f '{{.State.Pid}} {{.Name}}' $i; done

Voir les adresses IP des conteneurs

docker ps -q | xargs -n 1 docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{ .Name }}' | sed 's/ \// /'

Commandes diverses

PHP Composer et limitation de la mémoire

docker run -it --rm -v $(pwd):/app -w /app -e COMPOSER_MEMORY_LIMIT=-1 composer:latest bash -c "php -r \"ini_set('memory_limit', '8G');\" && composer update"

Problèmes connus

MySQL/MariaDB :

Error: io_setup() failed with EAGAIN after 5 attempts
$ sysctl fs.aio-max-nr
fs.aio-max-nr = 65536
sudo sysctl -w fs.aio-max-nr=2097152