meta données pour cette page
Différences
Ci-dessous, les différences entre deux révisions de la page.
| — |
envrac:docker-rootless [2024/08/16 17:25] (Version actuelle) ztrulphcs créée |
||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== docker rootless sous ubuntu ====== | ||
| + | |||
| + | ^ Date | août 2023 | | ||
| + | ^ ::: | 2024/ | ||
| + | ^ Temps | 10 minutes | ||
| + | ^ Difficulté | ||
| + | ^ Type | how-to | ||
| + | ^ OS | ✓ Ubuntu GNU/Linux 24.04 LTS «noble numbat» | ||
| + | ^ ::: | ✓ Ubuntu GNU/Linux 22.04 LTS «jammy jellyfish» | ||
| + | ^ cible | sysadmins tout venant | ||
| + | ^ Références | ||
| + | ^ ::: | docker : [[https:// | ||
| + | ^ ::: | docker : [[https:// | ||
| + | | ::: | github : [[https:// | ||
| + | | ::: | digital ocean: [[https:// | ||
| + | |||
| + | |||
| + | |||
| + | Le plus simple est de suivre les documentations citées en référence. | ||
| + | |||
| + | Mais j'ai noté un ou deux trucs. Ici, un résumé. | ||
| + | |||
| + | ===== Commandes à passer sous le compte root ===== | ||
| + | |||
| + | Pour économiser mes petits doigts, je ne tape sudo qu'une seule fois, | ||
| + | pour devenir root. Si vous ne faites pas cela, vous préfixerez les | ||
| + | commandes par sudo quand il le faudra. | ||
| + | |||
| + | <code bash> | ||
| + | sudo -i | ||
| + | </ | ||
| + | |||
| + | ===== Créer un utilisateur non privilégié ===== | ||
| + | |||
| + | Utilisateur système, volontairement sans sudo et à mot de passe bloqué. Connexion uniquement via ssh et des clés. | ||
| + | |||
| + | - Création du compte système, ajout dans ''/ | ||
| + | nouveauCompte=dockeruser | ||
| + | |||
| + | adduser --system --gecos " | ||
| + | | ||
| + | chpasswd -e <<<" | ||
| + | IFS=: read _ base taille < <( sort -b -t: -k 2,2n /etc/subuid | tail -1 ) && | ||
| + | echo ${nouveauCompte}: | ||
| + | IFS=: read _ base taille < <( sort -b -t: -k 2,2n /etc/subgid | tail -1 ) && | ||
| + | echo ${nouveauCompte}: | ||
| + | </ | ||
| + | - Copie des clés ssh autorisées de root (très personnel et donc très optionnel). M' | ||
| + | install -d -m 0700 / | ||
| + | cp -a / | ||
| + | </ | ||
| + | - Préconfigurer le démon docker rootless avec un fichier daemon.json. Là encore c'est très personnel -- et donc très optionnel -- mais ça donne une idée de ce qu'on peut faire facilement.< | ||
| + | install -d / | ||
| + | cat > / | ||
| + | { | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ], | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | EOH | ||
| + | </ | ||
| + | - Et finalement, corriger les propriétaires et groupes des fichiers.< | ||
| + | chown -hR ${nouveauCompte}: | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Installation des commandes docker ===== | ||
| + | |||
| + | |||
| + | Supprimer toute trace de docker qui n'est pas celui livré par docker soi même. | ||
| + | |||
| + | <code bash> | ||
| + | for pkg in \ | ||
| + | docker.io docker-doc docker-compose docker-compose-v2 \ | ||
| + | podman-docker containerd runc \ | ||
| + | ; do | ||
| + | apt-get purge $pkg | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | Et puis installation | ||
| + | <code bash> | ||
| + | dpkg -l dbus-user-session | grep -q ^.i || sudo apt-get install dbus-user-session | ||
| + | dpkg -l uidmap | grep -q ^.i || sudo apt-get install uidmap | ||
| + | apt-get install ca-certificates curl gnupg | ||
| + | install -m 0755 -d / | ||
| + | |||
| + | curl -fsSL https:// | ||
| + | -o / | ||
| + | chmod a+r / | ||
| + | echo \ | ||
| + | "deb [arch=$(dpkg --print-architecture) \ | ||
| + | signed-by=/ | ||
| + | https:// | ||
| + | $(. / | ||
| + | tee / | ||
| + | |||
| + | apt-get update | ||
| + | apt-get install docker-ce docker-ce-cli containerd.io \ | ||
| + | docker-buildx-plugin docker-compose-plugin | ||
| + | </ | ||
| + | |||
| + | |||
| + | Docker est maintenant installé. Pour l' | ||
| + | |||
| + | ===== Préparation du mode rootless ===== | ||
| + | |||
| + | Pour qu'on puisse utiliser les port réservés, il faut utiliser **une et une seule** des deux méthodes présentées ci-dessous. | ||
| + | |||
| + | - 1< | ||
| + | setcap cap_net_bind_service=ep $(which rootlesskit) | ||
| + | </ | ||
| + | * 2023 :\\ A priori, ce réglage est permanent et devrait résister aux mises à jour. mais c'est pas certain. CF [[https:// | ||
| + | * 2024 :\\ Ça résiste, sans souci. | ||
| + | - 2< | ||
| + | echo net.ipv4.ip_unprivileged_port_start=0 > / | ||
| + | sysctl --system | ||
| + | </ | ||
| + | |||
| + | ===== Passage en mode rootless ===== | ||
| + | |||
| + | En préliminaire, | ||
| + | et installer le paquet '' | ||
| + | <code bash> | ||
| + | systemctl disable --now docker.service docker.socket | ||
| + | apt-get install docker-ce-rootless-extras | ||
| + | </ | ||
| + | |||
| + | ==== S' | ||
| + | |||
| + | <code bash> | ||
| + | loginctl enable-linger $nouveauCompte | ||
| + | </ | ||
| + | |||
| + | ==== Devenir l' | ||
| + | |||
| + | <code bash> | ||
| + | ssh $nouveauCompte@localhost | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | dockerd-rootless-setuptool.sh install | ||
| + | </ | ||
| + | Cette commande crée et active le contexte docker '' | ||
| + | Avec cet utilisateur, | ||
| + | comme si docker était installé de manière standard. | ||
| + | |||
| + | ++++détails si ça vous intéresse| | ||
| + | <cli> | ||
| + | dockeruser@pc: | ||
| + | [INFO] Creating / | ||
| + | [INFO] starting systemd service docker.service | ||
| + | + systemctl --user start docker.service | ||
| + | + sleep 3 | ||
| + | + systemctl --user --no-pager --full status docker.service | ||
| + | ● docker.service - Docker Application Container Engine (Rootless) | ||
| + | | ||
| + | | ||
| + | Docs: https:// | ||
| + | Main PID: 7091 (rootlesskit) | ||
| + | Tasks: 40 | ||
| + | | ||
| + | CPU: 586ms | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | Jul 31 18:18:23 pc dockerd-rootless.sh[7134]: | ||
| + | Jul 31 18:18:23 pc dockerd-rootless.sh[7134]: | ||
| + | Jul 31 18:18:23 pc dockerd-rootless.sh[7134]: | ||
| + | Jul 31 18:18:23 pc dockerd-rootless.sh[7134]: | ||
| + | Jul 31 18:18:23 pc dockerd-rootless.sh[7134]: | ||
| + | Jul 31 18:18:23 pc dockerd-rootless.sh[7134]: | ||
| + | Jul 31 18:18:23 pc dockerd-rootless.sh[7134]: | ||
| + | Jul 31 18:18:23 pc dockerd-rootless.sh[7134]: | ||
| + | Jul 31 18:18:23 pc dockerd-rootless.sh[7134]: | ||
| + | Jul 31 18:18:23 pc systemd[4564]: | ||
| + | + DOCKER_HOST=unix:/// | ||
| + | Client: Docker Engine - Community | ||
| + | | ||
| + | API version: | ||
| + | Go version: | ||
| + | Git commit: | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | Server: Docker Engine - Community | ||
| + | | ||
| + | Version: | ||
| + | API version: | ||
| + | Go version: | ||
| + | Git commit: | ||
| + | Built: | ||
| + | OS/ | ||
| + | Experimental: | ||
| + | | ||
| + | Version: | ||
| + | GitCommit: | ||
| + | runc: | ||
| + | Version: | ||
| + | GitCommit: | ||
| + | | ||
| + | Version: | ||
| + | GitCommit: | ||
| + | | ||
| + | Version: | ||
| + | ApiVersion: | ||
| + | NetworkDriver: | ||
| + | PortDriver: | ||
| + | StateDir: | ||
| + | | ||
| + | Version: | ||
| + | GitCommit: | ||
| + | + systemctl --user enable docker.service | ||
| + | Created symlink / | ||
| + | [INFO] Installed docker.service successfully. | ||
| + | [INFO] To control docker.service, | ||
| + | [INFO] To run docker.service on system startup, run: `sudo loginctl enable-linger dockeruser` | ||
| + | |||
| + | [INFO] Creating CLI context " | ||
| + | Successfully created context " | ||
| + | [INFO] Using CLI context " | ||
| + | Current context is now " | ||
| + | |||
| + | [INFO] Make sure the following environment variable(s) are set (or add them to ~/.bashrc): | ||
| + | export PATH=/ | ||
| + | |||
| + | [INFO] Some applications may require the following environment variable too: | ||
| + | export DOCKER_HOST=unix:/// | ||
| + | |||
| + | dockeruser@pc: | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | Vérification que ça fonctionne bien | ||
| + | <cli> | ||
| + | dockeruser@pc: | ||
| + | CONTAINER ID | ||
| + | dockeruser@pc: | ||
| + | </ | ||
| + | |||
| + | On va malgré tout ajouter la variable d' | ||
| + | n' | ||
| + | <code bash> | ||
| + | echo . $HOME/ | ||
| + | echo export DOCKER_HOST=unix:/// | ||
| + | </ | ||
| + | |||
| + | S' | ||
| + | |||
| + | <code bash> | ||
| + | systemctl --user enable docker | ||
| + | </ | ||
| + | |||
| + | Et voilà, c'est tout pour le compte dockeruser. | ||
| + | ===== Utiliser ===== | ||
| + | |||
| + | <code bash> | ||
| + | reboot | ||
| + | </ | ||
| + | Et après un redémarrage, | ||
| + | qui fonctionne. | ||
| + | |||
| + | Sur une machine distante, ou sur la même machine avec un compte | ||
| + | d' | ||
| + | de créer, | ||
| + | on peut créer un contexte docker et l' | ||
| + | en fait un réglage de la commande docker. À un moment donné, | ||
| + | pour un utilisateur donné, il ne peut y avoir qu'un seul | ||
| + | contexte docker actif. On peut créer des centaines de contextes | ||
| + | différents si besoin. | ||
| + | |||
| + | <code bash> | ||
| + | docker context create \ | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | docker context use docker-pc | ||
| + | </ | ||
| + | |||
| + | Ensuite, tout est possible. Voilà une session d' | ||
| + | <cli prompt="/ | ||
| + | moi@monpc:~ (0) $ docker ps -a | ||
| + | CONTAINER ID | ||
| + | moi@monpc:~ (0) $ docker run --rm busybox echo hello world! | ||
| + | Unable to find image ' | ||
| + | latest: Pulling from library/ | ||
| + | ec562eabd705: | ||
| + | Digest: sha256: | ||
| + | Status: Downloaded newer image for busybox: | ||
| + | hello world! | ||
| + | moi@monpc:~ (0) $ docker run --rm busybox echo hello world! | ||
| + | hello world! | ||
| + | moi@monpc:~ (0) $ docker run --name patience --rm busybox sleep infinity | ||
| + | <cli prompt=/ | ||
| + | ^C | ||
| + | </ | ||
| + | context canceled | ||
| + | moi@monpc:~ (1) $ docker ps -a | ||
| + | CONTAINER ID | ||
| + | 7c15a6d3b253 | ||
| + | moi@monpc:~ (0) $ : "Ah ben ça alors, le ^C n'a tué que la commande locale" | ||
| + | moi@monpc:~ (0) $ : "Et la commande docker continue à fonctionner" | ||
| + | moi@monpc:~ (0) $ docker stop patience | ||
| + | patience | ||
| + | moi@monpc:~ (0) $ : Voyons le réseau, maintenant. Détachons un conteneur | ||
| + | moi@monpc:~ (0) $ : qui ne fait rien et allons voir. | ||
| + | moi@monpc:~ (0) $ docker run --name patience --rm -d busybox sleep infinity | ||
| + | af3adee0ae4485eaa50f1c5bcb486d58effa6df92528eb0e23aa85569bc3dda9 | ||
| + | moi@monpc:~ (0) $ docker ps -a | ||
| + | CONTAINER ID | ||
| + | af3adee0ae44 | ||
| + | moi@monpc:~ (0) $ docker exec -it patience sh | ||
| + | <cli prompt='# | ||
| + | / # ip a | ||
| + | 1: lo: < | ||
| + | link/ | ||
| + | inet 127.0.0.1/8 scope host lo | ||
| + | | ||
| + | inet6 ::1/128 scope host | ||
| + | | ||
| + | 17: eth0@if18: < | ||
| + | link/ether 02: | ||
| + | inet 10.14.0.2/ | ||
| + | | ||
| + | / # exit | ||
| + | </ | ||
| + | moi@monpc:~ (0) $ docker stop patience | ||
| + | patience | ||
| + | moi@monpc:~ (0) $ docker ps -a | ||
| + | CONTAINER ID | ||
| + | moi@monpc:~ (0) $ | ||
| + | </ | ||
| + | |||