docker-compose複数のコンテナ/サービスを管理


  • 同一管理mysql,oracle
  • ##      :    volume,         
    # docker volume create mysql-data
    # docker volume create oracle-11g
    
    ##     volumes:
    [root@docker ~]# docker inspect registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
     "Image": "helowin/oracle_cmd",
     "Volumes": {
          "/home/oracle/app/oracle/oradata": {}
      }        
    [root@docker ~]# docker inspect mysql
      "Image": "mysql:5.7",
      "Volumes": {
           "/var/lib/mysql": {}
       }
    
    ##   docker-compose  
    [root@docker ~]# cat mysql-oracle.yaml
    version: '3'
    services:
      mysql-docker:
        image: mysql:5.7
        container_name: mysql
        environment:
          MYSQL_ROOT_PASSWORD: '123456'
        ports:
          - 3306:3306
        volumes:
          - mysql-data:/var/lib/mysql
        deploy:
          restart_policy:
            condition: on-failure
            delay: 5s
            max_attempts: 3
      oracle-docker:
        image: registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
        container_name: oracle
        ports:
          - 1521:1521
        volumes:
          - oracle-11g:/home/oracle/app/oracle/oradata
    volumes:
      mysql-data:
      oracle-11g:
    
    ##     mysql, oracle
    [root@docker ~]# cat /etc/rc.d/rc.local
    #!/bin/bash
    touch /var/lock/subsys/local
    service docker start
    COMPOSE_HTTP_TIMEOUT=200 docker-compose -f /root/mysql-oracle.yaml  up -d >>/root/docker-db.log
    
  • 1,管理単体サービス:mysql
  • 2,複数のサービスを管理:app+mysql
  • 1,docker-compose mysqlを起動
    vim docker-compose.yml
    version: '3' 
    services:
      mysql-docker:
        image: mysql:5.7
        environment: 
          MYSQL_ROOT_PASSWORD: '123456'
        ports:
          - 3307:3306 
         #- 3306           
    
        deploy:
          restart_policy:
            condition: on-failure
            delay: 5s
            max_attempts: 3
    

    mysqlを起動し、構文をテストします.
    wang@wang-pc:~/txt/app$ docker-compose up
    WARNING: Some services (mysql-docker) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
    WARNING: The Docker Engine you're using is running in swarm mode.
    
    Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.
    
    To deploy your application across the swarm, use `docker stack deploy`.
    
    Starting app_mysql-docker_1 ... done
    Attaching to app_mysql-docker_1
    mysql-docker_1  | 2019-03-06T10:48:51.247271Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    mysql-docker_1  | 2019-03-06T10:48:51.249118Z 0 [Note] mysqld (mysqld 5.7.24) starting as process 1 ...
    mysql-docker_1  | 2019-03-06T10:48:51.252724Z 0 [Note] InnoDB: PUNCH HOLE support available
    
    

    mysqlを起動し、構文を検証
    wang@wang-pc:~/txt/app$ docker-compose up
    WARNING: Some services (mysql-docker) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
    WARNING: The Docker Engine you're using is running in swarm mode.
    
    Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.
    
    To deploy your application across the swarm, use `docker stack deploy`.
    
    Starting app_mysql-docker_1 ... done
    Attaching to app_mysql-docker_1
    mysql-docker_1  | 2019-03-06T10:48:51.247271Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    mysql-docker_1  | 2019-03-06T10:48:51.249118Z 0 [Note] mysqld (mysqld 5.7.24) starting as process 1 ...
    mysql-docker_1  | 2019-03-06T10:48:51.252724Z 0 [Note] InnoDB: PUNCH HOLE support available
    
    

    2,docker-compose app,mysqlを起動
    Webサービスを追加する構成はideaで以下のコードをa.jarにパッケージ化する
    import java.sql.*;
    public class MysqlTest {
        public static void main(String[] args) throws SQLException {
            //  :   docker-mysql,     
            Connection conn = DriverManager.getConnection("jdbc:mysql://172.17.0.1:3307", "root", "123456");
            Statement stm = conn.createStatement();
            ResultSet result = stm.executeQuery("show databases");
    
            while (result.next()){
                String dbname = result.getString(1);
                System.out.println(dbname);
            }
        }
    }
    

    a.jarを~/txt/app下にコピー
    wang@wang-pc:~/txt/app$ ls
    a.jar  docker-compose.yml  Dockerfile  jdk.tar.gz
    
    wang@wang-pc:~/txt/app$ cat docker-compose.yml 
    version: '3'
    services:
      mysql-docker:
        image: mysql:5.7
        environment: 
          MYSQL_ROOT_PASSWORD: '123456'
        ports:
          - 3307:3306 
         #- 3306           
    
        deploy:
          restart_policy:
            condition: on-failure
            delay: 5s
            max_attempts: 3
    
      
      app:
        build: .
        depends_on:
          - mysql-docker
        
    wang@wang-pc:~/txt/app$ cat Dockerfile 
    FROM centos
    
    RUN mkdir /app
    COPY a.jar /app
    COPY jdk.tar.gz /app
    
    WORKDIR /app
    RUN tar -zxvf jdk.tar.gz 
    #CMD jdk1.8/bin/java -jar a.jar 
    ENTRYPOINT jdk1.8/bin/java -jar 
    
    

    まずmysqlを起動してからappを起動します
    wang@wang-pc:~/txt/app$ docker-compose start mysql-docker
    WARNING: Some services (mysql-docker) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
    Starting mysql-docker ... done
    
    wang@wang-pc:~/txt/app$ docker-compose up
    WARNING: Some services (mysql-docker) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
    WARNING: The Docker Engine you're using is running in swarm mode.
    
    Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.
    
    To deploy your application across the swarm, use `docker stack deploy`.
    
    app_mysql-docker_1 is up-to-date
    Starting app_app_1 ... done
    Attaching to app_mysql-docker_1, app_app_1
    mysql-docker_1  | Initializing database
    ...
    mysql-docker_1  | 2019-03-06T11:10:33.061616Z 0 [Note] mysqld: ready for connections.
    mysql-docker_1  | Version: '5.7.24'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
    app_1           | information_schema
    app_1           | a
    app_1           | b
    app_1           | mysql
    app_1           | performance_schema
    app_1           | sys
    mysql-docker_1  | 2019-03-06T11:10:35.048116Z 2 [Note] Aborted connection 2 to db: 'unconnected' user: 'root' host: '172.24.0.3' (Got an error reading communication packets)
    app_app_1 exited with code 0