当前位置: 首页 > 面试题库 >

Docker,Django和Selenium-Selenium无法连接

曹涵润
2023-03-14
问题内容

我已经将Docker配置为使用docker-compose.yml运行Postgres和Django,并且工作正常。

我遇到的麻烦是Selenium无法连接到Django liveserver。

现在(至少对我而言),django必须访问selenium才能控制浏览器,而selenium必须访问django才能访问服务器。

我已经尝试从此处使用docker-
compose.yml的以下配置使用docker’大使’模式:https
:
//github.com/docker/compose/issues/666

postgis:
  dockerfile: ./docker/postgis/Dockerfile
  build: .
  container_name: postgis

django-ambassador:
  container_name: django-ambassador
  image: cpuguy83/docker-grand-ambassador
  volumes:
    - "/var/run/docker.sock:/var/run/docker.sock"
  command: "-name django -name selenium"

django:
  dockerfile: ./docker/Dockerfile-dev
  build: .
  command: python /app/project/manage.py test my-app
  container_name: django
  volumes:
    - .:/app
  ports:
    - "8000:8000"
    - "8081:8081"
  links:
    - postgis
    - "django-ambassador:selenium"
  environment:
    - SELENIUM_HOST=http://selenium:4444/wd/hub

selenium:
  container_name: selenium
  image: selenium/standalone-firefox-debug
  ports:
    - "4444:4444"
    - "5900:5900"
  links:
    - "django-ambassador:django"

当我检查http:// DOCKER-MACHINE-IP:4444 / wd / hub / static / resource /
hub.html时,
我可以看到Firefox启动了,但是由于Firefox无法连接到Django,所有测试都失败了

'Firefox can't establish a connection to the server at localhost:8081'

我也在这里https://github.com/docker/compose/issues/1991尝试了此解决方案,
但是这不起作用,因为我无法让django同时连接到Postgis和Selenium

'django.db.utils.OperationalError: could not translate host name "postgis" to address: Name or service not known'

我尝试使用下面列出的网络功能

postgis:
  dockerfile: ./docker/postgis/Dockerfile
  build: .
  container_name: postgis
  net: appnet

django:
  dockerfile: ./docker/Dockerfile-dev
  build: .
  command: python /app/project/manage.py test foo
  container_name: django
  volumes:
    - .:/app
  ports:
    - "8000:8000"
    - "8081:8081"
  net: appnet
  environment:
    - SELENIUM_HOST=http://selenium:4444/wd/hub

selenium:
  container_name: selenium
  image: selenium/standalone-firefox-debug
  ports:
    - "4444:4444"
    - "5900:5900"
  net: appnet

但结果是一样的

'Firefox can't establish a connection to the server at localhost:8081'

那么我怎样才能使selenium连接到Django?

我已经玩了好几天了-非常感谢您的帮助。

更多信息

另一个怪异的事情是,如果我运行./manage.py test foo,那么当测试服务器运行时 使用docker
(使用我的virtualenv等旧配置)时,我可以通过http://
localhost:8081上的任何浏览器访问服务器,并且获取提供的网页,但是如果我在docker下运行等效命令,则无法访问测试服务器。这很奇怪,因为我正在映射端口8081:8081-这与之相关吗?

注意:我正在使用OSX和Docker v1.9.1


问题答案:

我最终提出了一个更好的解决方案,该解决方案不需要我对IP地址进行硬编码。以下是我使用docker在django中运行测试的配置。

Docker组成文件

# docker-compose base file for everything
version: '2'

services:
  postgis:
    build:
      context: .
      dockerfile: ./docker/postgis/Dockerfile
    container_name: postgis
    volumes:
      # If you are using boot2docker, postgres data has to live in the VM for now until #581 fixed
      # for more info see here: https://github.com/boot2docker/boot2docker/issues/581
      - /data/dev/docker_cookiecutter/postgres:/var/lib/postgresql/data

  django:
    build:
      context: .
      dockerfile: ./docker/django/Dockerfile
    container_name: django
    volumes:
      - .:/app
    depends_on:
      - selenium
      - postgis
    environment:
      - SITE_DOMAIN=django
      - DJANGO_SETTINGS_MODULE=settings.my_dev_settings
    links:
      - postgis
      - mailcatcher

  selenium:
    container_name: selenium
    image: selenium/standalone-firefox-debug:2.52.0
    ports:
      - "4444:4444"
      - "5900:5900"

