Les images docker sont pratiques mais si ont prend par exemple le cas s'un serveur web, il faut bien pouvoir modifier les données présentées
Pour cela, on peut soit partager un repertoire du host ou utiliser un volume docker
docker run -d --name nginx --hostname nginx -p 12345:80 nginx
Le conteneur lance nginx le site par defaut est localisé dans /usr/share/nginx/html/
docker exec -ti nginx cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Récuperons le fichier:
mkdir -p module5/nginx/html
cd module5/nginx/
docker exec -ti nginx cat /usr/share/nginx/html/index.html > html/index.html
Editez et modifiez le fichier local html/index.html
Relancer le container:
docker rm -f nginx
docker run -d --name nginx --hostname nginx -p 12345:80 -v $(pwd)/html:/usr/share/nginx/html nginx
root@tuto01:~/module5/nginx# curl http://127.0.0.1:12345/
```html
<!DOCTYPE html>
<html>
<head>
<title>FORMATION DOCKER</title>
...
...
<h1>DOCKER TUTO MODULE 5</h1>
...
</html>
Il est possible de créer des volumes docker
docker volume ls
DRIVER VOLUME NAME
Pour le moment aucun volume
docker volume create web
docker volume ls
DRIVER VOLUME NAME
local web
Le volume est crée sur l'host docker dans /var/lib/docker/volumes/<nom du volume>
Les fichiers du volumes seront contenu dans le sous repertoire _data
Dans l'exemple /var/lib/docker/volumes/web/_data/
docker rm -f nginx
cp /home/stef/module5/nginx/html/index.html /var/lib/docker/volumes/web/_data/
docker run -d --name nginx --hostname nginx -p 12345:80 -v web:/usr/share/nginx/html nginx
Ajout d'un autre container
docker run -d --name nginx02 --hostname nginx02 -p 12346:80 -v web:/usr/share/nginx/html/ nginx
stef@tuto01:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2b10f1ac0cff nginx "/docker-entrypoint.…" 11 seconds ago Up 11 seconds 0.0.0.0:12346->80/tcp, [::]:12346->80/tcp nginx02
52699731aef4 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:12345->80/tcp, [::]:12345->80/tcp nginx
Sur le host docker, modifier le fichier /var/lib/docker/volumes/web/_data/index.html
sudo vi /var/lib/docker/volumes/web/_data/index.html
Vérifier que les deux nginx retourne la même page
curl http://127.0.0.1:12345/
<html>
...
<h1>DOCKER TUTO MODULE 5 VOLUME PARTAGE</h1>
...
</html>
curl http://127.0.0.1:12346/
<html>
...
<h1>DOCKER TUTO MODULE 5 VOLUME PARTAGE</h1>
...
</html>
Les modifications apportées dans un volume monté dans container et aussi partagé:
docker exec -ti nginx touch /usr/share/nginx/html/dummy
docker exec -ti nginx02 ls /usr/share/nginx/html
50x.html dummy index.html
Les linux namespace permettent d'isoler les processus les uns des autres que se soit au niveau fs , pid, reseaux etc ..
Ces via ce mecanisme que votre container semble fonctionner de maniere autonome par rapport a votre host docker.
On peut lister les namespaces via la commande lsns
root@tuto01:/# lsns
NS TYPE NPROCS PID USER COMMAND
4026531834 time 138 1 root /usr/lib/systemd/systemd --system --deserialize=34
4026531835 cgroup 135 1 root /usr/lib/systemd/systemd --system --deserialize=34
4026531836 pid 135 1 root /usr/lib/systemd/systemd --system --deserialize=34
4026531837 user 138 1 root /usr/lib/systemd/systemd --system --deserialize=34
4026531838 uts 133 1 root /usr/lib/systemd/systemd --system --deserialize=34
4026531839 ipc 135 1 root /usr/lib/systemd/systemd --system --deserialize=34
4026531840 net 135 1 root /usr/lib/systemd/systemd --system --deserialize=34
4026531841 mnt 132 1 root /usr/lib/systemd/systemd --system --deserialize=34
4026532435 mnt 1 4593 root ├─/usr/lib/systemd/systemd-udevd
4026532436 uts 1 4593 root ├─/usr/lib/systemd/systemd-udevd
4026532536 uts 1 692 root ├─/usr/lib/systemd/systemd-logind
4026532544 mnt 1 692 root └─/usr/lib/systemd/systemd-logind
4026531862 mnt 1 34 root kdevtmpfs
4026532488 mnt 3 18377 root nginx: master process nginx -g daemon off;
4026532489 uts 3 18377 root nginx: master process nginx -g daemon off;
4026532490 ipc 3 18377 root nginx: master process nginx -g daemon off;
4026532491 pid 3 18377 root nginx: master process nginx -g daemon off;
4026532492 cgroup 3 18377 root nginx: master process nginx -g daemon off;
4026532493 net 3 18377 root nginx: master process nginx -g daemon off;
Il est possible de switcher dans un processus et sont namespace d'execution via la commande nsenter
exemple:
sudo nsenter --target 18377 --mount --uts --ipc --pid --net --cgroup bash
root@nginx:/#
La commande prédcédente est equivalent a docker exec -ti nginx bash
docker rm -f nginx nginx02
docker volume rm web