原创

docker-compose建立网络

1.创建docker网络

介绍docker网络中其中使用使用方法network

1.创建一个lsh网络

lshdeMacBook-Pro: lsh$ docker network create lsh
0f3220a4b3322e2812f53c3c4dced3376f7cd2482e5e638fdf9d5c4abec82ef7

2.查看网络

lshdeMacBook-Pro: lsh$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
ff096d984a97        bridge              bridge              local
011ee7c1674c        doudizhu_default    bridge              local
fbc6129c83c5        host                host                local
1087354a1c7e        lsh                 bridge              local
d06405c56465        mysql_default       bridge              local
189d1e2a6c79        none                null                local

2.lsh网络中使用mysql

mysql的docker-compose.yml 。其中mysql在lsh’网络中的host为mysql56

version: '3'
services:
    dkmyqsl56:
        environment:
            MYSQL_ROOT_PASSWORD: "123456"
            MYSQL_USER: 'root'
            MYSQL_PASS: '123456'
        image: "mysql:5.6"
        restart: always
        volumes:
            - "${PWD}/data:/var/lib/mysql"
            - "${PWD}/conf/my.cnf:/etc/my.cnf"
        ports:
            - "3301:3306"
        networks:
            lsh:
                aliases:
                    - mysql56 
networks:
  lsh:
    external: true

3.使用lsh网络中的mysql服务

1.app的docker-compose.yml文件,定义其中使用的network为lsh即可

version: "3"

services:
  aaa:
    build: .
    ports:
      - "8080:8080"
    #volumes:
      #- ./ddz.log:ddz.log
    restart: always
    networks: 
      - lsh
networks:
  lsh:
    external: true

2. app代码中配置mysql地址

mysql://root:123456@mysql56:3306/ddz
正文到此结束
本文目录