Docker : Use Docker Compose
2018/08/28 |
Install Docker Compose to create or run multiple Containers easily.
|
|
[1] | Install Docker Compose. |
# install from EPEL
[root@dlp ~]#
[root@dlp ~]# yum --enablerepo=epel -y install python2-pip pip install docker-compose
|
[2] | For example, create a config to create 2 containers, one has Apache httpd, one has MariaDB. |
FROM centos MAINTAINER ServerWorld <admin@srv.world> RUN yum -y update RUN yum -y install httpd EXPOSE 80 CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"] version: '3' services: db: image: mariadb volumes: - /var/lib/docker/disk01:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: password MYSQL_USER: centos MYSQL_PASSWORD: password MYSQL_DATABASE: centos_db ports: - "3306:3306" web: build: . ports: - "80:80" volumes: - /var/lib/docker/disk02:/var/www/html # if SELinux is Enforcing, change to permissive or disabled [root@dlp ~]# setenforce 0
# build and run application [root@dlp ~]# docker-compose up -d Building web Step 1/6 : FROM centos Trying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centos 256b176beaff: Download complete ..... ..... Creating root_web_1 ... Creating root_db_1 ... Creating root_web_1 ... done Creating root_db_1 ... done[root@dlp ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06ece319c627 mariadb "docker-entrypoint..." 20 seconds ago Up 18 seconds 0.0.0.0:3306->3306/tcp root_db_1 1f3796c137f3 root_web "/usr/sbin/apachec..." 20 seconds ago Up 18 seconds 0.0.0.0:80->80/tcp root_web_1 # confirm possible to access or not [root@dlp ~]# mysql -h 127.0.0.1 -u root -p -e "show variables like 'hostname';" Enter password: +---------------+--------------+ | Variable_name | Value | +---------------+--------------+ | hostname | 06ece319c627 | +---------------+--------------+[root@dlp ~]# mysql -h 127.0.0.1 -u centos -p -e "show databases;" Enter password: +--------------------+ | Database | +--------------------+ | centos_db | | information_schema | +--------------------+[root@dlp ~]# echo "Hello Docker Compose World" > /var/lib/docker/disk02/index.html [root@dlp ~]# curl localhost 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/apachectl -D FOR ... Up 0.0.0.0:80->80/tcp # show logs of application [root@dlp ~]# docker-compose logs Attaching to root_db_1, root_web_1 web_1 | Passing arguments to httpd using apachectl is no longer supported. web_1 | You can only start/stop/restart httpd using this script. ..... ..... db_1 | 2018-08-28 2:35:54 0 [Note] Added new Master_info '' to hash table db_1 | 2018-08-28 2:35:54 0 [Note] mysqld: ready for connections. db_1 | Version: '10.3.9-MariaDB-1:10.3.9+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@06ece319c627:/# # 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 Stopping root_db_1 ... Stopping root_web_1 ... Stopping root_db_1 ... done Stopping 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/apachectl -D FOR ... Exit 137 # remove applications # running container won't be removed [root@dlp ~]# docker-compose rm
Going to remove root_db_1, root_web_1
Are you sure? [yN] y
Removing root_db_1 ... done
Removing root_web_1 ... done
|