GNU/Linux >> Belajar Linux >  >> Linux

Instal dan Gunakan Docker Compose dengan Docker di Ubuntu 22.04

Cara Instal Docker Compose dengan Docker di Ubuntu 22.04. Docker Compose adalah alat sederhana yang menyediakan cara untuk mengatur beberapa wadah untuk bekerja bersama yang membuat penerapan menggunakan yaml berkas.

Dalam panduan ini Anda akan mempelajari cara menginstal Docker compose dan membuat aplikasi baru menggunakan docker compose di Ubuntu 22.04.

Prasyarat

  1. Akses SSH ke server Anda
  2. Docker terinstal, lihat cara menginstal Docker di Ubuntu 22.04 LTS.

Instal Docker Compose

Setelah Anda menginstal Docker, Anda dapat melanjutkan untuk menginstal Docker Compose.

Di sini kita akan menginstal versi terbaru Docker Compose dari repositori resmi GitHub.

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

Siapkan izin yang benar untuk file yang diunduh.

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

Verifikasi instalasi menggunakan perintah berikut.

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

Sekarang Docker Compose berhasil diinstal dan Anda dapat mulai menjalankan container.

Gunakan Docker Compose

Docker Compose memungkinkan Anda menggunakan file YAML untuk mendefinisikan beberapa aplikasi container. Dengan file YAML, Anda dapat menjalankan, membangun, dan mengonfigurasi semua container.

Buat direktori proyek dan navigasikan ke dalam direktori itu.

mkdir docker-project
cd docker-project 

Buat file YAML. Ini adalah contoh dasar file yaml untuk hello world .

sudo nano docker-compose.yml

Tempel konten berikut dan simpan file.

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

Tekan Ctrl + X diikuti oleh Y dan Enter untuk menyimpan file dan keluar.

Sekarang Anda dapat menjalankan perintah berikut untuk menarik gambar hello word dari Docker Hub.

Tingkatkan Karir Administrasi Linux Anda dengan menyelesaikan kursus pelatihan dan dapatkan pekerjaan impian Anda.

docker-compose up

Anda akan menerima output yang mirip dengan ini.

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

Sekarang gambar hello-world ditarik dari Docker Hub dan docker-compose membuat wadah, melampirkan, dan menjalankan program.

Anda dapat melihat semua container menggunakan perintah berikut.

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

Sekarang Anda telah membuat penerapan menggunakan Docker Compose.

Kesimpulan

Sekarang Anda telah mempelajari cara menginstal dan menggunakan Docker Compose dengan Docker di Ubuntu 22.04.

Terima kasih atas waktunya. Jika Anda menghadapi masalah atau umpan balik, silakan tinggalkan komentar di bawah.


Linux
  1. Cara Menginstal dan Menggunakan Docker di Ubuntu 20.04

  2. Cara Menginstal dan Menggunakan Docker di Ubuntu 20.04

  3. Cara Menginstal dan Menggunakan Docker Compose di CentOS

  1. Cara Menginstal dan Menggunakan Docker di Ubuntu 18.04

  2. Cara Menginstal dan Menggunakan Docker Compose di Ubuntu 18.04

  3. Cara Menginstal dan Menggunakan Docker Compose di CentOS 7

  1. Cara Menginstal dan Menggunakan Docker Compose di Debian 9

  2. Cara Menginstal dan Menggunakan Docker Compose di Debian 10 Linux

  3. Instal dan Gunakan Docker Compose di CentOS 8