如何使Docker适应群体模式

时间:2019-02-21 07:36:30

标签: docker docker-swarm

我正在建立一个硒网格,可以容纳多个浏览器来运行自动化脚本,一段时间后,我找到了部署到集群模式的方法。

version: '3'
networks:
  private:
    driver: overlay
services:
  selenium_hub:
    image: selenium/hub
    ports:
      - 4444:4444
    deploy:
      mode: global
      placement:
        constraints:
          - node.role == manager
    networks:
      - private

  firefox:
    image: selenium/node-firefox
    volumes:
      - /dev/urandom:/dev/random
    depends_on:
      - selenium_hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium_hub
      - HUB_PORT_4444_TCP_PORT=4444
      - NODE_MAX_SESSION=1
    entrypoint: bash -c 'SE_OPTS="-host $$HOSTNAME -port 5555" /opt/bin/entry_point.sh'
    ports:
      - "5555:5555"
    deploy:
      replicas: 1
    networks:
      - private

  chrome:
    image: selenium/node-chrome
    volumes:
      - /dev/urandom:/dev/random
    depends_on:
      - selenium_hub
    environment:
      - HUB_PORT_4444_TCP_ADDR=selenium_hub
      - HUB_PORT_4444_TCP_PORT=4444
      - NODE_MAX_SESSION=1
    entrypoint: bash -c 'SE_OPTS="-host $$HOSTNAME -port 5556" /opt/bin/entry_point.sh'
    ports:
      - 5556:5556
    deploy:
      replicas: 1
    networks:
      - private

我的团队现在可以按比例运行Web自动化脚本,但是我的问题是改善网格以容纳android模拟器和真实设备。 为了实现我所需的功能,我找到了一个不错的示例,该示例允许我使用android仿真器部署Selenium网格,但无法将YAML文件转换为适合群模式。

我想知道我做错了什么,以及docker-compose命令与docker堆栈部署之间的关键区别是什么。

我要添加的数据是:

  real_device:
    image: appium/appium
    depends_on:
      - selenium_hub
    network_mode: "service:selenium_hub"
    privileged: true
    volumes:
      - /dev/bus/usb:/dev/bus/usb
      - ~/.android:/root/.android
      - $PWD/example/sample_apk:/root/tmp
    environment:
      - CONNECT_TO_GRID=true
      - SELENIUM_HOST=selenium_hub
      # Enable it for msite testing
      #- BROWSER_NAME=chrome

  # Docker-Android for Android application testing
  nexus_7.1.1:
    image: budtmo/docker-android-x86-7.1.1
    privileged: true
    # Increase scale number if needed
    scale: 1
    depends_on:
      - selenium_hub
      - real_device
    ports:
      - 6080
    # Change path of apk that you want to test. I use sample_apk that I provide in folder "example"
    volumes:
      - $PWD/example/sample_apk:/root/tmp/sample_apk
      - ./video-nexus_7.1.1:/tmp/video
    environment:
      - DEVICE=Nexus 5
      - CONNECT_TO_GRID=true
      - APPIUM=true
      - SELENIUM_HOST=selenium_hub
      - AUTO_RECORD=true

  # Docker-Android for mobile website testing with chrome browser
  # Chrome browser exists only for version 7.0 and 7.1.1
  samsung_galaxy_web_7.1.1:
    image: budtmo/docker-android-x86-8.1
    privileged: true
    # Increase scale number if needed
    scale: 1
    depends_on: 
      - selenium_hub
      - real_device
    ports:
      - 6080
    volumes:
      - ./video-samsung_7.1.1:/tmp/video
    environment:
      - DEVICE=Samsung Galaxy S6
      - CONNECT_TO_GRID=true
      - APPIUM=true
      - SELENIUM_HOST=selenium_hub
      - MOBILE_WEB_TEST=true
      - AUTO_RECORD=true

  # Docker-Android for mobile website testing with default browser
  # Default browser exists only for version 5.0.1, 5.1.1 and 6.0
  samsung_galaxy_web_5.1.1:
    image: budtmo/docker-android-x86-5.1.1
    privileged: true
    # Increase scale number if needed
    scale: 1
    depends_on:
      - selenium_hub
      - real_device
    ports:
      - 6080
    volumes:
      - ./video-samsung_5.1.1:/tmp/video
    environment:
      - DEVICE=Samsung Galaxy S6
      - CONNECT_TO_GRID=true
      - APPIUM=true
      - SELENIUM_HOST=selenium_hub
      - MOBILE_WEB_TEST=true
      - AUTO_RECORD=true

1 个答案:

答案 0 :(得分:0)

群集模式当前不支持特权容器。您可以在此处跟踪Docker植入该功能的进度:https://github.com/docker/swarmkit/issues/1030

作为一个变通的解决方法,我已经看到一些用户将docker客户端容器作为服务运行,安装docker套接字,并在该服务中作为命令运行docker run --privileged --rm ...

hacky解决方法的示例如下:

version: '3.5'
networks:
  private:
    name: selenium_private
    attachable: true
services:
  real_device:
    image: docker
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    command:
    - docker
    - run
    - --rm
    - --privileged
    - -v
    - /dev/bus/usb:/dev/bus/usb
    - -v
    - /path/to/.android:/root/.android
    - -v
    - /path/to/example/sample_apk:/root/tmp
    - -e
    - CONNECT_TO_GRID=true
    - -e
    - SELENIUM_HOST=selenium_hub
    - --net
    - selenium_private
    - appium/appium

我没有包括的那一部分是网络。硒似乎希望一切都在单个网络名称空间中运行,这将很难实现。您可以在群集模式之外创建具有已知名称的单个虚拟容器,并将所有内容附加到该容器名称的网络名称空间。如果您可以让它在覆盖网络上工作以在服务之间进行通信(大多数微服务的工作方式),那么我将专用网络的定义更新为可附加的,并给它起一个名字(要求使用3.5撰写版本)。 / p>