CentOS 8
Sponsored Link

Docker : Use Docker Compose
2020/12/14
 
To Install Docker Compose, it's easy to configure and run multiple containers as a Docker application.
[1] Install Docker Compose.
[root@dlp ~]#
curl -L https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
[root@dlp ~]#
chmod 755 /usr/local/bin/docker-compose
[root@dlp ~]#
docker-compose --version

docker-compose version 1.27.4, build 40524192
[2] For example, Configure an application that has Web and DB services with Docker Compose.
# define Web service container

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

RUN dnf -y install nginx

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

# define application configration

[root@dlp ~]#
vi docker-compose.yml
version: '3'
services:
  db:
    image: mariadb
    volumes:
      - /var/lib/docker/disk01:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: cent
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: cent_db
    ports:
      - "3306:3306"
  web:
    build: .
    ports:
      - "80:80"
    volumes:
      - /var/lib/docker/disk02:/usr/share/nginx/html

# buid and run

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

Building web
Step 1/6 : FROM centos
 ---> 300e315adb2f
Step 2/6 : MAINTAINER ServerWorld <admin@srv.world>
 ---> Using cache
 ---> bcfcafa6508d
Step 3/6 : RUN dnf -y install nginx
 ---> Running in bd158e3bbf20
.....
.....
Creating root_db_1 ... done

[root@dlp ~]#
docker ps

CONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS              PORTS                    NAMES
11febb3c1f22   mariadb    "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp   root_db_1
30eca7b8065b   root_web   "/usr/sbin/nginx -g …"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp       root_web_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      | 11febb3c1f22 |
+---------------+--------------+

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

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

[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] 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 mysqld      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_db_1, root_web_1
db_1   | 2020-12-11 02:51:30+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.8+maria~focal started.
db_1   | 2020-12-11 02:51:30+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
.....
.....
db_1   | 2020-12-11  2:51:38 0 [Note] Added new Master_info '' to hash table
db_1   | 2020-12-11  2:51:38 0 [Note] mysqld: ready for connections.
db_1   | Version: '10.5.8-MariaDB-1:10.5.8+maria~focal'  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

root@11febb3c1f22:/#

# stop application and also shutdown all containers

[root@dlp ~]#
docker-compose stop

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 mysqld      Exit 0
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