docker-compase使用

一、docker-compase.yml文件配置

version: "3"
services:
  nginx: # 服务名称,用户自定义
    image: nginx:latest  # 镜像版本
    network_mode: host
    ports:
      - 8102:8102  # 暴露端口
      - 8303:8303  # 暴露端口
    volumes: # 挂载
      - /root/nginx/html/dist:/usr/share/nginx/html/dist
      - /root/nginx/html/dist1:/usr/share/nginx/html/dist1
      - /root/nginx/nginx.conf:/etc/nginx/nginx.conf
    environment:
      - TZ=Asia/Shanghai
    privileged: true # 这个必须要,解决nginx的文件调用的权限问题
  mysql:
    image: mysql:8.0.23
    network_mode: host
    ports:
      - 3306:3306
    environment: # 指定用户root的密码
      - MYSQL_ROOT_PASSWORD=121820
      - TZ=Asia/Shanghai
  redis:
    image: redis:latest
    network_mode: host
    environment:
      - TZ=Asia/Shanghai  
  emqx: # 服务名称,用户自定义
    image: emqx/emqx:latest  # 镜像版本
    network_mode: host
    restart: always
    ports:
      - 1883:1883
      - 8083:8083
      - 8084:8084
      - 8883:8883
      - 18083:18083
    volumes: # 挂载
      - /root/acl.conf:/etc/acl.conf
    environment:
      - TZ=Asia/Shanghai
  mqtt:
    image: mqtt:latest
    network_mode: host
    build: /root/Dockerfile-MQTT # 表示以当前目录下的Dockerfile开始构建镜像
    ports:
      - 9002:9002
    environment:
      - TZ=Asia/Shanghai
  mqtt-vue:
    image: mqtt-vue:latest
    network_mode: host
    build: /root/Dockerfile-liteweb # 表示以当前目录下的Dockerfile开始构建镜像
    ports:
      - 9102:9102
    environment:
      - TZ=Asia/Shanghai
  material:
    image: material:latest
    network_mode: host
    build: /root/Dockerfile-material # 表示以当前目录下的Dockerfile开始构建镜像
    ports:
      - 9303:9303
    environment:
      - TZ=Asia/Shanghai
    depends_on: # 依赖与mysql、redis,其实可以不填,默认已经表示可以
      - nginx
      - mysql
      - redis
      - emqx
1.image

  image就是搭建环境的镜像,例如,mysql:8.0.23,即使用mysql8.0.23版本,redis:latest,即使用redis最新版本

2.network_mode

        network_mode有五种,常见的有4种:bridge 模式,host 模式,container 模式,none 模式。

①.bridge模式

        默认使用bridge模式,Docker会在宿主机上建立一个虚拟网桥docker0,宿主机上启动的容器会连接到这个虚拟网桥上.
        Docker会在一个可用网段(一般是172.17.0.0/16这个网段)中为docker0分配一个IP地址(一般是172.17.0.1),而每创建一个使用bridge网络模式的新的容器,Docker就会在上面的网段中选择一个尚未分配的IP地址分配给容器的eth0网卡.

        (1).后端yml配置文件

        在后端配置文件中,可以通过docker-compose中服务名称来代替服务地址,即mysql代替localhost。

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://mysql:3306/iot?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: XXXXXX
  redis:
    port: 6379
    host: redis
    database: 0
    password:
    #      设置最大的连接超时时间
    timeout: 2000ms
    lettuce:
      pool:
        #        最大连接池的数量,负数表示没有限制,默认为8
        max-active: 20
        #        连接池最大阻塞等待时间,负数表示没有限制
        max-wait: 1ms
        #        连接池最大连接数,默认为8
        max-idle: 8
        #        连接池最小连接数,默认为0
        min-idle: 0
        (2).nginx.conf文件配置

        如果前端使用Nginx,那么nginx.conf中进行配置反向代理时,需使用各容器的ip地址进行转发。

#user  root;
worker_processes  1;
events {
  worker_connections  1024;
}
http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  server {
      listen       8002;
      server_name  xxxxx.top;   #服务器地址
      root   /usr/share/nginx/html;
      try_files $uri $uri/ /index.html last; # 避免404
      index  index.html index.htm;
      #配置反向代理
      #当路径匹配到/prod-api/时,将用http://172.18.0.6:9303/来代替
      location /prod-api/ {
          #后端公网ip或者容器IP,docker inspect <容器id或容器名称> | grep IPAddress
          proxy_pass http://172.18.0.6:9303/; 
          #解决跨域
          add_header Access-Control-Allow-Origin always;
          add_header Access-Control-Allow-Headers '*';
          add_header Access-Control-Allow-Methods '*';
          add_header Access-Control-Allow-Credentials 'true';
          if ($request_method = 'OPTIONS') {
                return 204;
          }
      }
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
  }
}
②.host模式

        host模式即使用宿主机的ip地址,各个容器之间可以通过localhost相连通,但是端口相同会导致服务启动失败。

        (1).后端yml配置文件
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/iot?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: XXXXXX
  redis:
    port: 6379
    host: localhost
    database: 0
    password:
    #      设置最大的连接超时时间
    timeout: 2000ms
    lettuce:
      pool:
        #        最大连接池的数量,负数表示没有限制,默认为8
        max-active: 20
        #        连接池最大阻塞等待时间,负数表示没有限制
        max-wait: 1ms
        #        连接池最大连接数,默认为8
        max-idle: 8
        #        连接池最小连接数,默认为0
        min-idle: 0
        (2).nginx.conf文件配置

        如果前端使用Nginx,那么nginx.conf中进行配置反向代理时,使用localhost即可连接。

#user  root;
worker_processes  1;
events {
  worker_connections  1024;
}
http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  server {
      listen       8002;
      server_name  xxxxx.top;   #服务器地址
      root   /usr/share/nginx/html;
      try_files $uri $uri/ /index.html last; # 避免404
      index  index.html index.htm;
      #配置反向代理
      #当路径匹配到/prod-api/时,将用http://172.18.0.6:9303/来代替
      location /prod-api/ {
          #后端公网ip或者容器IP,docker inspect <容器id或容器名称> | grep IPAddress
          proxy_pass http://localhost:9303/; 
          #解决跨域
          add_header Access-Control-Allow-Origin always;
          add_header Access-Control-Allow-Headers '*';
          add_header Access-Control-Allow-Methods '*';
          add_header Access-Control-Allow-Credentials 'true';
          if ($request_method = 'OPTIONS') {
                return 204;
          }
      }
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
  }
}
③.Container 模式
docker run -it --name <新容器名称> --net=container:<容器id或容器名称> <镜像> /bin/bash

        Container模式,就是在运行容器时通过--net指定另一个已经在运行的有单独Network Namespace的容器,然后与这个容器共享一个Network Namespace.在这种情况下,新建的容器没有自己的网卡,也不会给它分配IP,而是两个容器共享IP和端口空间.

④.none 模式

        none模式下,Docker会为新创建的容器分配自己的Network Namespace,但不会为这个容器的网络进行任何配置,容器也没有自己的IP网卡等信息,需我们为其配置.

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dec.18

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值