• docker-compose.yml 结构

      docker-compose.yml文件分为三个主要部分:services、networks、volumes.。
      services主要用来定义各个容器。
      networks定义需要使用到的network.。
      volumes定义services使用到的volume 。

      docker-compose配置文件样板:

      可以不用docker-compose.yml命名,可以指定别的名字例如 service.yml

      version: "3"
      service:
      order-service:
      image: 192.168.0.101:10080/eshop/eshop-order:latest
      deploy:
      endpoint_mode: vip
      replicas: 3
      restart_policy:
      condition: on-failure
      resources:
      limits:
      cpus: "0.2"
      memory: 2G
      depends_on:
      - nginx-service
      - pay-service pay-service:
      image: 192.168.0.101:10080/eshop/eshop-pay:latest
      deploy:
      endpoint_mode: vip
      replicas: 3
      restart_policy:
      condition: on-failure
      resources:
      limits:
      cpus: "0.2"
      memory: 2G
      depends_on:
      - nginx-service nginx-service
      image: 192.168.0.101:10080/base/nginx
      ports:
      - "8002:80" networks:
      default:
      external:
      name: my-overlay-net

      然后用docker stack执行service.yml

      # test表示stack名
      docker stack deploy -c service.yml test

      每个服务都必须通过 image 指令指定镜像或 build 指令(需要 Dockerfile)来自动构建。如果使用 build 指令,在 Dockerfile 中设置的选项(例如:CMD, EXPOSE, VOLUME, ENV 等) 将会自动被获取,无需在 docker-compose.yml 中再次设置。

      常用命令介绍

      build

      使用当前目录下的Dockerfile进行构建。

      version: '3'
      services:
      web:
      build: ./

      build也可以指定文件路径,Dockerfile的名字.

      version: '3'
      services:
      web:
      build:
      context: ./
      dockerfile: mydockerfile

      image

      指定运行容器使用的镜像。下面的格式都支持。

      image: redis
      image: ubuntu:14.04
      image: yyee/eshop.order:latest
      image: www.myharbor.com:10080/eshop/eshop.order:latest
      image: eshop:latest

      如果本地不存在指定的镜像,则会从repository pull下来。

      version: '3'
      services:
      order-service
      build: ./
      image: www.myharbor.com:10080/eshop/order-service:v1.0.2

      container_name

      默认运行出来的容器名。

      version: '3'
      services:
      order-service
      build: ./
      image: www.myharbor.com:10080/eshop/order-service:v1.0.2
      container_name: my-order

      command

      覆盖容器启动后默认执行的命令(Dockerfile定义的CMD)。当Dockerfile定义了entrypoint的时候,docker-comose.yml定义的command会被覆盖。

      version: '3'
      services:
      web:
      build: ./
      command: ["dotnet","eshop.order.dll"]

      entrypoint

      可以覆盖Dockerfile中定义的entrypoint命令。

      version: '3'
      services:
      web:
      build: ./
      entrypoint: python app.py

      links

      链接到其它服务中的容器。使用服务名称(同时作为别名)或服务名称:服务别名 (SERVICE:ALIAS) 格式都可以。

      version: '3'
      services:
      order-service
      links:
      - user-service
      - pay-service
      - redis

      external_links

      链接到 docker-compose.yml 外部的容器,参数格式跟 links 类似。

      version: '3'
      services:
      order-service
      external_links:
      - redis_1
      - project_db_1:mysql
      - project_db_1:postgresql

      port

      暴露端口信息。

      version: '3'
      services:
      api-gateway-ocelot
      image: api-gateway-ocelot-service:latest
      links:
      - user-service
      - eshop-service
      port: 8080:8080

      expose

      指定内部端口,但不映射到宿主机,只被连接的服务访问。

      version: '3'
      services:
      api-gateway-ocelot
      image: api-gateway-ocelot-service:latest
      links:
      - user-service
      - eshop-service
      expose: 8080

      environment

      enviroment定义的变量会覆盖.env文件中定义的重名环境变量。

      environment:
      RACK_ENV: development
      SHOW: 'true'
      BASEAPI_URL: 192.168.0.101 # 或者 environment:
      - RACK_ENV=development
      - SHOW=true
      - BASEAPI_URL=192.168.0.101

      ports

      将容器的端口80映射到宿主机的端口8080

      version: '3'
      services:
      api-gateway-ocelot
      image: api-gateway-ocelot-service:latest
      links:
      - user-service
      - eshop-service
      ports: 8080 # 或者
      version: '3'
      services:
      api-gateway-ocelot
      image: api-gateway-ocelot-service:latest
      links:
      - user-service
      - eshop-service
      ports:
      - 8080:80
      - 127.0.0.1:8080:80

      volumes

      设置容器的数据卷路径。

      version: '3'
      services:
      order-service
      build: ./
      image: www.myharbor.com:10080/eshop/order-service:v1.0.2
      ontainer_name: my-order
      values:
      - data_path: /var/data/order-data
      - log-path:/ var/data/order-log

      networks

      容器使用的网络设置。

      ersion: '3'
      services:
      order-service
      network: my-net
 

