docker-编排文件(compose file)

参考使用文档和语法文档

安装方法(Linux)

//下载
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
//授权
sudo chmod +x /usr/local/bin/docker-compose
//软链接
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
//测试
docker-compose --version

常用命令

//忽略缓存,重新构建镜像
docker-compose --no-cache build image1 image2 ...
//【后台】构建镜像,启动容器
docker-compose up -d
//关掉容器,并删除容器
docker-compose down
//关掉容器,并删除容器和没有指定tag的镜像(一般指那些从dockerfile创建的镜像,有更新文件时,这个比较好用)
docker-compose down --rmi local

参考样例

version: "3.7"		#版本号		
networks:		#定义网络
  xxl:
    name: xxl		#网络名称
    driver: bridge		#网络驱动
    ipam:
      driver: default
      config:
        - subnet: 172.18.0.0/16		#子网ip
services:		#定义服务
  nginx:
    container_name: xxl-nginx		#容器名称
    image: nginx		#使用现有镜像
    networks:		#所在网络
      - xxl
    ports:		#端口映射
      - "0.0.0.0:80:80"
    volumes:		#文件映射
      - "./nginx/nginx.conf:/etc/nginx/nginx.conf:ro"
      - "./dist:/dist:ro"
    restart: always		#重启策略
    links:
      - peri		#关联的服务,关联之后本服务可以通过服务名访问其他服务
      - website		
  redis:
    container_name: xxl-redis
    image: redis
    networks:
      - xxl
    ports:
      - "0.0.0.0:6379:6379"
    volumes:
      - "./redis/redis.conf:/etc/redis/redis.conf:ro"
      - "./redis:/data:rw"
    command: redis-server /etc/redis/redis.conf		#覆盖默认的启动命令,这里是指定配置文件启动redis
    restart: always
  mysql:
    container_name: xxl-mysql
    image: mysql
    networks:
      - xxl
    ports:
      - "0.0.0.0:3306:3306"
    volumes:
      - "./mysql:/var/lib/mysql:rw"
    restart: always
  rabbitmq:
    container_name: xxl-rabbitmq
    build:		#使用dockerfile构建镜像
      context: .
      dockerfile: ./dockerfile/rabbitmq/Dockerfile
    networks:
      - xxl
    ports:
      - "0.0.0.0:15672:15672"
      - "0.0.0.0:1883:1883"
    restart: always
  nexus:
    container_name: xxl-nexus
    image: sonatype/nexus3
    networks:
      - xxl
    ports:
      - "0.0.0.0:8081:8081"
    volumes:
      - "./nexus:/nexus-data:rw"
    restart: always
  peri:
    container_name: xxl-peri
    build:
      context: D:\IdeaProjects\periscope-server\build\docker
      dockerfile: ./Dockerfile
    environment:
      - "SPRING_PROFILES_ACTIVE=peri"		#spring-boot程序可以通过这个环境变量指令激活的profile
    networks:
      - xxl
    volumes:
      - "./server/peri:/app/file:rw"
    restart: always
    links:
      - mysql
      - redis
      - rabbitmq
  website:
    container_name: xxl-website
    build:
      context: D:\IdeaProjects\periscope-server\build\docker
      dockerfile: ./Dockerfile
    environment:
      - "SPRING_PROFILES_ACTIVE=website"
    networks:
      - xxl
    volumes:
      - "./server/website:/app/file:rw"
    restart: always
    links:
      - mysql
      - redis
      - rabbitmq

你可能感兴趣的:(运维)