Ceci est une ancienne révision du document !


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

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

Registry

Lister les 10 premiers tags d'une image

curl 'https://registry.hub.docker.com/v2/repositories/library/debian/tags/'|jq '."results"[]["name"]'

Lister tous les tags d'une image

#!/bin/bash

# Don't understand why but whether the repository name contain "/" in its name, registry address must be diffrent !
# If someone has an idea...

REGISTRY_FOR_REPO_WITHOUT_SLASH="https://registry.hub.docker.com"
REGISTRY_FOR_REPO_WITH_SLASH="https://hub.docker.com"
PAGE_SIZE=100

REPO=$1

command -v curl >/dev/null 2>&1 || { echo >&2 "curl is not installed.  Aborting."; exit 1; }
command -v jq >/dev/null 2>&1 || { echo >&2 "jq is not installed.  Aborting."; exit 1; }

if [[ ! "$REPO" =~ "/" ]]; then
  url="$REGISTRY_FOR_REPO_WITHOUT_SLASH/v2/repositories/library/$REPO/tags"
else
  url="$REGISTRY_FOR_REPO_WITH_SLASH/v2/repositories/$REPO/tags"
fi

count=$(curl "$url/?page=1&page_size=1" 2>/dev/null|jq '."count"')

i=1
while [ $count -gt 0 ]
do
   curl "$url/?page=$i&page_size=$PAGE_SIZE" 2>/dev/null|jq '."results"[]["name"]'
   i=$((i+1))
   count=$((count-PAGE_SIZE))
done

Extraire le Dockerfile d'une image

docker history --no-trunc [image]  | tac | tr -s ' ' | cut -d " " -f 5- | sed 's,^/bin/sh -c #(nop) ,,g' | sed 's,^/bin/sh -c,RUN,g' | sed 's, && ,\n  & ,g' | sed 's,\s*[0-9]*[\.]*[0-9]*\s*[kMG]*B\s*$,,g' | head -n -1

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"}'