Ubuntu 24.04
Sponsored Link

Docker : Docker Compose का प्रयोग करें2024/06/13

 

Docker Compose को इंस्टॉल करने के लिए, Docker एप्लिकेशन के रूप में कई कंटेनरों को कॉन्फ़िगर करना और चलाना आसान है।

[1] Install Docker Compose.
root@dlp:~#
curl -L https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

root@dlp:~#
chmod 755 /usr/local/bin/docker-compose

root@dlp:~#
/usr/local/bin/docker-compose --version

Docker Compose version v2.27.0
[2] उदाहरण के लिए, Docker Compose के साथ एक एप्लिकेशन को कॉन्फ़िगर करें जिसमें वेब और DB सेवाएँ हैं।
# वेब सेवा कंटेनर को परिभाषित करें

root@dlp:~#
vi Dockerfile
FROM ubuntu
MAINTAINER ServerWorld <admin@srv.world>

ENV ubuntu_FRONTEND=noninteractive

RUN apt-get update
RUN apt-get -y install tzdata
RUN apt-get -y install apache2

EXPOSE 80
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

# एप्लिकेशन कॉन्फ़िगरेशन को परिभाषित करें

root@dlp:~#
vi docker-compose.yml
services:
  db:
    image: docker.io/library/mariadb
    volumes:
      - /var/lib/docker/disk01:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: ubuntu
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: ubuntu_db
    ports:
      - "3306:3306"
  web:
    build: .
    ports:
      - "80:80"
    volumes:
      - /var/lib/docker/disk02:/usr/share/nginx/html

# बनाएं और चलाएं

root@dlp:~#
/usr/local/bin/docker-compose up -d

[+] Running 9/9
 ✓ db Pulled                                                               7.8s
   ✓ 00d679a470c4 Already exists                                           0.0s
   ✓ 381fb89d9026 Pull complete                                            1.5s
   ✓ fd042176e576 Pull complete                                            1.8s
   ✓ 80b21b96f832 Pull complete                                            1.8s
   ✓ 310e39ef046d Pull complete                                            2.1s
   ✓ 8e4136144025 Pull complete                                            4.7s
   ✓ c187e9145a80 Pull complete                                            4.7s
   ✓ 7b8cfc1ced45 Pull complete                                            4.7s
[+] Building 22.0s (8/8) FINISHED                                docker:default
 => [web internal] load .dockerignore                                      0.1s
 => => transferring context: 2B                                            0.0s
 => [web internal] load build definition from Dockerfile                   0.1s
 => => transferring dockerfile: 268B                                       0.0s
 => [web internal] load metadata for docker.io/library/ubuntu:latest       0.0s
 => CACHED [web 1/4] FROM docker.io/library/ubuntu                         0.0s
 => [web 2/4] RUN apt-get update                                           5.1s
 => [web 3/4] RUN apt-get -y install tzdata                                2.7s
 => [web 4/4] RUN apt-get -y install apache2                              13.7s
 => [web] exporting to image                                               0.4s
 => => exporting layers                                                    0.4s
 => => writing image sha256:04dd1f9159cf877a6e105ca74c48d4a5dcd3f9ad1cfb8  0.0s
 => => naming to docker.io/library/root-web                                0.0s
[+] Running 3/3
 ✓ Network root_default  Created                                           0.1s
 ✓ Container root-web-1  Started                                           0.3s
 ✓ Container root-db-1   Started                                           0.4s

root@dlp:~#
docker ps

CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
fba9420393f3   mariadb    "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   root-db-1
adb1800e2691   root-web   "/usr/sbin/apachectl…"   3 minutes ago   Up 3 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp           root-web-1

# पहुंच सत्यापित करें

root@dlp:~#
mysql -h 127.0.0.1 -u root -p -e "show variables like 'hostname';"

Enter password:
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| hostname      | 51bcfccf5bbd |
+---------------+--------------+

root@dlp:~#
mysql -h 127.0.0.1 -u ubuntu -p -e "show databases;"

Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ubuntu_db          |
+--------------------+

root@dlp:~#
echo "Hello Docker Compose World" > /var/lib/docker/disk02/index.html

root@dlp:~#
curl 127.0.0.1

Hello Docker Compose World
[3] Docker Compose के अन्य बुनियादी संचालन इस प्रकार हैं।
# कंटेनरों की स्थिति सत्यापित करें

root@dlp:~#
/usr/local/bin/docker-compose ps

NAME         IMAGE                       COMMAND                  SERVICE   CREATED          STATUS          PORTS
root-db-1    docker.io/library/mariadb   "docker-entrypoint.s…"   db        49 seconds ago   Up 48 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
root-web-1   root-web                    "/usr/sbin/apachectl…"   web       49 seconds ago   Up 48 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp

# कंटेनरों के लॉग दिखाएं

root@dlp:~#
/usr/local/bin/docker-compose logs

db-1   | 2024-06-13 01:23:15+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.4.2+maria~ubu2404 started.
db-1   | 2024-06-13 01:23:15+00:00 [Warn] [Entrypoint]: /sys/fs/cgroup///memory.pressure not writable, functionality unavailable to MariaDB
db-1   | 2024-06-13 01:23:15+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db-1   | 2024-06-13 01:23:15+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.4.2+maria~ubu2404 started.
db-1   | 2024-06-13 01:23:15+00:00 [Note] [Entrypoint]: MariaDB upgrade not required

.....
.....

db-1   | 2024-06-13  1:23:17 0 [Note] mariadbd: Event Scheduler: Loaded 0 events
db-1   | 2024-06-13  1:23:17 0 [Note] mariadbd: ready for connections.
db-1   | Version: '11.4.2-MariaDB-ubu2404'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

# किसी कंटेनर के अंदर कोई भी कमांड चलाएँ
# कंटेनर नाम [docker-compose.yml] में केवल एक सेट है

root@dlp:~#
/usr/local/bin/docker-compose exec db /bin/bash

root@fba9420393f3:/#

# एप्लिकेशन बंद करें और सभी कंटेनर भी बंद करें

root@dlp:~#
/usr/local/bin/docker-compose stop

[+] Stopping 2/2
 ✓ Container root-web-1  Stopped                                          12.2s
 ✓ Container root-db-1   Stopped                                          12.2s

# एप्लिकेशन में अकेले एक सेवा प्रारंभ करें
# यदि निर्भरता सेट की जाती है, तो अन्य कंटेनर प्रारंभ हो जाता है

root@dlp:~#
/usr/local/bin/docker-compose up -d web

[+] Running 1/1
 ✓ Container root-web-1  Started                                           0.2s

root@dlp:~#
/usr/local/bin/docker-compose ps

NAME         IMAGE      COMMAND                  SERVICE   CREATED         STATUS          PORTS
root-web-1   root-web   "/usr/sbin/apachectl…"   web       5 minutes ago   Up 25 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp

# एप्लिकेशन में मौजूद सभी कंटेनर हटा दें
# यदि कोई कंटेनर चल रहा है, तो उसे हटाया नहीं जाएगा

root@dlp:~#
/usr/local/bin/docker-compose rm

? Going to remove root-db-1 Yes
[+] Removing 1/0e root-db-1 (y/N) y
 ✓ Container root-db-1  Removed                                            0.0s
मिलान सामग्री