Amb l’ordre docker images podem veure totes les imatges que s’han descarregat i que estan en la cache de la màquina virtual:
shell
$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEsail-8.3/app latest be7554cb158e 4 days ago 1.66GBredis alpine eac30ee4acc6 5 days ago 46.1MB...
Pots verificar que no tinc cap imatge alpine:3.1:
shell
$ docker images alpine:3.1REPOSITORY TAG IMAGE ID CREATED SIZE$
Per executar un contenidor s’ha de descarregar la imatge corresponent:
shell
$ docker run --name alpine alpine:3.1 echo "Hello!"Unable to find image 'alpine:3.1' locally3.1: Pulling from library/alpine0f253aa151d7: Pull complete Digest: sha256:4dfc68bc95af5c1beb5e307133ce91546874dcd0d880736b25ddbe6f483c65b4Status: Downloaded newer image for alpine:3.1Hello!
La imatge queda guardada en la màquina virtual:
shell
$ docker images alpine:3.1REPOSITORY TAG IMAGE ID CREATED SIZEalpine 3.1 a1038a41fe2b 5 years ago 5.05MB
El contenidor alpine necessita aquesta imatge perquè fa servir el sistema de fitxers de la imatge.
Si intento eliminar la imatge docker em diu que no puc:
shell
$ docker image rm alpine:3.1Error response from daemon: conflict: unable to remove repository reference "alpine:3.1" (must force)- container 22a5e3a77bb2 is using its referenced image a1038a41fe2b
Si vols eliminar les imatges que cap contenidor estigui fent servir per tal d’alliberar espai al disc, utilitza aquesta comanda:
shell
$ docker image prune -aWARNING! This will remove all images without at least one container associated to them.Are you sure you want to continue?[y/N] y
Pots veure que es baixen diferents “layers” (en parlarem més endavant).
Modifica el contingut de la pàgina index.html del contenidor:
shell
$ docker exec -it apache bash... $ echo "A poc a poc i bona lletra"> htdocs/index.html $ exitexit
O en una sóla línia:
shell
$ docker exec apache bash -c"echo 'A poc a poc i bona lletra' > htdocs/index.html"
Amb l’ordre container diff pots veure les diferències entre els sistema de fitxers del contenidor i el de les imatges de les quals deriva el contenidor:
També que el contenidor respon amb el nou fitxer index.html:
shell
$ curl localhostA poc a poc i bona lletra
Si parem el contenidor aquest s’esborra i es perd el sistema de fitxers (a “layer”) del contenidor.
Si volem, podem crear una nova imatge a partir del contenidor:
shell
$ docker container commit apache apache-dixitsha256:6509481cba1c9ba72c6288cbdd76d9477ca43ce69ddccfa6882323b6c9a301c0$ docker images apache-dixitREPOSITORY TAG IMAGE ID CREATED SIZEapache-dixit latest 6509481cba1c 30 seconds ago 147MB
Eliminem el contenidor apache i fem servir la imatge apache-dixit que hem creat per aixecar un nou contenidor:
shell
$ docker stop apacheapache$ docker run --rm -d --name apache -p 80:80 apache-dixitcf254be6de0e6ece208ae61aaf644a093fd077d0d5e2dd2c9c4df3c147ca6fc3$ curl localhostA poc a poc i bona lletra
A partir d’aquesta imatge es poden crear tants contendiors apache com vulguis que enlloc de respondre amb <html><body><h1>It works!</h1></body></html> responen amb A poc a poc i bona lletra.