• Docker Compose



    title: “Docker Compose”
    createTime: 2022-01-04T20:14:01+08:00
    updateTime: 2022-01-04T20:14:01+08:00
    draft: false
    author: “name”
    tags: [“docker”,“docker-compose”]
    categories: [“docker”]
    description: “测试的”

    docker-compose 的相关说明

    docker-compose.yml 文件说明

    # common.env 文件里面格式为
    RACK_ENV=development   
    
    
    # arg 和 env_file 和 environment
    联系:因为都是设置变量的
    arg             设置dockerfile的变量
    env_file        设置docker容器的运行环境变量的
    environment     设置docker镜像和容器的环境变量的,会覆盖 dockerfile 里面 env 设置的环境变量  
    
    
    # 在工作目录下创建 docker-compose.yml 文件,编辑以下内容
    version: "3"
    # version 版本
    services:
    # 定义服务,这里定义 test-service服务
      test-service:
      #指定这个服务的名字 
        image: test-service-image
        # 镜像的名字 
        container_name: test-service-container
        # 容器的名字
        hostname: 192.168.1.65
        restart: always
        privileged: true
        working_dir: /root
        env_file:   #  专门存放变量的文件。如果通过 docker-compose -f FILE 指定了配置文件,则 env_file 中路径会使用配置文件路径。如果有变量名称与 environment 指令冲突,则以后者为准
            - ./common.env
            - ./apps/web.env
        environment:  #给运行的容器 存放环境变量
          - profile=local
        volumes:  #容器与宿主机映射的  注意是 宿主机的内容映射到容器中
          - /root/docker/:/root/data/  # 把/root/docker 的内容映射到 /root/data
        expose:  # 这个标签与 Dockerfile 中的 EXPOSE 指令一样,用于指定暴露的端口
          - "30001"
        ports:   #映射端口的标签。使用 HOST:CONTAINER 格式或者只是指定容器的端口,宿主机会随机映射端口。
          - "30001:30001" 
        extra_hosts:  # 加主机名的标签,就是往 /etc/hosts 文件中添加一些记录、
          - "host:192.168.1.9" 
        dns:
          - 8.8.8.8
          - 9.9.9.9
        command: /bin/sh  # 指定容器启动后的命令
        entrypoint: /start.sh  # Dockerfile 中有一个指令叫做 ENTRYPOINT 指令,用于指定接入点,在 docker-compose.yml 中可以定义接入点,覆盖 Dockerfile 中的定义
        tmpfs: #  挂载临时目录到容器内部,与 run 的参数一样
          - /run
          - /tmp
        labels: # 向容器添加元数据,和 Dockerfile 的 LABEL 指令一个意思。  在 inspect 可以看到
          - com.example.description: "Accounting webapp"
          - com.example.department: "Finance"
          - com.example.label-with-empty-value: ""
      test-build-service:   #指定这个服务的名字 
        build:       # 镜像构建的标志
          context: .  # 镜像构建的上下文
          dockerfile: Dockerfile #镜像构建的上下文
          args:   # 给dockerfile文件传送变量值
            buildno: 1
            password: secret
        container_name: test-build-service
        # 容器的名字
        hostname: 192.168.1.65
        depends_on:  # 依赖与某一个容器进行启动
          - test-service
        external_links: 与某个容器相通
          - test-service
        link:    # 与容器连接 低级方法  不推荐使用
          - test-service
        logging:  #配置日志
          driver: syslog  #默认是json-file  好像可以对接elk
          options:
            max-file: "3"
            max-size: "50m"
        pid: "host" # PID 模式设置为主机 PID 模式,跟主机系统共享进程命名空间。容器使用这个标签将能够访问和操纵其他容器和宿主机的名称空间。
        volumes: #挂载一个目录或者一个已存在的数据卷容器,可以直接使用 HOST:CONTAINER 这样的格式,
        # 或者使用 HOST:CONTAINER:ro 这样的格式,后者对于容器来说,数据卷是只读的,这样可以有效保护宿主机的文件系统。
          - /var/lib/mysql   # 只是指定一个路径,Docker 会自动在创建一个数据卷(这个路径是容器内部的)。
          - /opt/data:/var/lib/mysql   # 使用绝对路径挂载数据卷
          - ./cache:/tmp/cache   # 以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器。
          - ~/configs:/etc/configs/:ro  # 使用用户的相对路径(~/ 表示的目录是 /home/<用户目录>/ 或者 /root/)。
          - datavolume:/var/lib/mysql # 已经存在的命名的数据卷。
        devices: # 设备映射列表
          - "/dev/ttyUSB0:/dev/ttyUSB0"
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    docker-compose 命令说明

    网址: https://docs.docker.com/compose/reference/up/

    一、docker-compose up [options] [SERVICE…]

    • options 参数 可以用-d 变成后台运行
    • service 服务名
    • docker-compose -f ~/docker/docker-compose.yml up -d

    二、docker-compose logs [options] [SERVICE…]

    Options:
        --no-color          Produce monochrome output.
        -f, --follow        Follow log output.
        -t, --timestamps    Show timestamps.
        --tail="all"        Number of lines to show from the end of the logs
                            for each container.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    三、docker-compose stop alarm-user 停止正在运行的容器

    四、docker-compose start alarm-user 运行已经存在的容器

    五、docker-compose rm [options] [SERVICE…]

    Usage: rm [options] [SERVICE...]
    
    Options:
        -f, --force   Don't ask to confirm removal
        -s, --stop    Stop the containers, if required, before removing
        -v            Remove any anonymous volumes attached to containers
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    六、docker-compose ps

    Usage: ps [options] [SERVICE...]
    
    Options:
        -q, --quiet          Only display IDs
        --services           Display services
        --filter KEY=VAL     Filter services by a property
        -a, --all            Show all stopped containers (including those created by the run command)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    leetcode.62. 不同路径
    vben admin BasicTable 表格的基本使用
    centos7安装mysql5.7步骤(图解版)
    华为机试题刷题总结
    kubernetes进阶 (三) 基础练习
    数据中台的那些“经验与陷阱”
    相信未来:技术的进步意味着重构
    20. 有效的括号 --力扣 --JAVA
    自然语音处理(NLP)系列(五)——详解智能问答系统
    asp毕业设计——基于asp+access的网页设计辅导系统设计与实现(毕业论文+程序源码)——网页设计辅导系统
  • 原文地址:https://blog.csdn.net/ZHUXIUQINGIT/article/details/133430671