当前位置: 首页 > article >正文

docker-compose常用模板

注意docker-compose版本不同,指令执行结果会不同,具体的以官网文档为主

注意docker-compose版本不同,指令执行结果会不同,具体的以官网文档为主

注意docker-compose版本不同,指令执行结果会不同,具体的以官网文档为主

中文API文档,看这里看这里看这里

ports映射的端口,用引号引起来,避免出现进制转换出问题

version: "3.0"
services:
  tomcat:
    # image: 镜像ID也可以
    image: tomcat:jre8
    ports:
     - "8080:8080"

宿主机与容器中目录数据卷共享

// 可以使用几种方式挂载数据卷
volumes:
  - 宿主机绝对路径:容器内部的文件路径
  - # 相对路径挂容器内部路径,注意这种要在外部声明一下volumes
  - testFile:/user/local/tomcat/webapps
version: "3.0"
services:
  tomcat:
    image: tomcat:jre8
    ports:
     - "8080:8080"
    volumes:
     # 要在外部声明一下
     - test:/usr/local/tomcat/webapps
# 声明上面服务所使用的自动创建的卷名
volumes:
  # 声明指定的卷名
  test

使用指令执行docker-compose之后,docker volume ls查看创建的数据卷,会发现和你yml中的不一致,默认的规则是项目名+你的数据卷名字,例如你写的项目名是A,那么会是A_test这种

// 查看数据卷
docker inspect A_test

使用自定义卷名

version: "3.0"
services:
  tomcat:
    image: tomcat:jre8
    ports:
     - "8080:8080"
    volumes:
     # 要在外部声明一下
     - test:/usr/local/tomcat/webapps
# 声明上面服务所使用的自动创建的卷名
volumes:
  # 声明指定的卷名
  test:
    # 使用自定义卷名,注意使用自定义卷的话,需要你自己先创建一个test数据卷
    # docker volume create test
    external:
      true # true代表使用自定义卷名

network网桥

version: "3.0"
services:
  tomcat:
    image: tomcat:jre8
    ports:
     - "8080:8080"
    volumes:
     - test:/usr/local/tomcat/webapps
    # 指定一个网桥
    networks:
     - test_network
  tomcat02:
    image: tomcat:jre8
    ports:
     - "8081:8080"
    volumes:
     - test02:/usr/local/tomcat/webapps
    networks:
     - test_network
volumes:
  test:
    external:
      true
  test02:
    external:
      true
# 定义服务用到桥
networks:
  # 定义上面的服务用到的网桥名称,默认是bridge,创建的网桥会默认带上项目名字,例如项目名字是A,那么是A_test_network
  test_network

container_name指定容器名字,默认是:项目名称_服务名称_序号

version: "3.0"
services:
  tomcat:
    # 相当于run的 --name
  	container_name: test01
  	# 相当于run的 image
    image: tomcat:jre8
    # 相当于run -p
    ports:
     - "8080:8080"
    # 相当于run -v
    volumes:
     - test:/usr/local/tomcat/webapps
    # 相当于run -network
    networks:
     - test_network
  tomcat02:
    container_name: test02
    image: tomcat:jre8
    ports:
     - "8081:8080"
    volumes:
     - test02:/usr/local/tomcat/webapps
    networks:
     - test_network
volumes:
  test:
    external:
      true
  test02:
    external:
      true
networks:
  test_network
    external:
      # 使用外部网桥,网桥必须存在
      # docker network create -d bridge test_network
      true 

配置mysql,redis

version: "3.0"
services:
  tomcat:
    # 相当于run的 --name
  	container_name: test01
  	# 相当于run的 image
    image: tomcat:jre8
    # 相当于run -p
    ports:
     - "8080:8080"
    # 相当于run -v
    volumes:
     - test:/usr/local/tomcat/webapps
    # 相当于run -network
    networks:
     - test_network
  tomcat02:
    container_name: test02
    image: tomcat:jre8
    ports:
     - "8081:8080"
    volumes:
     - test02:/usr/local/tomcat/webapps
    networks:
     - test_network
   mysql:
     image: mysql:5.7.32
     container_name: mysql
     ports:
      - "3307:3306"
     volumes:
      - mysqldata:/var/lib/mysql
      - mysqlconfig:/etc/mysql
     environment:
      - MYSQL_ROOT_PASSWORD=root
     networks:
      - test_network
volumes:
  test:
    external:
      true
  test02:
    external:
      true
  mysqldata:
  mysqlconf:
networks:
  test_network
    external:
      # 使用外部网桥,网桥必须存在
      # docker network create -d bridge test_network
      true 
version: "3.0"
services:
  tomcat:
    # 相当于run的 --name
  	container_name: test01
  	# 相当于run的 image
    image: tomcat:jre8
    # 相当于run -p
    ports:
     - "8080:8080"
    # 相当于run -v
    volumes:
     - test:/usr/local/tomcat/webapps
    # 相当于run -network
    networks:
     - test_network
  tomcat02:
    container_name: test02
    image: tomcat:jre8
    ports:
     - "8081:8080"
    volumes:
     - test02:/usr/local/tomcat/webapps
    networks:
     - test_network
  mysql:
    image: mysql:5.7.32
    container_name: mysql
    ports:
     - "3307:3306"
    volumes:
     - mysqldata:/var/lib/mysql
     - mysqlconfig:/etc/mysql
    environment:
     - MYSQL_ROOT_PASSWORD=root
    networks:
     - test_network
  redis:
    image: redis:5.0.10
    container_name: redis
    ports:
     - "6379:6379"
    volumes:
     - redisdata:/data
    # command 覆盖容器内部命令
    command: "redis-server --appendonly yes"
    networks:
     - test_network
volumes:
  test:
    external:
      true
  test02:
    external:
      true
  mysqldata:
  mysqlconf:
  redisdata:
networks:
  test_network
    external:
      # 使用外部网桥,网桥必须存在
      # docker network create -d bridge test_network
      true 

http://www.kler.cn/a/37265.html

相关文章:

  • 从小白开始学习CAD(一)
  • MySQL常用命令1
  • vscode 端口转发实现端口映射,实现端口自由
  • Leetcode 43 字符串相乘
  • xShell中使用vim编辑时,无法粘贴外来文本
  • [C++] C++特殊类设计 以及 单例模式:设计无法拷贝、只能在堆上创建、只能在栈上创建、不能继承的类, 单例模式以及饿汉与懒汉的场景...
  • VMware ESXi 7.0 U3n macOS Unlocker OEM BIOS 集成网卡驱动和 NVMe 驱动 (集成驱动版)
  • Qt5.15.2 Webassembly源码裁剪编译
  • 分布式应用之zookeeper集群+消息队列Kafka
  • mmc记录
  • 并发和并行的区别
  • NLog写日志到数据库
  • 网络安全面试题
  • 【探索 Kubernetes|作业管理篇 系列 16】离线业务 Job、CronJob
  • 【NLP】BERT和原理揭示
  • 女孩与花田-InsCode Stable Diffusion 美图活动一期
  • 驾驶证——科目一技巧(三)
  • 文心一言 VS 讯飞星火 VS chatgpt (57)-- 算法导论6.4 1题
  • Es直方图聚合--date_histogram
  • 树莓派4B安装系统 + 花生壳 + docker + portainer管理工具