Fedora 40
Sponsored Link

Podman : Use Docker Compose2024/05/03

 
To Install Docker Compose, it's easy to configure and run multiple containers as a Docker application.
[1]
[2] Install Docker Compose.
[root@dlp ~]#
dnf -y install docker-compose
[3] For example, Configure an application that has Web and DB services with Docker Compose.
# start podman.socket

[root@dlp ~]#
systemctl start podman.socket
# define Web service container

[root@dlp ~]#
vi Dockerfile
FROM fedora
MAINTAINER ServerWorld <admin@srv.world>

RUN dnf -y install nginx

EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]

# define application configuration

[root@dlp ~]#
vi docker-compose.yml
version: '3.8'
services:
  db:
    image: docker.io/library/mariadb
    volumes:
      - /var/lib/containers/disk01:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: fedora
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: fedora_db
    user: 0:0
    privileged: true
    ports:
      - "3306:3306"
  web:
    build: .
    ports:
      - "80:80"
    volumes:
      - /var/lib/containers/disk02:/usr/share/nginx/html
    privileged: true

# buid and run

[root@dlp ~]#
docker-compose up -d

Creating network "root_default" with the default driver
Pulling db (docker.io/library/mariadb:)...
Building web
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
STEP 1/5: FROM fedora
.....
.....
Creating root_db_1  ... done
Creating root_web_1 ... done

[root@dlp ~]#
podman ps

CONTAINER ID  IMAGE                             COMMAND               CREATED        STATUS        PORTS                   NAMES
1a00065423b8  localhost/root_web:latest         /usr/sbin/nginx -...  3 seconds ago  Up 4 seconds  0.0.0.0:80->80/tcp      root_web_1
27e996781150  docker.io/library/mariadb:latest  mariadbd              3 seconds ago  Up 4 seconds  0.0.0.0:3306->3306/tcp  root_db_1

# verify accesses

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

Enter password:
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| hostname      | 27e996781150 |
+---------------+--------------+

[root@dlp ~]#
mysql -h 127.0.0.1 -u fedora -p -e "show databases;"

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

[root@dlp ~]#
echo "Hello Docker Compose World" > /var/lib/containers/disk02/index.html

[root@dlp ~]#
curl 127.0.0.1

Hello Docker Compose World
[4] Other basic operations of Docker Compose are follows.
# verify state of containers

[root@dlp ~]#
docker-compose ps

   Name                 Command               State           Ports
----------------------------------------------------------------------------
root_db_1    docker-entrypoint.sh mariadbd    Up      0.0.0.0:3306->3306/tcp
root_web_1   /usr/sbin/nginx -g daemon off;   Up      0.0.0.0:80->80/tcp

# show logs of containers

[root@dlp ~]#
docker-compose logs

Attaching to root_web_1, root_db_1
db_1   | 2024-05-03 08:04:35+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.3.2+maria~ubu2204 started.
db_1   | 2024-05-03 08:04:36+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1   | 2024-05-03 08:04:36+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.3.2+maria~ubu2204 started.
db_1   | 2024-05-03 08:04:36+00:00 [Note] [Entrypoint]: MariaDB upgrade not required
db_1   | 2024-05-03  8:04:36 0 [Note] Starting MariaDB 11.3.2-MariaDB-1:11.3.2+maria~ubu2204 source revision 068a6819eb63bcb01fdfa037c9bf3bf63c33ee42 as process 1
.....
.....
db_1   | 2024-05-03  8:04:36 0 [Note] mariadbd: Event Scheduler: Loaded 0 events
db_1   | 2024-05-03  8:04:36 0 [Note] mariadbd: ready for connections.
db_1   | Version: '11.3.2-MariaDB-1:11.3.2+maria~ubu2204'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

# run any commands inside a container
# container name is just the one set in [docker-compose.yml]

[root@dlp ~]#
docker-compose exec db /bin/bash

Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
root@27e996781150:/#

# stop application and also shutdown all containers

[root@dlp ~]#
docker-compose stop

Stopping root_db_1  ...
Stopping root_web_1 ...
Stopping root_web_1 ... done
Stopping root_db_1  ... done

# start a service alone in application
# if set dependency, other container starts

[root@dlp ~]#
docker-compose up -d web

Starting root_web_1 ... done
[root@dlp ~]#
docker-compose ps

   Name                 Command               State            Ports
-----------------------------------------------------------------------------
root_db_1    docker-entrypoint.sh mariadbd    Exit 0   0.0.0.0:3306->3306/tcp
root_web_1   /usr/sbin/nginx -g daemon off;   Up       0.0.0.0:80->80/tcp

# remove all containers in application
# if a container is running, it won't be removed

[root@dlp ~]#
docker-compose rm

Going to remove root_db_1
Are you sure? [yN] y
Removing root_db_1 ... done
Matched Content