Docker application container

Spread the love
docker
docker

Docker è un gestore di container, un sistema che consente di gestire degli ambienti di lavoro isolati per sviluppare applicazioni.

Ciò che mi ha spinto ad utilizzare Docker è l’urgenza di confinare un’applicazione che sto scrivendo in un ambiente stabile immune dagli avanzamenti di versione a cui devo sottoporre regolarmente il sistema operativo (Ubuntu Linux) della mia macchina (HP ProBook 440G).

Ho bisogno di una sandbox che rimanga stabile per tutto lo sviluppo dell’applicazione.

Questa prassi avrà poi un altro risvolto positivo, quello di poter trasferire (“deployare”) la sandbox così com’è nell’ambiente di collaudo o di produzione, e rendere così le operazioni CI/CD molto rapide e indolori.

Istruzioni per installare Docker su una macchina Ubuntu.

Passo 1: installazione

Seguo la guida del sito [1]; non funziona il comando com’è scritto, ma il suggerimento che danno poco prima, quello di studiarsi prima lo script, è illuminante: lo eseguo da root una riga alla volta:

# Executing docker install script, commit: b2e29ef7a9a89840d2333637f7d1900a83e7153f
apt-get update -qq >/dev/null
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
mkdir -p /etc/apt/keyrings && chmod -R 0755 /etc/apt/keyrings
curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable" > /etc/apt/sources.list.d/docker.list
apt-get update -qq >/dev/null
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-scan-plugin >/dev/null
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce-rootless-extras >/dev/null

e riesco a terminare.

Passo 2: post-installazione: usare Docker come non root user

Eseguire questi comandi se si vuole agire da utente normale (vedi i drawbacks in [2])

$ sudo groupadd docker
$ sudo usermod -aG docker $USER
$ cat /etc/group | grep docker
docker:x:999:

Passo 3: test installazione

Digitare

$ sudo docker run hello-world

Output

[sudo] password di marcob: 
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:

Utilizzando il package Manager di Ubuntu

Provo anche a installare con il gestore di pacchetti APT.

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

e verifico:

$ sudo docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Un po’ più complicato: una installazione Ubuntu intera

Ho bisognno di un container con una versione di Ubuntu precedeente a quella della mia macchina.

L’esempio seguente fa al caso mio. Ovviamente dovrò approfondire, ma mi pare di essere sulla buona strada.

$ sudo docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
405f018f9d1d: Pull complete 
Digest: sha256:b6b83d3c331794420340093eb706a6f152d9c1fa51b262d9bf34594887c2c7ac
Status: Downloaded newer image for ubuntu:latest
root@9d0779c49ba7:/# whoami
root
root@9d0779c49ba7:/# hostname
9d0779c49ba7
root@9d0779c49ba7:/# pwd
/
root@9d0779c49ba7:/# cd home
root@9d0779c49ba7:/home# ll
total 8
drwxr-xr-x 2 root root 4096 Apr 18 10:28 ./
drwxr-xr-x 1 root root 4096 Jun 17 13:16 ../

La prossima sfida è installare una release di Ubuntu leggermente più vecchia (la 21.04 Hirsute Hippo), Python, GDAL, QGIS e PostgrSQL.

Problema di conflitto degli indirizzi di rete

Per impostazione predefinita, il sistema di virtualizzazione Docker utilizza le reti 172.17.0.0/12 per il suo funzionamento. Se queste impostazioni creano conflitti nelle reti a cui dobbiamo connetterci, occorre cambiare questo indirizzo e per fare questo si possono modificare le impostazioni.

Per vedere quale rete sta usando il tuo demone, lista la confgurazione delle interfacce di rete:

$ ifconfig 
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:13ff:fe52:d90  prefixlen 64  scopeid 0x20<link>
        ether 02:42:13:52:0d:90  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3  bytes 306 (306.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
...

Io sto lavorando in una rete aziendale che ha una sottorete esattamente all’indirizzo 172.17.0. Non riesco più a raggiungere le macchine di questa sottorete…

Quindi devo poter cambiare la configurazione di docker.

Step 1 creare un file di configurazione

Alla posizione

/etc/docker

creare il file daemon.js

$ touch daemon.js 

con questo contenuto:

{
	"live-restore": true,
	"bip": "10.10.0.1/16",
	"default-address-pools": [{
		"base": "10.0.0.0/8",
		"size": 16
	}]
}

Sostitutire gli indirizzi IP a seconda del bisogno (in questa rete mi vanno bene questi indirizzi),

Riavviare il demone

$ sudo service docker restart

Ripetendo il comando:

$ ifconfig 
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 10.10.0.1  netmask 255.255.0.0  broadcast 10.10.255.255
        inet6 fe80::42:13ff:fe52:d90  prefixlen 64  scopeid 0x20<link>
        ether 02:42:13:52:0d:90  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3  bytes 306 (306.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

e riesco ad accedere alla sottorete nella quale devo lavorare.

Risorse

  1. Installazione di Docker
  2. Post-installazione
  3. Networking
  4. Dockerizing Python apps

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.