10 Features of Docker-compose

1. Main feature – Put configuration of all App Layers in 1 place

Specify logically as “Services” all the docker containers needed for different parts of your overall application.
For ex: – WebApp, Business service, DB Layer and even the networking needed along with necessary information like ports.

Also can specify which service depends on what.

version: '2'
services:
  wordpress:
    image: wordpress:latest # https://hub.docker.com/_/wordpress/
    ports:
      - 80:80 # change ip if required
    volumes:
      - ./wp-app:/var/www/html # Full wordpress project
      #- ./plugin-name/trunk/:/var/www/html/wp-content/plugins/plugin-name # Plugin development
      #- ./theme-name/trunk/:/var/www/html/wp-content/themes/theme-name # Theme development
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: password
    depends_on:
      - db
    networks:
      - wordpress-network
  db:
    image: mysql:latest # https://hub.docker.com/_/mysql/ - or mariadb https://hub.docker.com/_/mariadb
    ports:
      - 3306:3306 # change ip if required
    volumes:
      - ./wp-data:/docker-entrypoint-initdb.d
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_ROOT_PASSWORD: password
    networks:
      - wordpress-network
networks:
  wordpress-network:
      driver: bridge

2. Start your App services in 1 shot – Docker-compose up

[root@ip-172-31-17-58 try-compose]# docker-compose up -d
Creating network "try-compose_wordpress-network" with driver "bridge"
Creating try-compose_db_1 ... done
Creating try-compose_wordpress_1 ... done

3.Key ways to stop app in 1 shot – Docker-compose stop v/s Docker-compose down

Stop – stops the containers brought up

Down – stops, removes the containers brought up and also removes any extra resources used by services such as network

[root@ip-172-31-17-58 try-compose]# docker-compose stop
Stopping try-compose_wordpress_1 ... done
Stopping try-compose_db_1        ... done

[root@ip-172-31-17-58 try-compose]# docker-compose down
Stopping try-compose_wordpress_1 ... done
Stopping try-compose_db_1        ... done
Removing try-compose_wordpress_1 ... done
Removing try-compose_db_1        ... done
Removing network try-compose_wordpress-network

4. Monitor your App services – Docker-compose logs and Docker-compose ps

For example on entering the WordPress install in English (US) – we get all the logs from Web app layer of wordpress and also the MYSQL DB layer of wordpress