Dockerfile(对于Django)

ENTRYPOINT ["/docker/django/entrypoint.sh"]

在入口点文件中

#!/bin/bash
set -e

# Now we need to get the ip address of this container so we can supply it as an environmental
# variable for django so that selenium knows what url the test server is on
# Use below or alternatively you could have used
# something like "$@ --liveserver=$THIS_DOCKER_CONTAINER_TEST_SERVER"

if [[ "'$*'" == *"manage.py test"* ]]  # only add if 'manage.py test' in the args
then
  # get the container id
  THIS_CONTAINER_ID_LONG=`cat /proc/self/cgroup | grep 'docker' | sed 's/^.*\///' | tail -n1`
  # take the first 12 characters - that is the format used in /etc/hosts
  THIS_CONTAINER_ID_SHORT=${THIS_CONTAINER_ID_LONG:0:12}
  # search /etc/hosts for the line with the ip address which will look like this:
  #     172.18.0.4    8886629d38e6
  THIS_DOCKER_CONTAINER_IP_LINE=`cat /etc/hosts | grep $THIS_CONTAINER_ID_SHORT`
  # take the ip address from this
  THIS_DOCKER_CONTAINER_IP=`(echo $THIS_DOCKER_CONTAINER_IP_LINE | grep -o '[0-9]\+[.][0-9]\+[.][0-9]\+[.][0-9]\+')`
  # add the port you want on the end
  # Issues here include: django changing port if in use (I think)
  # and parallel tests needing multiple ports etc.
  THIS_DOCKER_CONTAINER_TEST_SERVER="$THIS_DOCKER_CONTAINER_IP:8081"
  echo "this docker container test server = $THIS_DOCKER_CONTAINER_TEST_SERVER"
  export DJANGO_LIVE_TEST_SERVER_ADDRESS=$THIS_DOCKER_CONTAINER_TEST_SERVER
fi


eval "$@"

在您的Django设置文件中

SITE_DOMAIN = 'django'

然后运行测试

docker-compose run django ./manage.py test


 类似资料:
  • 我已经将Docker配置为使用docker-compose.yml运行Postgres和Django,它运行良好。 我遇到的问题是硒无法连接到Django直播服务器。 现在(至少对我来说)django必须访问selenium才能控制浏览器,而selenium必须访问django才能访问服务器。 我尝试使用docker大使模式,使用以下配置从这里docker-compose.yml:https://

  • 当我使用Chrome驱动程序运行时,我的selenium功能测试失败,但在使用Firefox时工作正常。 Mac OS X山狮 Chrome版本22.0.1229.94 构建信息:版本:“2.2.1”,修订版:“16551”,时间:“2012-04-11 21:42:35” 系统信息:os。名称:“Mac OS X”,操作系统。拱门:“x86_64”,操作系统。版本:“10.8.2”,java。版

  • 我在facebook php webdriver上工作,我在Ubuntu上工作,selenium独立服务器与webdriver库在同一个目录下。 当我执行以下代码时,我使用本地ip地址192.168.x.x连接到linux 我经常得到这个错误“未捕获的异常”UnhandledWebDriverError“,消息是”无法在45000 ms后连接到端口7055上的主机127.0.0.1“。Firefo

  • null V: BrowserTimeout:0 调试:false DownPollingLimit:2 集线器:http://jenkins主机:jenkins端口 ID:http://node ip:node端口 null 异常的第一行说它无法解析某些东西,但我不能理解什么? 我是不是漏掉了什么?我是第一次做网格设置。

  • 警告会话0x0,因为服务器空、意外错误、关闭套接字连接并试图重新连接(org.apache.zookeeper.clientcnxn)java.net.ConnectException:连接被拒绝 zookeeper首先开始,它在2181上,当开始Kafka时,它看着2181“zookeeper”然后在我身上爆炸。忠告?

  • 我正在研究一套Selenium WebDriver(不是Selenium RC,也不使用Selenium-Server)测试,这些测试通过对mstest的命令行调用执行。我将它们分解为我们计划测试的每个浏览器的测试类(目前是Chrome、Firefox和IE9),每个测试类都有一个启动方法,它登录到测试中的系统,然后执行请求的测试,还有一个拆分方法,它使用webdriver.quit()关闭浏览器