Ubuntu 24.04
Sponsored Link

Podman : Use Docker Compose2024/05/05

 
To Install Docker Compose, it's easy to configure and run multiple containers as a Docker application.
Docker Compose is supported on Podman 3.0 or later.
[1]
[2] 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
[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 ubuntu
MAINTAINER ServerWorld <admin@srv.world>

RUN apt-get update
RUN apt-get -y install nginx
RUN echo "Compose Test on Nginx" > /var/www/html/index.html

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

# define application configuration

root@dlp:~#
vi docker-compose.yml
services:
  db:
    image: docker.io/library/mariadb
    volumes:
      - /var/lib/containers/disk01:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: ubuntu
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: ubuntu_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

# build and run

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

[+] Running 2/2
 ✓ Container root-web-1  Started                                           0.5s
 ✓ Container root-db-1   Started                                           0.5s

root@dlp:~#
podman ps

CONTAINER ID  IMAGE                                    COMMAND               CREATED             STATUS             PORTS                   NAMES
bfd4a69009e5  docker.io/moby/buildkit:buildx-stable-1                        4 minutes ago       Up 4 minutes                               buildx_buildkit_default
cd88fe33591a  docker.io/library/root-web:latest        /usr/sbin/nginx -...  About a minute ago  Up About a minute  0.0.0.0:80->80/tcp      root-web-1
2950f7b74af7  docker.io/library/mariadb:latest         mariadbd              About a minute ago  Up About a minute  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      | 2950f7b74af7 |
+---------------+--------------+

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

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

root@dlp:~#
curl 127.0.0.1

Compose Test on Nginx
[4] Other basic operations of Docker Compose are follows.
# verify state of containers

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

NAME         IMAGE                               COMMAND                  SERVICE   CREATED         STATUS         PORTS
root-db-1    docker.io/library/mariadb:latest    "mariadbd"               db        7 seconds ago   Up 6 seconds   3306/tcp
root-web-1   docker.io/library/root-web:latest   "/usr/sbin/nginx -g …"   web       7 seconds ago   Up 6 seconds   80/tcp

# show logs of containers

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

db-1  | 2024-05-05 05:22:31+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.3.2+maria~ubu2204 started.
db-1  | 2024-05-05 05:22:31+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db-1  | 2024-05-05 05:22:31+00:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:11.3.2+maria~ubu2204 started.
db-1  | 2024-05-05 05:22:31+00:00 [Note] [Entrypoint]: MariaDB upgrade not required
.....
.....
db-1  | 2024-05-05  5:22:31 0 [Note] mariadbd: Event Scheduler: Loaded 0 events
db-1  | 2024-05-05  5:22:31 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:~#
/usr/local/bin/docker-compose exec db /bin/bash

root@142e39567911:/#

# stop application and also shutdown all containers

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

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

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

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

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

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

NAME         IMAGE                               COMMAND                  SERVICE   CREATED         STATUS          PORTS
root-web-1   docker.io/library/root-web:latest   "/usr/sbin/nginx -g …"   web       4 minutes ago   Up 27 seconds   80/tcp

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

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

[+] Removing 1/0e root-db-1 (y/N) y
 ✓ Container root-db-1  Removed
Matched Content