首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker,Django和Selenium - Selenium无法连接

Docker,Django和Selenium - Selenium无法连接
EN

Stack Overflow用户
提问于 2016-02-02 02:14:53
回答 3查看 2.9K关注 0票数 3

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

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

现在(至少对我来说) django必须访问selenium来控制浏览器,selenium必须访问django才能访问服务器,这是有意义的。

我已经尝试使用了码头‘大使’模式,使用了下面配置的docker-compose.yml:https://github.com/docker/compose/issues/666

代码语言:javascript
复制
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时,我可以看到火狐启动了,但是所有的测试都失败了,因为火狐无法连接到django

代码语言:javascript
复制
'Firefox can't establish a connection to the server at localhost:8081'

我在这里也尝试过这个解决方案,https://github.com/docker/compose/issues/1991,但是这不起作用,因为我不能让django同时连接到postgis和selenium

代码语言:javascript
复制
'django.db.utils.OperationalError: could not translate host name "postgis" to address: Name or service not known'

我尝试使用网络功能,如下所示

代码语言:javascript
复制
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

但结果是一样的

代码语言:javascript
复制
'Firefox can't establish a connection to the server at localhost:8081'

那么,我如何才能让selenium连接到django呢?

我已经玩了好几天了--我真的很感激你的帮助。

更多信息

另一件奇怪的事情是,当测试服务器运行时,而不是使用docker的(使用我以前的虚拟服务器配置等等)。如果我运行./manage.py e.py测试foo,我可以通过http://localhost:8081上的任何浏览器访问服务器,并获得网页服务,但如果在docker下运行等效命令,则无法访问测试服务器。这很奇怪,因为我正在映射端口8081:8081 -这是相关的吗?

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-25 03:59:15

最后,我想出了一个更好的解决方案,不需要我硬编码IP地址。下面是我用来用docker在django中运行测试的配置。

船坞-合成文件

代码语言:javascript
复制
# 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)

代码语言:javascript
复制
ENTRYPOINT ["/docker/django/entrypoint.sh"]

在入口点文件

代码语言:javascript
复制
#!/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设置文件中

代码语言:javascript
复制
SITE_DOMAIN = 'django'

然后运行你的测试

代码语言:javascript
复制
docker-compose run django ./manage.py test
票数 5
EN

Stack Overflow用户

发布于 2016-02-02 06:13:21

每当您看到localhost时,首先尝试转发该端口(在VM级别)。

见"从外部连接到在码头容器内运行的服务

代码语言:javascript
复制
VBoxManage controlvm "default" natpf1 "tcp-port8081,tcp,,8081,,8081"
VBoxManage controlvm "default" natpf1 "udp-port8081,udp,,8081,,8081"

(将default替换为您的停靠机器的名称:请参阅docker-machine ls)

对于端口映射,在docker主机级别(这是基于引导2docker的Linux主机),这是不同的。

卢克-奥斯确认在评论中

为网络输入IP地址解决了问题!

票数 2
EN

Stack Overflow用户

发布于 2017-04-11 19:25:28

我也一直在努力解决这个问题,我终于找到了一个对我有用的解决方案。你可以试试这样的东西:

代码语言:javascript
复制
postgis:
  dockerfile: ./docker/postgis/Dockerfile
  build: .

django:
  dockerfile: ./docker/Dockerfile-dev
  build: .
  command: python /app/project/manage.py test my-app
  volumes:
    - .:/app
  ports:
    - "8000:8000"
  links:
    - postgis
    - selenium  # django can access selenium:4444, selenium can access django:8081-8100
  environment:
    - SELENIUM_HOST=http://selenium:4444/wd/hub
    - DJANGO_LIVE_TEST_SERVER_ADDRESS=django:8081-8100  # this gives selenium the correct address

selenium:
  image: selenium/standalone-firefox-debug
  ports:
    - "5900:5900"

我不认为您需要在selenium配置中包括端口4444。默认情况下,该端口是公开的,因此不需要将其映射到主机,因为django容器可以通过其指向selenium容器的链接直接访问它。

我发现您也不需要显式地公开django容器的8081端口。另外,我为测试服务器使用了一系列端口,因为如果测试是并行运行的,您可以得到一个“已在使用的地址”错误,正如讨论过的这里

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35143927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档