Docker : Use Docker Compose2018/06/12 |
To Install Docker Compose, it's easy to configure and run multiple containers as a Docker application.
|
|
[1] | Install Docker Compose. |
root:~# apt -y install docker-compose
|
[2] | For example, Configure an application that has WEB and DB services with Docker Compose. |
FROM ubuntu MAINTAINER ServerWorld <admin@srv.world> RUN apt-get update RUN apt-get -y install apache2 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: bionic MYSQL_PASSWORD: password MYSQL_DATABASE: bionic_db ports: - "3306:3306" web: build: . ports: - "80:80" volumes: - /var/lib/docker/disk02:/var/www/html # buid and run root@dlp:~# docker-compose up -d Building web Step 1/6 : FROM ubuntu ---> 113a43faa138 Step 2/6 : MAINTAINER ServerWorld <admin@srv.world> ---> Using cache ---> 6fdcd1fb1603 Step 3/6 : RUN apt-get update ---> Using cache ---> 6fd58799374c ..... ..... Starting root_db_1 ... Starting root_db_1 Creating root_web_1 ... Creating root_web_1 ... doneroot@dlp:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2e873214fcf9 root_web "/usr/sbin/apachectl…" 30 seconds ago Up 27 seconds 0.0.0.0:80->80/tcp root_web_1 ee9472495571 mariadb "docker-entrypoint.s…" 22 minutes ago Up 29 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 | ee9472495571 | +---------------+--------------+root@dlp:~# mysql -h 127.0.0.1 -u bionic -p -e "show databases;" Enter password: +--------------------+ | Database | +--------------------+ | bionic_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/apachectl -D FOR ... Up 0.0.0.0:80->80/tcp # show logs of containers root@dlp:~# docker-compose logs db_1 | 2018-06-12 4:35:23 0 [Note] mysqld (mysqld 10.3.7-MariaDB-1:10.3.7+maria~jessie) starting as process 1 ... db_1 | 2018-06-12 4:35:23 0 [Note] InnoDB: Using Linux native AIO ..... ..... db_1 | 2018-06-12 5:34:24 0 [Note] mysqld: ready for connections. db_1 | Version: '10.3.7-MariaDB-1:10.3.7+maria~jessie' socket: '/var/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@ee9472495571:/# # 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 ... Starting root_web_1 ... doneroot@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 ... Up 0.0.0.0:80->80/tcp # remove all containers in application # if a container is running, it won't be removeds 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
|
Sponsored Link |