Fedora 29
Sponsored Link

Docker : Use Docker Compose
2018/11/07
 
Install Docker Compose to create or run multiple Containers easily.
[1] Install Docker Compose.
[root@dlp ~]#
dnf -y install docker-compose
[2] For example, create a config to create 2 containers, one has Apache httpd, one has MariaDB.
# create Dockerfile with httpd

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

RUN dnf -y upgrade
RUN dnf -y install httpd

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

# define application configuration

[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: fedora
      MYSQL_PASSWORD: password
      MYSQL_DATABASE: fedora_db
    ports:
      - "3306:3306"
  web:
    build: .
    ports:
      - "80:80"
    volumes:
      - /var/lib/docker/disk02:/var/www/html

# build and run application

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

Creating network "root_default" with the default driver
Pulling db (mariadb:)...
Trying to pull repository docker.io/library/mariadb ...
Building web
Step 1/6 : FROM fedora
 ---> c582c1438f27
Step 2/6 : MAINTAINER ServerWorld <admin@srv.world>
 ---> Using cache
 ---> 6c28f50bfcce
.....
.....
Creating root_db_1  ... done
Creating root_web_1 ... done

[root@dlp ~]#
docker ps

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                    NAMES
13316571d184   root_web  "/usr/sbin/httpd -..."   28 seconds ago  Up 26 seconds  0.0.0.0:80->80/tcp       root_web_1
31ff9de8ca96   mariadb   "docker-entrypoint..."   28 seconds ago  Up 26 seconds  0.0.0.0:3306->3306/tcp   root_db_1

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

Enter password:
+---------------+--------------+
| Variable_name | Value        |
+---------------+--------------+
| hostname      | 31ff9de8ca96 |
+---------------+--------------+

[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/docker/disk02/index.html

[root@dlp ~]#
curl 127.0.0.1

Hello Docker Compose World
[3] The follows are some other basic Operation for Docker Compose.
# show status of application

[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/httpd -D FOREGROUND   Up      0.0.0.0:80->80/tcp

# show logs of application

[root@dlp ~]#
docker-compose logs

.....
.....
db_1   | 2018-11-06  8:15:05 0 [Note] mysqld: ready for connections.
db_1   | Version: '10.3.10-MariaDB-1:10.3.10+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

# run commands inside a container

# for container name, it's just the name set in [docker-compose.yml]

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

root@31ff9de8ca96:/#

# stop allications

[root@dlp ~]#
docker-compose stop

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

# run an application particularly

# if it has dependency, another container also runs

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

Starting root_web_1 ... 
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/httpd -D FOREGROUND   Up       0.0.0.0:80->80/tcp

# remove applications

# running container won't be removed

[root@dlp ~]#
docker-compose rm

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