Docker Linux · 10 min read · Feb 05, 2026
Gestire i contenitori Linux con Docker su Ubuntu - Pagina 2
4.2 Creazione di immagini con un Dockerfile
Il Dockerfile utilizza un DSL di base con istruzioni per la creazione di immagini Docker. Utilizziamo quindi il comando docker build per costruire una nuova immagine dalle istruzioni nel Dockerfile. Il team di Docker, Inc., ha anche pubblicato un tutorial sul Dockerfile per aiutarti a imparare come costruire Dockerfile qui.
Ora creerò un semplice file docker che può essere ulteriormente migliorato in base alle esigenze dell’utente. Prima crea una directory supponiamo dir=kishore:
mkdir kishore
cd kishoreOra crea un Dockerfile come:
vi Dockerfile
& inserisci il contenuto come segue:
| FROM ubuntu:12.04 MAINTAINER Srijan Kishore <[email protected]> RUN apt-get -qq update RUN apt-get -qqy install apache2 |
Ora per aprire il contenitore personalizzato usa:
sudo docker build -t="srijan/custom1" .Produrrà il seguente output:
srijan@vboxtest:~/kishore$ sudo docker build -t="srijan/custom1" .
[sudo] password for srijan:
Sending build context to Docker daemon 2.56 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:12.04
Pulling repository ubuntu
ea7d6801c538: Download complete
511136ea3c5a: Download complete
65b7e9ccb809: Download complete
f8dd6bd14f58: Download complete
a343823119db: Download complete
---> ea7d6801c538
Step 1 : MAINTAINER Srijan Kishore <[email protected]>
---> Running in a6135f405eb4
---> be97c22efa82
Removing intermediate container a6135f405eb4
Step 2 : RUN apt-get -qq update
---> Running in b5681cd85ba8
---> cbc3a95de894
Removing intermediate container b5681cd85ba8
Step 3 : RUN apt-get -qqy install apache2
---> Running in 5765c09b530f
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libsqlite3-0.
(Reading database ... 7551 files and directories currently installed.)
Unpacking libsqlite3-0 (from .../libsqlite3-0_3.7.9-2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libroken18-heimdal.
Unpacking libroken18-heimdal (from .../libroken18-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libasn1-8-heimdal.
Unpacking libasn1-8-heimdal (from .../libasn1-8-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libgpg-error0.
Unpacking libgpg-error0 (from .../libgpg-error0_1.10-2ubuntu1_amd64.deb) ...
Selecting previously unselected package libgcrypt11.
Unpacking libgcrypt11 (from .../libgcrypt11_1.5.0-3ubuntu0.2_amd64.deb) ...
Selecting previously unselected package libgdbm3.
Unpacking libgdbm3 (from .../libgdbm3_1.8.3-10_amd64.deb) ...
Selecting previously unselected package libp11-kit0.
Unpacking libp11-kit0 (from .../libp11-kit0_0.12-2ubuntu1_amd64.deb) ...
Selecting previously unselected package libtasn1-3.
Unpacking libtasn1-3 (from .../libtasn1-3_2.10-1ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libgnutls26.
Unpacking libgnutls26 (from .../libgnutls26_2.12.14-5ubuntu3.8_amd64.deb) ...
Selecting previously unselected package libhcrypto4-heimdal.
Unpacking libhcrypto4-heimdal (from .../libhcrypto4-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libheimbase1-heimdal.
Unpacking libheimbase1-heimdal (from .../libheimbase1-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libwind0-heimdal.
Unpacking libwind0-heimdal (from .../libwind0-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libhx509-5-heimdal.
Unpacking libhx509-5-heimdal (from .../libhx509-5-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libkrb5-26-heimdal.
Unpacking libkrb5-26-heimdal (from .../libkrb5-26-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libheimntlm0-heimdal.
Unpacking libheimntlm0-heimdal (from .../libheimntlm0-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libgssapi3-heimdal.
Unpacking libgssapi3-heimdal (from .../libgssapi3-heimdal_1.6~git20120311.dfsg.1-2ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libsasl2-2.
Unpacking libsasl2-2 (from .../libsasl2-2_2.1.25.dfsg1-3ubuntu0.1_amd64.deb) ...
Selecting previously unselected package libldap-2.4-2.
Unpacking libldap-2.4-2 (from .../libldap-2.4-2_2.4.28-1.1ubuntu4.4_amd64.deb) ...
Selecting previously unselected package libcap2.
Unpacking libcap2 (from .../libcap2_1%3a2.22-1ubuntu3_amd64.deb) ...
Selecting previously unselected package libexpat1.
Unpacking libexpat1 (from .../libexpat1_2.0.1-7.2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libmagic1.
Unpacking libmagic1 (from .../libmagic1_5.09-2ubuntu0.3_amd64.deb) ...
Selecting previously unselected package file.
Unpacking file (from .../file_5.09-2ubuntu0.3_amd64.deb) ...
Selecting previously unselected package mime-support.
Unpacking mime-support (from .../mime-support_3.51-1ubuntu1_all.deb) ...
Selecting previously unselected package netbase.
Unpacking netbase (from .../netbase_4.47ubuntu1_all.deb) ...
Selecting previously unselected package libsasl2-modules.
Unpacking libsasl2-modules (from .../libsasl2-modules_2.1.25.dfsg-3ubuntu0.1_amd64.deb) ...
Selecting previously unselected package openssl.
Unpacking openssl (from .../openssl_1.0.1-4ubuntu5.16_amd64.deb) ...
Selecting previously unselected package libapr1.
Unpacking libapr1 (from .../libapr1_1.4.6-1_amd64.deb) ...
Selecting previously unselected package libaprutil1.
Unpacking libaprutil1 (from .../libaprutil1_1.3.12+dfsg-3_amd64.deb) ...
Selecting previously unselected package libaprutil1-dbd-sqlite3.
Unpacking libaprutil1-dbd-sqlite3 (from .../libaprutil1-dbd-sqlite3_1.3.12+dfsg-3_amd64.deb) ...
Selecting previously unselected package libaprutil1-ldap.
Unpacking libaprutil1-ldap (from .../libaprutil1-ldap_1.3.12+dfsg-3_amd64.deb) ...
Selecting previously unselected package apache2.2-bin.
Unpacking apache2.2-bin (from .../apache2.2-bin_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package apache2-utils.
Unpacking apache2-utils (from .../apache2-utils_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package libswitch-perl.
Unpacking libswitch-perl (from .../libswitch-perl_2.16-2_all.deb) ...
Selecting previously unselected package libclass-isa-perl.
Unpacking libclass-isa-perl (from .../libclass-isa-perl_0.36-3_all.deb) ...
Selecting previously unselected package perl-modules.
Unpacking perl-modules (from .../perl-modules_5.14.2-6ubuntu2.4_all.deb) ...
Selecting previously unselected package perl.
Unpacking perl (from .../perl_5.14.2-6ubuntu2.4_amd64.deb) ...
Selecting previously unselected package apache2.2-common.
Unpacking apache2.2-common (from .../apache2.2-common_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package apache2-mpm-worker.
Unpacking apache2-mpm-worker (from .../apache2-mpm-worker_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package apache2.
Unpacking apache2 (from .../apache2_2.2.22-1ubuntu1.6_amd64.deb) ...
Selecting previously unselected package ssl-cert.
Unpacking ssl-cert (from .../ssl-cert_1.0.28ubuntu0.1_all.deb) ...
Setting up libsqlite3-0 (3.7.9-2ubuntu1.1) ...
Setting up libroken18-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libasn1-8-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libgpg-error0 (1.10-2ubuntu1) ...
Setting up libgcrypt11 (1.5.0-3ubuntu0.2) ...
Setting up libgdbm3 (1.8.3-10) ...
Setting up libp11-kit0 (0.12-2ubuntu1) ...
Setting up libtasn1-3 (2.10-1ubuntu1.1) ...
Setting up libgnutls26 (2.12.14-5ubuntu3.8) ...
Setting up libhcrypto4-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libheimbase1-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libwind0-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libhx509-5-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libkrb5-26-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libheimntlm0-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libgssapi3-heimdal (1.6~git20120311.dfsg.1-2ubuntu0.1) ...
Setting up libsasl2-2 (2.1.25.dfsg1-3ubuntu0.1) ...
Setting up libldap-2.4-2 (2.4.28-1.1ubuntu4.4) ...
Setting up libcap2 (1:2.22-1ubuntu3) ...
Setting up libexpat1 (2.0.1-7.2ubuntu1.1) ...
Setting up libmagic1 (5.09-2ubuntu0.3) ...
Setting up file (5.09-2ubuntu0.3) ...
Setting up mime-support (3.51-1ubuntu1) ...
update-alternatives: using /usr/bin/see to provide /usr/bin/view (view) in auto mode.
Setting up netbase (4.47ubuntu1) ...
Setting up libsasl2-modules (2.1.25.dfsg1-3ubuntu0.1) ...
Setting up openssl (1.0.1-4ubuntu5.16) ...
Setting up libapr1 (1.4.6-1) ...
Setting up libaprutil1 (1.3.12+dfsg-3) ...
Setting up libaprutil1-dbd-sqlite3 (1.3.12+dfsg-3) ...
Setting up libaprutil1-ldap (1.3.12+dfsg-3) ...
Setting up apache2.2-bin (2.2.22-1ubuntu1.6) ...
Setting up apache2-utils (2.2.22-1ubuntu1.6) ...
Setting up libclass-isa-perl (0.36-3) ...
Setting up ssl-cert (1.0.28ubuntu0.1) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
Setting up libswitch-perl (2.16-2) ...
Setting up perl-modules (5.14.2-6ubuntu2.4) ...
Setting up perl (5.14.2-6ubuntu2.4) ...
update-alternatives: using /usr/bin/prename to provide /usr/bin/rename (rename) in auto mode.
Setting up apache2.2-common (2.2.22-1ubuntu1.6) ...
Enabling site default.
Enabling module alias.
Enabling module autoindex.
Enabling module dir.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module status.
Enabling module auth_basic.
Enabling module deflate.
Enabling module authz_default.
Enabling module authz_user.
Enabling module authz_groupfile.
Enabling module authn_file.
Enabling module authz_host.
Enabling module reqtimeout.
Setting up apache2-mpm-worker (2.2.22-1ubuntu1.6) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up apache2 (2.2.22-1ubuntu1.6) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
---> 634855a43331
Removing intermediate container 5765c09b530f
Successfully built 634855a43331
srijan@vboxtest:~/kishore$Ora diamo un’occhiata alla nostra nuova immagine. Possiamo farlo utilizzando il comando docker images.
docker images srijan/custom1srijan@vboxtest:~/kishore$ docker images srijan/custom1
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
srijan/custom1 latest 634855a43331 About a minute ago 277.4 MB
srijan@vboxtest:~/kishore$Qui troverai che le immagini avranno per impostazione predefinita apache2 installato. Verifichiamolo come:
docker run -t -i srijan/apache2:webserver /bin/bashOra entrerai nel nuovo contenitore con apache2 preinstallato con 12.04ubuntu.
5 Cambiare hostname
Supponiamo che tu voglia cambiare l’hostname o desideri un hostname personalizzato, ad esempio nel mio caso server1.example.com.
Userò
docker run -h 'server1.example.com' -t -i srijan/custom1 /bin/bashProdurrà il contenitore dell’immagine come
srijan@vboxtest:~/kishore$ docker run -h 'server1.example.com' -t -i srijan/custom1 /bin/bash
root@server1:/# hostname -f
server1.example.com
root@server1:/#6 Comandi utili per docker
- pull (pull è usato per scaricare un’immagine o un repository dal registro)
e.g.
docker run -i -t ubuntu /bin/bash- commit (commit è usato per salvare il contenitore)
e.g.
docker commit 73527b8b4261 srijan/apache2 8ce0ea7a1528- cp (Copia file/cartelle dal filesystem di un contenitore al percorso host. I percorsi sono relativi alla radice del filesystem.)
e.g.
docker cp CONTAINER:PATH HOSTPATHdove CONTAINER è il contenitore & copia file/cartelle dal PATH al HOSTPATH
- start e stop un contenitore:
docker start 4386fb97867ddocker stop 4386fb97867ddove 4386fb97867d è il numero del tuo contenitore
- export (Esporta i contenuti di un filesystem come un archivio tar su STDOUT)
docker export 4386fb97867d > latest.tar- import (Crea un’immagine di filesystem vuota e importa i contenuti del tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) in essa, quindi facoltativamente etichettala.)
docker import http://example.com/exampleimage.tgzImporta da un file locale:
Importa in docker tramite pipe e stdin.
cat exampleimage.tgz | sudo docker import - exampleimagelocal:newImporta da una directory locale:
sudo tar -c . | sudo docker import - exampleimagedir
- history (Mostra la cronologia di un’immagine)
docker history [OPTIONS] IMAGEsudo docker history ea7d6801c538- images (Mostrerà le immagini)
docker images [OPTIONS] [NAME]Segue alcune opzioni come
-a, –all=false Mostra tutte le immagini (per impostazione predefinita filtra i livelli di immagine intermedi)
-f, –filter=[]: Fornisci valori di filtro (cioè ‘dangling=true’)
–no-trunc=false Non troncare l’output
-q, –quiet=false Mostra solo ID numerici
- info (Visualizza informazioni a livello di sistema)
srijan@VE130214:~$ sudo docker infosrijan@vboxtest:~$ sudo docker info
Containers: 20
Images: 65
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Dirs: 105
Execution Driver: native-0.2
Kernel Version: 3.13.0-30-generic
WARNING: No swap limit support- inspect (Restituisce informazioni a basso livello su un contenitore/imagine)
docker inspect CONTAINER|IMAGE [CONTAINER|IMAGE...]- kill (Uccidi un contenitore in esecuzione/invia SIGKILL, o segnale specificato)
docker kill [OPTIONS] CONTAINER [CONTAINER...]- login (Registrati o accedi a un server di registro docker, se non viene specificato alcun server “ https://index.docker.io/v1/ “ è il predefinito.)
docker login localhost:8080
Effettuerà il login a un registro auto-ospitato.
- logs (Recupera i log di un contenitore)
docker logs CONTAINER- ps (Elenca i contenitori)
docker ps [OPTIONS]Ha opzioni come:
-a, –all=false Mostra tutti i contenitori. Solo i contenitori in esecuzione sono mostrati per impostazione predefinita.
–before=”” Mostra solo il contenitore creato prima di Id o Nome, includendo quelli non in esecuzione.
-l, –latest=false Mostra solo l’ultimo contenitore creato, includendo quelli non in esecuzione.
-n=-1 Mostra gli n ultimi contenitori creati, includendo quelli non in esecuzione.
–no-trunc=false Non troncare l’output
-q, –quiet=false Mostra solo ID numerici
-s, –size=false Mostra le dimensioni
–since=”” Mostra solo i contenitori creati da Id o Nome, includendo quelli non in esecuzione.
- push (Invia un’immagine o un repository al registro)
docker push NAME[:TAG]- restart (Riavvierà un contenitore in esecuzione)
docker restart [OPTIONS] CONTAINER [CONTAINER...]- rm (Rimuoverà uno o più contenitori)
docker rm [OPTIONS] CONTAINER [CONTAINER...]- rmi (Rimuoverà uno o più immagini)
docker rmi IMAGE [IMAGE...]- run (Esegui un comando in un nuovo contenitore)
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]Ha le seguenti opzioni:
-a, –attach=[] Attacca a stdin, stdout o stderr.
-c, –cpu-shares=0 CPU shares (peso relativo)
–cidfile=”” Scrivi l’ID del contenitore nel file
-d, –detach=false Modalità distaccata: Esegui il contenitore in background, stampa il nuovo ID del contenitore
–dns=[] Imposta server dns personalizzati
–dns-search=[] Imposta domini di ricerca dns personalizzati
-e, –env=[] Imposta variabili di ambiente
–entrypoint=”” Sovrascrivi il punto di ingresso predefinito dell’immagine
–env-file=[] Leggi un file delimitato da linee di variabili ENV
–expose=[] Espone una porta dal contenitore senza pubblicarla sul tuo host
-h, –hostname=”” Nome host del contenitore
-i, –interactive=false Mantieni stdin aperto anche se non attaccato
–link=[] Aggiungi un link a un altro contenitore (nome:alias)
–lxc-conf=[] (solo driver di esecuzione lxc) Aggiungi opzioni lxc personalizzate –lxc-conf=”lxc.cgroup.cpuset.cpus = 0,1”
-m, –memory=”” Limite di memoria (formato:
–name=”” Assegna un nome al contenitore
–net=”bridge” Imposta la modalità di rete per il contenitore
‘bridge’: crea un nuovo stack di rete per il contenitore sul ponte docker
‘none’: nessuna rete per questo contenitore
‘container:
‘host’: utilizza lo stack di rete dell’host all’interno del contenitore
-p, –publish=[] Pubblica la porta di un contenitore sull’host
formato: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort
(usa ‘docker port’ per vedere il mapping effettivo)
-P, –publish-all=false Pubblica tutte le porte esposte sulle interfacce host
–privileged=false Dai privilegi estesi a questo contenitore
–rm=false Rimuovi automaticamente il contenitore quando esce (incompatibile con -d)
–sig-proxy=true Proxifica tutti i segnali ricevuti al processo (anche in modalità non tty)
-t, –tty=false Assegna un pseudo-tty
-u, –user=”” Nome utente o UID
-v, –volume=[] Monta un volume (ad es. dall’host: -v /host:/container, da docker: -v /container)
–volumes-from=[] Monta volumi dai contenitori specificati
-w, –workdir=”” Directory di lavoro all’interno del contenitore
- save (Salva un’immagine in un archivio tar (streamed to stdout per impostazione predefinita)
docker save IMAGE- search (Cerca nell’indice docker per immagini)
docker search TERM- tag (Etichetta un’immagine in un repository)
docker tag [OPTIONS] IMAGE [REGISTRYHOST/][USERNAME/]NAME[:TAG]- top (Cerca i processi in esecuzione di un contenitore)
docker top CONTAINER [ps OPTIONS]- version (Mostra le informazioni sulla versione di docker.)
sudo docker versionsrijan@vboxtest:~$ sudo docker version
[sudo] password for srijan:
Client version: 1.0.1
Client API version: 1.12
Go version (client): go1.2.1
Git commit (client): 990021a
Server version: 1.0.1
Server API version: 1.12
Go version (server): go1.2.1
Git commit (server): 990021a6 Riepilogo
Ho cercato di farti familiarizzare di più con docker, spero che questa guida ti aiuti a comprendere docker e a utilizzare docker nei tuoi ambienti di test/produzione.
Per ulteriori informazioni fare riferimento a http://www.docker.com/
Ricevi i nuovi post nella tua casella di posta.
Nessuno spam. Disiscriviti in qualsiasi momento.