[root@ip-172-31-17-58 try-compose]# docker-compose logs
Attaching to try-compose_wordpress_1, try-compose_db_1
wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.29.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.29.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_1  | [Wed Sep 15 04:40:09.192406 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.48 (Debian) PHP/7.4.23 configured -- resuming normal operations
wordpress_1  | [Wed Sep 15 04:40:09.192471 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
db_1         | 2021-09-15 04:40:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
db_1         | 2021-09-15 04:40:08+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1         | 2021-09-15 04:40:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
db_1         | 2021-09-15 04:40:08+00:00 [Note] [Entrypoint]: Initializing database files
db_1         | 2021-09-15T04:40:08.816031Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.26) initializing of server in progress as process 42
db_1         | 2021-09-15T04:40:08.825151Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1         | 2021-09-15T04:40:09.450146Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1         | 2021-09-15T04:40:10.906616Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
db_1         | 2021-09-15T04:40:10.907011Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
db_1         | 2021-09-15T04:40:11.007476Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@ip-172-31-17-58 try-compose]# docker-compose logs
Attaching to try-compose_wordpress_1, try-compose_db_1
wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.29.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.29.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_1  | [Wed Sep 15 04:40:09.192406 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.48 (Debian) PHP/7.4.23 configured -- resuming normal operations
wordpress_1  | [Wed Sep 15 04:40:09.192471 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "POST /wp-admin/install.php?step=1 HTTP/1.1" 200 2807 "http://13.232.89.6:82/wp-admin/install.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-includes/js/zxcvbn-async.min.js?ver=1.0 HTTP/1.1" 200 605 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-includes/js/dist/vendor/regenerator-runtime.min.js?ver=0.13.7 HTTP/1.1" 200 2750 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-includes/js/dist/hooks.min.js?ver=a7edae857aab69d69fa10d5aef23a5de HTTP/1.1" 200 2142 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-includes/js/dist/vendor/wp-polyfill.min.js?ver=3.15.0 HTTP/1.1" 200 6383 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-includes/js/dist/i18n.min.js?ver=5f1269854226b4dd90450db411a12b79 HTTP/1.1" 200 4209 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-admin/js/password-strength-meter.min.js?ver=5.8.1 HTTP/1.1" 200 971 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-includes/js/wp-util.min.js?ver=5.8.1 HTTP/1.1" 200 1055 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-includes/js/underscore.min.js?ver=1.13.1 HTTP/1.1" 200 7670 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-admin/js/user-profile.min.js?ver=5.8.1 HTTP/1.1" 200 2637 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /favicon.ico HTTP/1.1" 302 404 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-includes/js/zxcvbn.min.js HTTP/1.1" 200 400400 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
wordpress_1  | 49.36.187.126 - - [15/Sep/2021:04:40:29 +0000] "GET /wp-admin/install.php HTTP/1.1" 200 4556 "http://13.232.89.6:82/wp-admin/install.php?step=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"
db_1         | 2021-09-15 04:40:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
db_1         | 2021-09-15 04:40:08+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1         | 2021-09-15 04:40:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
db_1         | 2021-09-15 04:40:08+00:00 [Note] [Entrypoint]: Initializing database files
db_1         | 2021-09-15T04:40:08.816031Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.26) initializing of server in progress as process 42
db_1         | 2021-09-15T04:40:08.825151Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1         | 2021-09-15T04:40:09.450146Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1         | 2021-09-15T04:40:10.906616Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
db_1         | 2021-09-15T04:40:10.907011Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
db_1         | 2021-09-15T04:40:11.007476Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
db_1         | 2021-09-15 04:40:15+00:00 [Note] [Entrypoint]: Database files initialized
db_1         | 2021-09-15 04:40:15+00:00 [Note] [Entrypoint]: Starting temporary server
db_1         | 2021-09-15T04:40:15.306380Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.26) starting as process 91
db_1         | 2021-09-15T04:40:15.324692Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1         | 2021-09-15T04:40:15.504943Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1         | 2021-09-15T04:40:15.786511Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
db_1         | 2021-09-15T04:40:15.786674Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
db_1         | 2021-09-15T04:40:15.787895Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1         | 2021-09-15T04:40:15.788090Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
db_1         | 2021-09-15T04:40:15.790796Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1         | 2021-09-15T04:40:15.820388Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
db_1         | 2021-09-15T04:40:15.820536Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.26'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
db_1         | 2021-09-15 04:40:15+00:00 [Note] [Entrypoint]: Temporary server started.
db_1         | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db_1         | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
db_1         | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db_1         | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
db_1         | 2021-09-15 04:40:18+00:00 [Note] [Entrypoint]: Creating database wordpress
db_1         |
db_1         | 2021-09-15 04:40:18+00:00 [Note] [Entrypoint]: Stopping temporary server
db_1         | 2021-09-15T04:40:18.231800Z 11 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.26).
db_1         | 2021-09-15T04:40:19.563106Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.26)  MySQL Community Server - GPL.
db_1         | 2021-09-15 04:40:20+00:00 [Note] [Entrypoint]: Temporary server stopped
db_1         |
db_1         | 2021-09-15 04:40:20+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
db_1         |
db_1         | 2021-09-15T04:40:20.495844Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.26) starting as process 1
db_1         | 2021-09-15T04:40:20.507915Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
db_1         | 2021-09-15T04:40:20.697659Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1         | 2021-09-15T04:40:20.933634Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
db_1         | 2021-09-15T04:40:20.933803Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
db_1         | 2021-09-15T04:40:20.935014Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1         | 2021-09-15T04:40:20.935234Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
db_1         | 2021-09-15T04:40:20.938492Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1         | 2021-09-15T04:40:20.963025Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
db_1         | 2021-09-15T04:40:20.963544Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.26'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

