Configuration d'un registre d'images Docker privé avec Harbor

Envoi d'images vers un registre cloud Huawei

Pour transférer des images Docker vers le reigstre Huawei Cloud (SWR), on applique d'abord un tag puis on pousse l'image.

# Format de la commande
sudo docker tag {nom_image}:{version} swr.cn-east-3.myhuaweicloud.com/{organisation}/{nom_image}:{version}
sudo docker push swr.cn-east-3.myhuaweicloud.com/{organisation}/{nom_image}:{version}

Exemple concret :

# Connexion à Huawei Cloud
$ docker login -u cn-east-3@HST3WI8PGUOC4EI7IYMK -p 442f39e3ab61b3f4c14

# Liste des images locales
$ docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
app-java            1.0       a97d030fd858   41 hours ago     522MB
app-web             latest    8b82a40260a4   42 hours ago     253MB

# Renommer l'image pour le registre
$ docker tag app-java:1.0 swr.cn-east-3.myhuaweicloud.com/mon_projet/app-java:1.0

# Transférer l'image vers le registre
$ docker push swr.cn-east-3.myhuaweicloud.com/mon_projet/app-java:1.0

Installation d'un registre local

On peut déployer un registre Docker simple en utilisant l'image officielle registry:2.

# Lancement du conteneur registry
$ docker run -d -p 5000:5000 -v /mon_registry:/var/lib/registry registry:2

# Préparation d'une image pour ce registre
$ docker tag httpd:latest localhost:5000/httpd_locale:v1

# Envoi de l'image vers le registre local
$ docker push localhost:5000/httpd_locale:v1

# Vérification du catalogue
$ curl http://localhost:5000/v2/_catalog
{"repositories":["httpd_locale"]}

Déploiement de Harbor

Harbor est une plateforme complète de gestion de registres d'images. Voici les étapes d'installation.

# Extraction de l'archive Harbor
$ tar -xvf harbor-offline-installer-v2.9.1.tgz
$ mkdir /opt/harbor
$ mv harbor/* /opt/harbor/
$ cd /opt/harbor/

# Configuration
$ cp harbor.yml.tmpl harbor.yml
$ vim harbor.yml  # Modifier hostname et mot de passe

# Génération des fichiers de configuration
$ ./prepare
$ ./install.sh

Après l'installation, on accède à l'interfaec web pour créer des projets, par exemple un projet nommé "production".

Utilisation de Harbor comme registre privé

Pour utiliser Harbor, on doit configurer Docker pour faire confiance au registre.

# Configuration de Docker pour accepter le registre
$ cat > /etc/docker/daemon.json <<eof admin="" connexion="" d="" docker="" envoi="" eof="" et="" harbor="" harbor.example.com="" image="" login="" mon_image:latest="" push="" restart="" systemctl="" tag=""></eof>

Gestion avancée des images

Sauvegarde et restauration

# Exporter des images dans un fichier tar
$ docker save -o sauvegarde_images.tar image1:v1 image2:v2

# Importer des images depuis un fichier tar
$ docker load -i sauvegarde_images.tar

Conteneurs en arrière-plan

# Exécuter une tâche de fond
$ docker run -d --name tache_fond ubuntu /bin/bash -c "while true; do sleep 1; echo travail; done"

Accès aux conteneurs

# Méthode attach (connexion au terminal principal)
$ docker attach tache_fond

# Méthode exec (ouverture d'un nouveau shell)
$ docker exec -it tache_fond /bin/bash

Contrôle des ressources

Limitation mémoire

# Créer une image avec stress
$ docker build -t ubuntu-stress - <<eof apt-get="" avec="" docker="" entrypoint="" eof="" from="" install="" lancer="" limitation="" m="" run="" stress="" ubuntu="" ubuntu-stress="" update=""></eof>

Limitation CPU

# Ajuster la priorité CPU entre conteneurs
$ docker run -d --name conteneur_prio -c 2048 ubuntu-stress --cpu 2 -v
$ docker run -d --name conteneur_normal -c 1024 ubuntu-stress --cpu 2 -v

# Observer l'utilisation des ressources
$ docker stats

Étiquettes: Docker Harbor registre conteneur images

Publié le 9 juin à 03h08