In questo tutorial ti mostrerò uno script per installare Docker e Docker Compose su Ubuntu 22.04.
Oggigiorno sempre più applicazioni sono disponibili in container con Docker, mi capita spesso di dover installare nuove macchine su Ubuntu e di dover installare Docker e Docker Compose.
Personalmente preferisco installare Docker dai repository ufficiali piuttosto che usare la versione più recente offerta da Ubuntu.
All’inizio andavo ogni volta sul sito Docker e copiavo e incollavo righe di codice al suo interno, il che è piuttosto noioso.
Sommario
Installa Docker e Docker Compose con uno script
Per risparmiare tempo, ho deciso di utilizzare uno script bash che accetta i comandi ed eccolo:
Per utilizzare lo script, copiane il contenuto in un file sul tuo server con estensione .sh e quindi eseguilo
sudo sh script-file-name.sh
Lo script funziona anche con Ubuntu 20.04
Vérifier que Docker est installé
Pour vérifier que l’installation à bien fonctionner vous pouvez entrer ces commandes pour avoir les versions des composant de Docker.
docker --version
Pour Docker Compose
docker compose version
Elenco dei comandi disponibili
Docker
Per accedere ai comandi Docker, immettere il seguente comando:
docker
Cosa restituisce:
Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Common Commands: run Create and run a new container from an image exec Execute a command in a running container ps List containers build Build an image from a Dockerfile pull Download an image from a registry push Upload an image to a registry images List images login Log in to a registry logout Log out from a registry search Search Docker Hub for images version Show the Docker version information info Display system-wide information Management Commands: builder Manage builds buildx* Docker Buildx (Docker Inc., v0.11.2) checkpoint Manage checkpoints compose* Docker Compose (Docker Inc., v2.21.0) container Manage containers context Manage contexts image Manage images manifest Manage Docker image manifests and manifest lists network Manage networks plugin Manage plugins system Manage Docker trust Manage trust on Docker images volume Manage volumes Swarm Commands: config Manage Swarm configs node Manage Swarm nodes secret Manage Swarm secrets service Manage Swarm services stack Manage Swarm stacks swarm Manage Swarm Commands: attach Attach local standard input, output, and error streams to a running container commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server export Export a container's filesystem as a tar archive history Show the history of an image import Import the contents from a tarball to create a filesystem image inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images save Save one or more images to a tar archive (streamed to STDOUT by default) start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers wait Block until one or more containers stop, then print their exit codes Global Options: --config string Location of client config files (default "/home/ubuntu/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket to connect to -l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/home/ubuntu/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/home/ubuntu/.docker/cert.pem") --tlskey string Path to TLS key file (default "/home/ubuntu/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Run 'docker COMMAND --help' for more information on a command. For more help on how to use Docker, head to https://docs.docker.com/go/guides/
Docker compose
Per Docker Compose:
docker compose
Usage: docker compose [OPTIONS] COMMAND Define and run multi-container applications with Docker. Options: --ansi string Control when to print ANSI control characters ("never"|"always"|"auto") (default "auto") --compatibility Run compose in backward compatibility mode --dry-run Execute command in dry run mode --env-file stringArray Specify an alternate environment file. -f, --file stringArray Compose configuration files --parallel int Control max parallelism, -1 for unlimited (default -1) --profile stringArray Specify a profile to enable --progress string Set type of progress output (auto, tty, plain, quiet) (default "auto") --project-directory string Specify an alternate working directory (default: the path of the, first specified, Compose file) -p, --project-name string Project name Commands: build Build or rebuild services config Parse, resolve and render compose file in canonical format cp Copy files/folders between a service container and the local filesystem create Creates containers for a service. down Stop and remove containers, networks events Receive real time events from containers. exec Execute a command in a running container. images List images used by the created containers kill Force stop service containers. logs View output from containers ls List running compose projects pause Pause services port Print the public port for a port binding. ps List containers pull Pull service images push Push service images restart Restart service containers rm Removes stopped service containers run Run a one-off command on a service. start Start services stop Stop services top Display the running processes unpause Unpause services up Create and start containers version Show the Docker Compose version information wait Block until the first service container stops Run 'docker compose COMMAND --help' for more information on a command.
Prova Docker eseguendo un’immagine
Per verificare che Docker funzioni correttamente, puoi eseguire l’immagine hello-world
sudo docker run hello-world
L’immagine dovrebbe restituire qualcosa del genere:
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 719385e32844: Pull complete Digest: sha256:c79d06dfdfd3d3eb04cafd0dc2bacab0992ebc243e083cabe208bac4dd7759e0 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: https://docs.docker.com/get-started/