5. Manage other lifecycle events of app in 1 shot

[root@ip-172-31-17-58 try-compose]# docker-compose restart
Restarting try-compose_wordpress_1 ... done
Restarting try-compose_db_1        ... done
[root@ip-172-31-17-58 try-compose]# docker-compose pause
Pausing try-compose_db_1        ... done
Pausing try-compose_wordpress_1 ... done
[root@ip-172-31-17-58 try-compose]# docker-compose ps
         Name                        Command               State                           Ports
-----------------------------------------------------------------------------------------------------------------------
try-compose_db_1          docker-entrypoint.sh mysqld      Paused   0.0.0.0:3307->3306/tcp,:::3307->3306/tcp, 33060/tcp
try-compose_wordpress_1   docker-entrypoint.sh apach ...   Paused   0.0.0.0:82->80/tcp,:::82->80/tcp
root@ip-172-31-17-58 try-compose]# docker-compose unpause
Unpausing try-compose_wordpress_1 ... done
Unpausing try-compose_db_1        ... done

6. Scale your App together or specific services

[root@ip-172-31-17-58 try-compose]# docker-compose scale db=0
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Stopping and removing try-compose_db_1 ... done
[root@ip-172-31-17-58 try-compose]# docker-compose scale db=1
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Creating try-compose_db_1 ... done

7. Get back app config in 1 shot

[root@ip-172-31-17-58 try-compose]# docker-compose config
networks:
  wordpress-network:
    driver: bridge
services:
  db:
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_ROOT_PASSWORD: password
    image: mysql:latest
    networks:
      wordpress-network: null
    ports:
    - published: 3307
      target: 3306
    volumes:
    - /home/centos/aarushi/try-compose/wp-data:/docker-entrypoint-initdb.d:rw
  wordpress:
    depends_on:
      db:
        condition: service_started
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_USER: root
    image: wordpress:latest
    networks:
      wordpress-network: null
    ports:
    - published: 82
      target: 80
    volumes:
    - /home/centos/aarushi/try-compose/wp-app:/var/www/html:rw
version: '2'

8. Get finer details for all containers running for entire app

Process level info



[root@ip-172-31-17-58 try-compose]# docker-compose top
try-compose_db_1
  UID      PID    PPID    C   STIME   TTY     TIME      CMD
-------------------------------------------------------------
polkitd   15533   15512   0   04:57   ?     00:00:01   mysqld

try-compose_wordpress_1
UID     PID    PPID    C   STIME   TTY     TIME             CMD
------------------------------------------------------------------------
root   15395   15376   0   04:57   ?     00:00:00   apache2 -DFOREGROUND
33     15476   15395   0   04:57   ?     00:00:00   apache2 -DFOREGROUND
33     15477   15395   0   04:57   ?     00:00:00   apache2 -DFOREGROUND
33     15478   15395   0   04:57   ?     00:00:00   apache2 -DFOREGROUND
33     15479   15395   0   04:57   ?     00:00:00   apache2 -DFOREGROUND
33     15480   15395   0   04:57   ?     00:00:00   apache2 -DFOREGROUND


9 . Pull/Push service images in an app together

[root@ip-172-31-17-58 try-compose]# docker-compose pull
Pulling db        ... done
Pulling wordpress ... done
[root@ip-172-31-17-58 try-compose]# docker images | grep word
wordpress              latest    5a38e52b30bf   4 days ago     618MB
wordpress              4.5       c7441ac916f3   5 years ago    422MB
[root@ip-172-31-17-58 try-compose]# docker images | grep mysql
mysql                  latest    0716d6ebcc1a   11 days ago    514MB

10. Execute commands for running containers in app

[root@ip-172-31-17-58 try-compose]# docker-compose exec db /bin/bash
root@cef92d1cba17:/# ls
bin   dev                         entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint-initdb.d  etc            lib   media  opt  root  sbin  sys  usr