docker-compose加速下载

##查看镜像的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文件
##使用volume, 防止容器数据丢失
cat > compose.yaml <<EOF
version: '3'
services:
  mysql-docker:
    image: mysql:5.7
    container_name: mysql1
    restart: always 
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
    ports:
      - 3306:3306
    volumes:
      - db1:/var/lib/mysql

  oracle-docker:
    image: registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
    container_name: oracle1
    restart: always 
    ports:
      - 1520:1521
    volumes:
      - db2:/home/oracle/app/oracle

volumes:
  db1: {}
  db2: {}
EOF

## 开机启动mysql, oracle
cat >>  /etc/rc.d/rc.local <<EOF
service docker start
COMPOSE_HTTP_TIMEOUT=200 docker-compose -f /root/compose.yaml  up -d >>/root/docker-db.log
EOF
  • 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


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