docker-compose.yml 使用说明的更多相关文章

  1. Docker Compose YML文件配置

    Docker Compose YML 默认的模板文件是 docker-compose.yml,其中定义的每个服务都必须通过 image 指令指定镜像或 build 指令(需要 Dockerfile)来 ...

  2. docker compose yml 文件常用字段简介

    常用参数: version # 指定 compose 文件的版本 services # 定义所有的 service 信息, services 下面的第一级别的 key 既是一个 service 的名称 ...

  3. Docker Compose yml

    Wordpress + Mysql version: '3' services: db: image: mysql:latest volumes: - db_data:/var/lib/mysql e ...

  4. Docker Compose 创建yml 简单试例

    Docker Compose 创建yml 简单试例 Docker Compose 文件使用格式版本需要与Docker版本对应可在官网内查找 查找地址:https://docs.docker.com/c ...

  5. docker内程序如何读取dockerfile和compose.yml中设置的环境变量

    docker内程序如何读取dockerfile和compose.yml中设置的环境变量 背景 compose文件中配置了服务A和服务B,其中B服务调用了A服务的接口,那么B的实现代码中该如何调用A的服 ...

  6. 25.docker compose 简介 和 docker-compose.yml 参数介绍

    1. docker compose概念 文档  https://docs.docker.com/compose/compose-file/compose-versioning 一个基于 docker ...

  7. Docker Compose部署项目到容器-基于Tomcat和mysql的项目yml配置文件代码

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  8. Docker Compose部署Nexus3时的docker-compose,yml代码

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  9. Docker Compose容器编排

    Compose是Docker官方的开源项目,可以实现对Docker容器集群的快速编排.Compose 中有两个重要的概念:服务(service):一个应用的容器,实际上可以包括若干运行相同镜像的容器实 ...

  10. docker——三剑客之Docker Compose

    编排(Orchestration)功能是复杂系统实现灵活可操作性的关键.特别是在Docker应用场景中,编排意味着用户可以灵活的对各种容器资源实现定义和管理. 作为Docker官方编排工具,Compo ...

随机推荐

  1. 关于 apt 的一些命令

    apt update 用于 更新 包索引,包列表 apt install -y open-vm-tools-desktop fuse 安装 vm-tools 工具

  2. package.json文件干什么的 ?

    package.json 是项目描述文件,记录了当前项目的信息,比如项目的名字,版本,作者,还有所依赖的第三方模块 : dependencies 是项目依赖,是项目上线时要依赖的第三方包 : devd ...

  3. spring boot 向nacos注册方式,以及遇见的报错(boot!boot! 不是cloud!)

    一.首先添加nacos注册发现的pom依赖 <dependency> <groupId>com.alibaba.boot</groupId> <artifac ...

  4. 3DRealCar: An In-the-wild RGB-D Car Dataset with 360-degree Views

    3DRealCar:An In-the-wild RGB-D Car Dataset with 360-degree Views Du, Xiaobiao and Sun, Haiyang and W ...

  5. 【笔记】 STL容器

    [笔记] STL容器 vector vector<int> v; v.push_back(x); v.emplace(x); v.size(); v.erase(v.begin(),v.b ...

  6. 网络与并行计算国际会议IFIP NPC 2024(CCF推荐会议)投稿延期至2024年8月25日

    原地址: https://mp.weixin.qq.com/s/zmg0SDbyDmoNBbJYzDjADA 主页: https://www.npc-conference.com/#/npc2024/ ...

  7. require/import路径中的叹号是什么?

    问题: 之前在一些开源项目的源码里,以及一些文章里,见到如下这样的require/import路径,其中包含形如!.的片段,不知道是什么意思: // https://juejin.im/post/68 ...

  8. SpringMVC-Mybatis-Maven项目整合

    上次不知道为什么,把写好的系列文章都搞成一样了.结果,五篇文章,都变成了最后一篇文章. 悲剧,好吧,只好重新写了. 这系列文章写的是SpringMVC-Mybatis-Maven项目整合.说白了,其实 ...

  9. ibatis源码学习(一)整体设计和核心流程

    本文主要从ibatis框架的基本代码骨架进行切入,理解ibatis框架的整体设计思路,各组件的实现细节将在后文进行分析. 背景  介绍ibatis实现之前,先来看一段jdbc代码:  Class.fo ...

  10. javascript 实现参数重载

    1.概要 在java中,同一个函数签名,比如 getUser,我们可以根据参数的不同,调用不同功能的方法.这也就是参数重载,如何在javascript也实现参数重载呢? 2.实现方法 function ...