{"id":274,"date":"2023-10-10T10:30:38","date_gmt":"2023-10-10T08:30:38","guid":{"rendered":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/?page_id=274"},"modified":"2025-02-11T14:26:12","modified_gmt":"2025-02-11T12:26:12","slug":"2-4-eliminar-imatges-i-contenidors-docker","status":"publish","type":"page","link":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/2-4-eliminar-imatges-i-contenidors-docker\/","title":{"rendered":"2.4. Eliminar imatges i contenidors Docker"},"content":{"rendered":"<p>Sempre \u00e9s recomanable mantenir una bona gesti\u00f3 d\u2019imatges i contenidors, ja que, si no s\u2019utilitzen, van ocupant espai de manera innecess\u00e0ria. Aix\u00ed, aprendrem a eliminar imatges i contenidors.<\/p>\n<p>Per poder eliminar imatges, necessitarem saber la Image ID. Per a aix\u00f2, utilitzarem la seg\u00fcent ordre:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker image ls<\/pre>\n<p>Ens apareixer\u00e0 un <em>output<\/em> similar al seg\u00fcent:<\/p>\n<div class=\"tabletitle\"><p>Taula 1.<\/p>\n<\/div>\n<table width=\"642\">\n<tbody>\n<tr class=\"table-header\">\n<td width=\"128\"><strong>REPOSITORY\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><\/td>\n<td width=\"128\"><strong>TAG<\/strong><\/td>\n<td width=\"128\"><strong>IMAGE ID\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><\/td>\n<td width=\"128\"><strong>CREATED\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><\/td>\n<td width=\"128\"><strong>SIZE<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"128\">alpine<\/td>\n<td width=\"128\">latest<\/td>\n<td width=\"128\">9ed4aefc74f6<\/td>\n<td width=\"128\">10 days ago<\/td>\n<td width=\"128\">7.05MB<\/td>\n<\/tr>\n<tr>\n<td width=\"128\">hello-world<\/td>\n<td width=\"128\">latest<\/td>\n<td width=\"128\">feb5d9fea6a5<\/td>\n<td width=\"128\">18 months ago<\/td>\n<td width=\"128\">13.3kB<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"tablefooter\"><p>Font: elaboraci\u00f3 pr\u00f2pia.<\/p>\n<\/div>\n<p>Aix\u00ed, per eliminar una imatge simplement s\u2019ha d\u2019especificar la Image ID:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker image rm feb5d9fea6a5<\/pre>\n<p>O emprant el seu nom:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker image rm hello-world<\/pre>\n<p>Moltes vegades ens podem trobar amb un missatge d\u2019error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"droide\">Error response from daemon: conflict: unable to delete feb5d9fea6a5 (must be forced) \u2013 image is being used by stopped container 06dda9650983<\/pre>\n<p>Aix\u00f2 ens est\u00e0 indicant que hi ha un contenidor que encara est\u00e0 actiu, que encara no ha estat netejat o que est\u00e0 o ha fet \u00fas d&#8217;aquesta imatge. Per aix\u00f2, primer hem d\u2019eliminar els contenidors que estan impedint l\u2019eliminaci\u00f3 d\u2019aquesta imatge.<\/p>\n<p>Primer llistem els contenidors actius:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker container ls<\/pre>\n<p>O, de manera similar, podem utilitzar:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker ps<\/pre>\n<p>\u00c9s possible que no ens retorni cap contenidor actiu. En realitat, el missatge d\u2019error previ ens indicava que el contenidor s\u2019havia aturat; per tant, t\u00e9 sentit que no trobem el contenidor en aquest llistat. Aix\u00ed, necessitarem veure tant els contenidors actius com els que recentment s\u2019han aturat:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker container ls \u2013all<\/pre>\n<p>Aqu\u00ed s\u00ed que podem veure el contenidor anteriorment especificat com a enlla\u00e7at a \u00abhello-world\u00bb:<\/p>\n<div class=\"tabletitle\"><p>Taula 2.<\/p>\n<\/div>\n<table width=\"642\">\n<tbody>\n<tr class=\"table-header\">\n<td width=\"108\"><strong>CONTAINER ID\u00a0\u00a0 <\/strong><\/td>\n<td width=\"80\"><strong>IMAGE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><\/td>\n<td width=\"85\"><strong>COMMAND\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><\/td>\n<td width=\"122\"><strong>CREATED\u00a0 <\/strong><\/td>\n<td width=\"66\"><strong>STATUS <\/strong><\/td>\n<td width=\"58\"><strong>PORTS\u00a0 <\/strong><\/td>\n<td width=\"122\"><strong>NAMES<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"108\">8a149fe84874<\/td>\n<td width=\"80\">alpine<\/td>\n<td width=\"85\">&#8220;sh&#8221;<\/td>\n<td width=\"122\">About an hour ago<\/td>\n<td width=\"66\">Exited<\/td>\n<td width=\"58\"><\/td>\n<td width=\"122\">xenodochial_thompson<\/td>\n<\/tr>\n<tr>\n<td width=\"108\">06dda9650983<\/td>\n<td width=\"80\">hello-world<\/td>\n<td width=\"85\">&#8220;\/hello&#8221;<\/td>\n<td width=\"122\">12 hours ago<\/td>\n<td width=\"66\">Exited<\/td>\n<td width=\"58\"><\/td>\n<td width=\"122\">upbeat_satoshi<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"tablefooter\"><p>Font: elaboraci\u00f3 pr\u00f2pia.<\/p>\n<\/div>\n<p>Per evitar que es vagin acumulant els contenidors, es pot afegir a l\u2019ordre <code>run<\/code> una opci\u00f3 d\u2019eliminar autom\u00e0ticament un cop finalitzat:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker container run --rm hello-world<\/pre>\n<p>Si no hem especificat l\u2019opci\u00f3 <code>rm<\/code>, l\u2019hem d\u2019eliminar manualment utilitzant com a refer\u00e8ncia el Container ID:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker container rm 06dda9650983<\/pre>\n<p>Si s\u2019elimina correctament, ens retornar\u00e0 el Container ID al terminal. Es pot esborrar m\u00e9s d\u2019un Container ID simult\u00e0niament.<\/p>\n<p>Si tenim diversos contenidors associats a una imatge i volem esborrar-los tots utilitzant un patr\u00f3 podem:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker container ps -a | grep \"world\" | awk '{print $1}' | xargs docker rm<\/pre>\n<p>En aquest cas, utilitzem l\u2019ordre <code>xargs<\/code> per controlar una llista d\u2019arguments per poder ser eliminats mitjan\u00e7ant <code>rm<\/code> de Docker.<\/p>\n<p>Ara veurem que, quan llistem els contenidors, ja no apareix el contenidor i, si provem d\u2019eliminar la imatge com pr\u00e8viament hav\u00edem intentat, ho far\u00e0 sense problemes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker image rm hello-world<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker image ls<\/pre>\n<p>Si es volen eliminar tots els contenidors existents, podem utilitzar:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"mowtwo\">$ docker container prune<\/pre>\n<p>D\u2019aquesta manera tots els contenidors existents seran eliminats i, si volem tornar a reconnectar-los, haurem d\u2019iniciar-los de nou.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sempre \u00e9s recomanable mantenir una bona gesti\u00f3 d\u2019imatges i contenidors, ja que, si no s\u2019utilitzen, van ocupant espai de manera innecess\u00e0ria. Aix\u00ed, aprendrem a eliminar imatges i contenidors. Per poder eliminar imatges, necessitarem saber la Image ID. Per a aix\u00f2, utilitzarem la seg\u00fcent ordre: $ docker image ls Ens apareixer\u00e0 un output similar al seg\u00fcent: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"acf":[],"_links":{"self":[{"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/wp-json\/wp\/v2\/pages\/274"}],"collection":[{"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/wp-json\/wp\/v2\/comments?post=274"}],"version-history":[{"count":9,"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/wp-json\/wp\/v2\/pages\/274\/revisions"}],"predecessor-version":[{"id":448,"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/wp-json\/wp\/v2\/pages\/274\/revisions\/448"}],"wp:attachment":[{"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/entornos-y-contenedores\/wp-json\/wp\/v2\/media?parent=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}