Podman - Deploiement Des Conteneurs

Podman et Skopeo permetten de gérer les conteneurs et les images de conteu=neurs.

Utilitaires pour conteneurs

Le paquet container-tools permet de télécharger, exécuter et comparer des conteneurs (et contient podman et skopeo) :

# dnf install container-tools

Information sur le paquet :

$ dnf info container-tools

[...]
Summary : A meta-package witch container tools such as podman, buildah, skopeo, etc.
License : MIT
Description : Latest versions of podman, buildah, skopeo, runc, conmon, CRIU, Udica, etc as well as dependencies such as container-selinux built and tested together, and updated.
[...]

Skopeo

La commande skopeo inspect affiche les informations d’image de conteneur (versions, ports, métadonnées, paquetages inclus,…) :

$ skopeo inspect docker://registry.access.redhat.com/ubi8/python-38

{
"Name": "registry.access.redhat.com/ubi8/python-38",
"Digest":
"sha256:a2c8b3e7f0d941aa2c8b3i4h6gaf9dbecl8k2j3i4h6gaf9dbec2c8b3e7f0d941",
"RepoTags": [
[...]
"1-68",
"1-77-source",
"latest"
[...]
"name": "ubi8/python-38",
"release": "86.1648121386",
"summary": "Platform for building and running Python 3.8 applications",
[...]

Podman

Podman n’utilise pas de daemon pour fonctionner, il n’est pas nécessaire d’avoir un compte root pour utiliser les conteneurs.

Commandes courantes

Commandes courantes de Podman.

Commandes Podman Description
podman build Crée une image de conteneur avec un fichier de conteneurs
podman run Exécute une commande dans un nouveau conteneur
podman images Liste les images du stockage local
podman ps Affiche des informations des conteneurs
podman inspect Affiche la config d’un conteneur, image, volume, réseau, pod
podman pull Télécharge une image à partir d’un registre
podman cp Copie des fichiers/répertoires entre un conteneur et le système local
podman exec Exécute une commande dans un conteneur en cours d’exécution
podman rm Supprime un ou plusieurs conteneurs
podman rmi Supprime une ou plusieurs images stockées localement
podman search Recherche une image dans un registre

Les images

La commande podman info affiche les informations des images :

$ podman info

[...]
insecure registries:
registries: []
registries:
registries:
- registry.redhat.io
- quay.io
- docker.io
[...]

La commande podman search recherche une image dans registries.conf :

$ podman search python-38

NAME DESCRIPTION
registry.access.redhat.com/ubi7/python-38 Python 3.8 platform for building and running applications
registry.access.redhat.com/ubi8/python-38 Platform for building and running Python 3.8 applications
[...]

La commande podman pull permet de télécharger l’image :

$ podman pull registry.access.redhat.com/ubi8/python-38

Trying to pull registry.access.redhat.com/ubi8/python-38:latest...
Getting image source signatures
Checking if image destination supports signatures
Copying blob b664526afe9e done
[...]

La commande podman images affiche les images locales :

$ podman images

REPOSITORY TAG IMAGE ID CREATED SIZE
registry.access.redhat.com/ubi8/python-38 latest b25ee6afd458 1 hour ago 901 MB

Création d’images

Il est possible de créer une image de conteneur à partir d’un fichier de conteneur.

Ce fichier de conteneur permet de créer l’image de conteneur dans le répertoire python36-app :

$ cat myContainerFile

FROM registry.access.redhat.com/ubi8/ubi:latest
RUN dnf install -y python3
CMD ["/bin/bash", "-c", "sleep infinity"]
  • FROM registry.access.redhat.com/ubi8/ubi:latest : image qui sera utilisé
  • RUN dnf install -y python3 : installe Python3
  • CMD ["/bin/bash", "-c", "sleep infinity"] : exécute un sleep infini pour empêcher la fermeture du conteneur

La commande podman build permet de compiler l’image, l’option sert -t à indiquer le nom et la balise :

$ podman build -t NAME:TAG DIR
  • NAME : Nom de l’image
  • TAG : Balise de l’image. Si lpas spécifiée, met automatiquement latest
  • DIR : Chemin du répertoire de travail :
    • le fichier de conteneur doit se trouver dans le répertoire de travail
    • si c’est le répertoire courant, on utilise le “.”
    • si répertoire différent du répertoire actuel : option -f
$ podman build -t python36:1.0 .

STEP 1/3: FROM registry.access.redhat.com/ubi8/ubi:latest
STEP 2/3: RUN dnf install -y python36
[...]
STEP 3/3: CMD ["/bin/bash", "-c", "sleep infinity"]
COMMIT python36:1.0
--> 2ac6b82088b
Successfully tagged localhost/python36:1.0
b5407ffc94cb4664526afe9880f17064526afe9e2506b8b6880f17064526afe9

La commande podman images permet de vérifier que l’image créée :

$ podman images

REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/python36 1.0 b5407ffc94cb 3 minute ago 266 MB
registry.access.redhat.com/ubi8/python-38 latest a33d92f90990 1 hour ago 901 MB

La commande podman inspect permet d’afficher les informations de l’image :

$ podman inspect localhost/python36:1.0

[...]
"Cmd": [
"/bin/bash",
"-c",
"sleep infinity"
],
[...]
{
"created": "2022-04-18T19:47:52.708227513Z",
"created_by": "/bin/sh -c dnf install -y python36",
"comment": "FROM registry.access.redhat.com/ubi8/ubi:latest"
},
[...]

Les conteneurs

Etats des conteneurs

Un conteneur (et ses processus) peut être dans l’un des états suivants :

  • Created : conteneur créé mais pas démarré
  • Running : conteneur qui s’exécute
  • Stopped : conteneur arrêté
  • Paused : conteneur en pause ( pas pris en charge pour les rootless)
  • Deleted : conteneur inactif (dead).

La commande podman ps liste les conteneurs en cours d’exécution et podman ps -a liste tous les conteneurs

Créer des conteneurs

La commande podman create permet de créer un conteneur avec l’ID de l’image :

$ podman create --name python36 dd6ca291f097
c54c7ee281581c198cb96b07d78a0f94be083ae94dacbae69c05bd8cd354bbec
$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
c54c7ee28158 localhost/python36:1.0 /bin/bash -c slee... 5 seconds ago Created
python36

La commande podman start démarre le conteneur :

$ podman start python36
python36
$ podman start <ID>
python36
$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
c54c7ee28158 localhost/python36:1.0 /bin/bash -c slee... 6 minutes ago Up 3
seconds ago python36

Exécuter des conteneurs

La commande podman run permet de créer et exécuter le conteneur en une seule étape, l’option -d (detach) permet d’exécuter le conteneur en arrière-plan.

Si une commande est déjà définie dans le fichier de conteneur (sleep infinity), il n’est pas nécessaire d’éxécuter le conteneur avec une commande mais si lancée quand même, cela empêchera la fermeture du conteneur :

$ podman run -d --name python38 registry.access.redhat.com/ubi8/python-38 sleep infinity
a60f71a1dc1b997f5ef244aaed232e5de71dd1e8a2565428ccfebde73a2f9462
$ podman ps
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
c54c7ee28158 localhost/python36:1.0 /bin/bash -c
slee... 37 minutes ago Up 30 minutes ago python36
a60f71a1dc1b registry.access.redhat.com/ubi8/python-38:latest sleep infinity
32 seconds ago Up 33 seconds ago python38

La commande podman exec permet d’afficher les processus en cours d’exécution dans le conteneur python36 :

$ podman exec python38 ps -ax
PID TTY STAT TIME COMMAND
1 ? Ss 0:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep infinity
7 ? R 0:00 ps -ax

La commande sh -c permet d’encapsuler la commande à exécuter dans le conteneur, le caractère > peut être interpréter directement par Podmanet pas comme argument de l’option podman exec :

$ podman exec python38 sh -c 'ps -ax > /tmp/process-data.log'
PID TTY STAT TIME COMMAND
1 ? Ss 0:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep infinity
7 ? R 0:00 ps -ax

Comparaison de version python installée sur le système et dans les conteneurs :

$ python3 --version
Python 3.9.10
$ podman exec python36 python3 --version
Python 3.6.8
$ podman exec python38 python3 --version
Python 3.8.8

Copie de fichiers entre système et conteneurs

La commande podman cp copie les fichiers et les répertoires entre le système et le conteneur

$ podman cp /tmp/hello.sh python38:/tmp/hello.sh
$ podman exec python38 stat /tmp/hello.sh
File: /tmp/hello.sh
Size: 19 Blocks: 8 IO Block: 4096 regular file
Device: 3bh/59d Inode: 12280058 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ default) Gid: ( 0/ root)
Access: 2024-02-25 90:06:42.000000000 +0000
Modify: 2024-02-25 90:06:42.000000000 +0000
Change: 2024-02-25 90:12:04.552212154 +0000
Birth: 2024-02-25 90:12:04.552212154 +0000

Une fois le script copié dans le conteneur, il peut y être exécuté :

$ podman exec python38 bash /tmp/hello.sh
hello world

Suppression de conteneurs et images

Les commandes :

  • podman rm : supprimer des conteneurs
  • podman rmi : supprimer des images

Avant de supprimer une image il faut supprimer le conteneur en cours d’éxécution :

$ podman rmi registry.access.redhat.com/ubi8/python-38
Error: Image used by
a60f71a1dc1b997f5ef244aaed232e5de71dd1e8a2565428ccfebde73a2f9462: image is in use by a container

Arrêter le conteneur avec podman stop :

$ podman stop python38

Supprimer le conteneur avec podman rm :

$ podman rm python38
a60f71a1dc1b997f5ef244aaed232e5de71dd1e8a2565428ccfebde73a2f9462

Supprimer l’image avec podman rmi :

$ podman rmi registry.access.redhat.com/ubi8/python-38
Untagged: registry.access.redhat.com/ubi8/python-38:latest
Deleted: a33d92f90990c9b1bad9aa98fe017e48f30c711b49527dcc797135352ea57d12

/!\ Résumé des commandes

Commandes :

$ podman build	  // Crée une image de conteneur avec un fichier de conteneurs 
$ podman run // Exécute une commande dans un nouveau conteneur
$ podman images // Liste les images du stockage local
$ podman ps // Affiche des informations des conteneurs
$ podman inspect // Affiche la config d’un conteneur, image, volume, réseau, pod
$ podman pull // Télécharge une image à partir d’un registre
$ podman cp // Copie des fichiers/répertoires entre un conteneur et le système local
$ podman exec // Exécute une commande dans un conteneur en cours d’exécution
$ podman rm // Supprime un ou plusieurs conteneurs
$ podman rmi // Supprime une ou plusieurs images stockées localement
$ podman search // Recherche une image dans un registre

$ skopeo inspect docker://registry.access.redhat.com/ubi8/python-38

$ podman info
$ podman search python-38
$ podman pull registry.access.redhat.com/ubi8/python-38
$ podman images
$ podman build -t NAME:TAG DIR
$ podman build -t python36:1.0 .
$ podman inspect localhost/python36:1.0
$ podman create --name python36 dd6ca291f097
$ podman ps
$ podman ps -a
$ podman start python36
$ podman start <ID>
$ podman run -d --name python38 registry.access.redhat.com/ubi8/python-38 sleep infinity
$ podman exec python38 ps -ax
$ podman exec python38 sh -c 'ps -ax > /tmp/process-data.log'
$ podman exec python36 python3 --version
$ podman cp /tmp/hello.sh python38:/tmp/hello.sh
$ podman exec python38 bash /tmp/hello.sh
$ podman stop python38
$ podman rm python38
$ podman rmi registry.access.redhat.com/ubi8/python-38

Fichiers :


Documentation

MAN containersregistries.conf(5)
MAN podman(1)
MAN podman-build(1)
MAN podman-cp(1)
MAN podman-exec(1)
MAN podman-images(1)
MAN podman-inspect(1)
MAN podman-ps(1)
MAN podman-pull(1)
MAN podman-rm(1)
MAN podman-rmi(1)
MAN podman-run(1)
MAN podman-search(1)
MAN podmanstop(1)

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/building_running_and_managing_containers/index#starting-withcontainers_building-running-and-managing-containers

> Partager <