51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

docker-compose 实战案例

编写docker-compose 文件案例

cat docker-compose.yml

docker-compose.yml 内容'

# docker-compose版本号
version: "3.9"

# docker-compose启动的服务
services:
  # 服务的名称,可以自定义
  baimei-game-bird:
    # 定义如何部署镜像 
    deploy:
      # 指定副本的数量
      replicas: 3
    # 指定存储卷
    volumes:
      # 使用随机(匿名)存储卷
    - /data01
      # 将存储卷的名称挂载到容器的/data02目录
    - dashu:/data02
      # 将宿主机的/haha挂载到/data03目录
    - /haha:/data03
    # 指定容器启动时的命令
    command: ["tail","-f","/etc/hosts"]
    # 配置端口映射,对外暴露服务,相当于"docker run -p"
    # ports:
    # - "80:80"
    # - "9200:9200/tcp"
    # - "127.0.0.1:9300:9300/tcp"
    # 定义容器的重启策略
    restart: always
    # 暴露服务的端口,相当于dockerfile的EXPOSE指令
    expose:
    - "9200"
    - "9300"
    # 传递环境变量
    environment:
    - SCHOOL=baimei
    - CLASS=dashu
    - APPS=bird
    - email=baimei@baimei.com
    # 该服务依赖的其他服务,那么依赖的服务会优先启动
    depends_on:
      - baimei-game-chengbao
      - baimei-game-feiji
    # 给服务打标签
    labels:
       email: baimei@baimei.com
       address: ShaHe
    # 配置健康检查
    healthcheck:
      # 配置健康检查的命令
      test: curl -f http://localhost || exit 1
      # 指定每间隔多久检测依次
      interval: 1s
      # 指定超时时间
      timeout: 3s
      # 指定重试次数
      retries: 3
      # 指定延迟的秒数
      start_period: 10s
    # 引用自定义网络
    networks:
      - baimei-net
    # 指定容器的名称
    # container_name: bird
    # 指定镜像
    image: baimei-games-bird:v4
    # 指定编译的相关参数
    build:
      # 指定工作目录
      context: .
      # 指定dockerfile文件名称
      dockerfile: bird.dockerfile

  baimei-game-chengbao:
    restart: unless-stopped
    healthcheck:
      test: curl -f http://localhost || exit 1
      interval: 1s
      timeout: 3s
      retries: 3
      start_period: 10s
    networks:
      - baimei-net
    container_name: chengbao
    image: baimei-games-chengbao:v4
    build:
      context: .
      dockerfile: chengbao.dockerfile

  baimei-game-feiji:
    restart: on-failure:3
    healthcheck:
      test: curl -f http://localhost || exit 1
      interval: 1s
      timeout: 3s
      retries: 3
      start_period: 10s
    networks:
      - baimei-net
      - baimei-dashu
    container_name: feiji
    image: baimei-games-feiji:v4
    build:
      context: .
      dockerfile: feiji.dockerfile


# 定义网络配置,可以自定义多个网络
networks:
  baimei-dashu: {}
  # 网络名称,该名称可以自定义
  baimei-net:
    # 定义ip相关的属性
    ipam:
      # 定义相关的配置
      config:
          # 定义自定义网段的子网
        - subnet: 172.28.0.0/16
          # 自定自定义网关地址
          gateway: 172.28.0.254


# 定义存储卷
volumes:
  # 自定义存储卷名称
  dashu:
  baimei:

bird.dockerfile 内容

FROM alpine

LABEL school=baimei \
      class=dashu \
      office=www.baimei.com \
      apps=bird

EXPOSE 80

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
    apk update && \
    apk add nginx curl && \
    rm -rf /var/cache/apk/ 

COPY config/bird.conf  /etc/nginx/nginx.conf

ADD softwares/baimei-games-bird.tar.gz /baimei/games

CMD ["nginx","-g","daemon off;"]

下班是 nginx 配置文件

bird.conf 内容

user  nginx;
worker_processes  auto;

error_log  logs/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format main '{"@timestamp":"$time_iso8601",'
                              '"host":"$server_addr",'
                              '"clientip":"$remote_addr",'
                              '"SendBytes":$body_bytes_sent,'
                              '"responsetime":$request_time,'
                              '"upstreamtime":"$upstream_response_time",'
                              '"upstreamhost":"$upstream_addr",'
                              '"http_host":"$host",'
                              '"uri":"$uri",'
                              '"domain":"$host",'
                              '"xff":"$http_x_forwarded_for",'
                              '"referer":"$http_referer",'
                              '"tcp_xff":"$proxy_protocol_addr",'
                              '"http_user_agent":"$http_user_agent",'
                              '"status":"$status"}';

    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    
    server {
        listen        0.0.0.0:80;
        root          /baimei/games;
        server_name   bird.baimei.com;
    }
}
赞(7)
未经允许不得转载:工具盒子 » docker-compose 实战案例