GNU/Linux >> Linux Esercitazione >  >> Linux

Installa e usa Docker Compose con Docker su Ubuntu 22.04

Come installare Docker Compose con Docker su Ubuntu 22.04. Docker Compose è un semplice strumento che fornisce un modo per orchestrare più contenitori per lavorare insieme, rendendo la distribuzione utilizzando un yaml file.

In questa guida imparerai come installare Docker Compose e creare una nuova applicazione utilizzando Docker Compose su Ubuntu 22.04.

Prerequisiti

  1. Accesso SSH al tuo server
  2. Docker installato, guarda come installare Docker su Ubuntu 22.04 LTS.

Installa Docker Compose

Dopo aver installato Docker, puoi procedere con l'installazione di Docker Compose.

Qui installeremo l'ultima versione di Docker Compose dal repository GitHub ufficiale.

sudo curl -L https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Imposta le autorizzazioni corrette per il file scaricato.

sudo chmod +x /usr/local/bin/docker-compose

Verifica l'installazione utilizzando il comando seguente.

docker-compose --version
Output
Docker Compose version v2.5.0

Ora Docker Compose è installato correttamente e puoi iniziare a eseguire i container.

Usa Docker Compose

Docker Compose consente di utilizzare il file YAML per definire più applicazioni contenitore. Con il file YAML puoi eseguire, creare e configurare tutti i container.

Crea una directory di progetto e naviga all'interno di quella directory.

mkdir docker-project
cd docker-project 

Crea un file YAML. Questo è un esempio di base del file yaml per hello world .

sudo nano docker-compose.yml

Incolla i seguenti contenuti e salva il file.

version: '3.9'
services:
   hello-world:
      image:
         hello-world:latest

Premi Ctrl + X seguito da Y e Enter per salvare il file ed uscire.

Ora puoi eseguire il comando seguente per estrarre l'immagine della parola ciao da Docker Hub.

Potenzia la tua carriera di amministrazione Linux con un corso di formazione completato e ottieni il lavoro dei tuoi sogni.

docker-compose up

Riceverai un output simile a questo.

Output
[+] Running 2/2
 ⠿ hello-world Pulled                                                                   1.5s
   ⠿ 2db29710123e Pull complete                                                         0.4s
[+] Running 2/2
 ⠿ Network docker-project_default          Created                                      0.1s
 ⠿ Container docker-project-hello-world-1  Created                                      0.1s
Attaching to docker-project-hello-world-1
docker-project-hello-world-1  | 
docker-project-hello-world-1  | Hello from Docker!
docker-project-hello-world-1  | This message shows that your installation appears to be working correctly.
docker-project-hello-world-1  | 
docker-project-hello-world-1  | To generate this message, Docker took the following steps:
docker-project-hello-world-1  |  1. The Docker client contacted the Docker daemon.
docker-project-hello-world-1  |  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
docker-project-hello-world-1  |     (amd64)
docker-project-hello-world-1  |  3. The Docker daemon created a new container from that image which runs the
docker-project-hello-world-1  |     executable that produces the output you are currently reading.
docker-project-hello-world-1  |  4. The Docker daemon streamed that output to the Docker client, which sent it
docker-project-hello-world-1  |     to your terminal.
docker-project-hello-world-1  | 
docker-project-hello-world-1  | To try something more ambitious, you can run an Ubuntu container with:
docker-project-hello-world-1  |  $ docker run -it ubuntu bash
docker-project-hello-world-1  | 
docker-project-hello-world-1  | Share images, automate workflows, and more with a free Docker ID:
docker-project-hello-world-1  |  https://hub.docker.com/
docker-project-hello-world-1  | 
docker-project-hello-world-1  | For more examples and ideas, visit:
docker-project-hello-world-1  |  https://docs.docker.com/get-started/
docker-project-hello-world-1  | 
docker-project-hello-world-1 exited with code 0

Ora l'immagine hello-world viene estratta da Docker Hub e docker-compose crea un contenitore, collega ed esegue il programma.

Puoi vedere tutti i contenitori usando il seguente comando.

docker ps -a
Output
CONTAINER ID   IMAGE                COMMAND    CREATED         STATUS                     PORTS     NAMES
3a83d1a6be58   hello-world:latest   "/hello"   2 minutes ago   Exited (0) 2 minutes ago             docker-project-hello-world-1

Ora hai eseguito una distribuzione utilizzando Docker Compose.

Conclusione

Ora hai imparato come installare e utilizzare Docker Compose con Docker su Ubuntu 22.04.

Grazie per il tuo tempo. In caso di problemi o feedback, si prega di lasciare un commento qui sotto.


Linux
  1. Come installare e utilizzare Docker su Ubuntu 20.04

  2. Come installare e utilizzare Docker su Ubuntu 18.04

  3. Come installare e utilizzare Docker Compose su Ubuntu 18.04

  4. Come installare e utilizzare Docker in Ubuntu 20.04

  5. Come installare e utilizzare Docker Compose su CentOS

Come installare e configurare Laravel con Docker Compose su Ubuntu 22.04

Come installare e configurare Laravel con Docker Compose su Ubuntu 20.04

Come installare e utilizzare Docker su Ubuntu 20.04

Come installare e utilizzare Docker Compose su Debian 11

Installa WordPress con Docker su Ubuntu 20.04

Come installare e utilizzare Docker su AlmaLinux 8