随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

docker-compose.yml编写

 #使用指定的docker-compose文件启动,如不指定则使用docker-compose.yml文件

docker-compose -f docker-compose-n01.yml up  -d # -d表示后台启动,不加-d前台启动

 

复制代码
version: '3'
services:
  web_nginx:
    build:  #使用某个dockerfile启动容器
      context: .
      dockerfile: dockerfile-nginx01
    depends_on:  #设置依赖
      - app_alpine
    ports:
      - 80:80
  app_alpine:
    image: alpine01:latest
    container_name: alpine04
    environment:   #设置容器启动时的环境变量
      - RACK=idc01
      - SHOW=true
    extra_hosts:   #设置容器的/etc/hosts解析
      - "host01:192.168.3.3"
    volumes:
      - /data10   #docker自动创建卷
      - /data1/software:/data11  #从宿主机挂载目录
    #tty: true  #alpine镜像需要使用其驻留进程,否则会自动退出
    command: /bin/sh -c "while true;do sleep 3600;done"  #指定容器启动运行的命令
复制代码

 

 

复制代码
version: '3'
services:
  wad_redis:
    image: redis:latest
    ports:            ###将端口映射到宿主
      - "6379:6379"
    restart: always
    networks:      ####使用已创建的网络bridge_internet,并指定IP
      bridge_internet:
        ipv4_address: 172.18.0.11

networks:         ###此处要先声明已有的网络bridge_internet
 bridge_internet:
    external: true
复制代码

指定使用默认网络:

#前面无需指定networks,使用已存在的网络watchad-master_default
networks:
  default:
    external:
      name: watchad-master_default

默认桥接网络上的容器间使用容器名称无法ping通,自定义的桥接网络上的容器间的 容器名称(container_name) 可以ping通,hostname不能ping通 

使用host网络:

复制代码
  watchad_logstash:
    image: logstash:6.4.1
    ports:
      - "5044:5044"
    environment:
      XPACK_MONITORING_ENABLED: "false"
      pipeline.batch.size: 10
    volumes:
      - ./settings/logstash/:/usr/share/logstash/pipeline/
    network_mode: "host"
    restart: always
复制代码

 

 

volumes用法:

复制代码
ersion: '3'
services:
  test_alpine01:
    image: alpine:latest
    container_name: alpine10
    volumes:
      - type: volume  #使用docker管理的volume
        source: mydata01
        target: /data01
        volume:
          nocopy: true
      - type: bind   #将宿主绝对路径映射到容器内部
        source: ./curr_mydata02
        target: /data02
    networks:
      - bridge_internet


  test_alpine02:
    image: alpine:latest
    container_name: alpine11
    volumes:
      - "alpine_v02:/my_v02"
      - "/home/d03/index.htm:/var/my/index.htm"
    networks:
      - bridge_internet



networks:
  bridge_internet:
    external: true
volumes:
  mydata01:
  alpine_v02:   #不需要已存在,docker会自动创建
复制代码

volumes_from用法:

volumes_from:
  - service_name
  - service_name:ro
  - container:container_name
  - container:container_name:rw

参考:https://www.jianshu.com/p/2217cfed29d7

 

build用法:

复制代码
version: '2'
services:
  frontend:
    build: ./frontend  ####指定Dockerfile所在目录
dockerfile: dockerfile01 #如果不指定,默认使用Dockerfile文件
ports: - "80:80" server: build: ./server ####指定Dockerfile所在目录
 ports: - "5000:5000" network_mode: "host"
复制代码

 
entrypoint.sh文件位置:/usr/local/bin/docker-entrypoint.sh

 

 

docker-compose 参数参考:

https://www.cnblogs.com/yyxianren/p/10894708.html

https://blog.csdn.net/chenfeidi1/article/details/80866841

 

#使用指定的docker-compose文件启动,如不指定则使用docker-compose.yml文件
docker-compose -f docker-compose-n01.yml up -d # -d表示后台启动

#查看docker-compose-n01.yml配置文件下运行的所有容器
docker-compose -f docker-compose-n01.yml ps

#查看docker-compose-n01.yml配置文件下运行的容器服务web_nginx(yml文件中定义的service)
docker-compose -f docker-compose-n01.yml ps web_nginx

#让docker-compose-n01.yml配置文件下容器服务web_nginx运行命令“hostname”
docker-compose -f docker-compose-n01.yml run web_nginx hostname

 

posted on   momingliu11  阅读(9624)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示