Docker,Django和硒 - 硒无法连接 [英] Docker, Django and Selenium - Selenium unable to connect

查看:170
本文介绍了Docker,Django和硒 - 硒无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我遇到的麻烦是Selenium无法连接到Django的维护者。



现在有道理(至少对我来说)django必须访问硒以控制浏览器,硒必须访问django才能访问服务器。



我已经尝试使用docker'ambassador'模式,使用docker-compose.yml的以下配置: 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
卷:
- /var/run/docker.sock:/var/run/docker.sock
命令:-name django -name selenium

django:
dockerfile :./docker/Dockerfile-dev
build:。
命令:python /app/project/manage.py test my-app
container_name:django
volumes:
- 。:/ app
ports:
- 8000:8000
- 8081:8081
链接:
- postgis
- django-ambassador:selenium
环境:
- 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无法在localhost建立与服务器的连接:8081'

我也试过这样这里请 https://github.com/docker/compose/issues/1991
但是这不工作,因为我不能得到django连接到postgis和硒在同一时间

 'django .db.utils.OperationalError:无法将主机名称postgis翻译成地址:名称或服务不知道

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

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

django:
dockerfile:./docker/Dockerfile-dev
build:。
命令:python /app/project/manage.py test foo
container_name:django
volumes:
- 。:/ app
ports:
- 8000:8000
- 8081:8081
net:appnet
环境:
- SELENIUM_HOST = http:// selenium:4444 / wd / hub

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

但结果是一样的

 'Firefox无法与本地主机建立与服务器的连接:8081'

那么如何才能获得硒连接到django?



我已经玩了几天 - 会非常感谢任何帮助。



更多信息



另一个奇怪的是,测试服务器运行使用docker(使用我的virtualenv的旧配置等),如果我运行./manage.py测试foo我可以通过 http:// localhost:8081 上的任何浏览器访问服务器,并获得服务但是当我在docker下运行相同的命令时,我无法访问测试服务器。这是奇怪的原因我映射端口8081:8081 - 这是相关的吗?



注意:我使用的是OSX和Docker v1.9.1

解决方案

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



Docker-compose文件



 $ $ $ $ $ $ $ $ $ $ $ $ 


postgis:
build:
上下文:。
dockerfile:./docker/postgis/Dockerfile
container_name:postgis
卷:
#如果您使用的是boot2docker,postgres数据必须现在在VM中直到#581固定
#了解更多信息请看这里:https://github.com/boot2docker/boot2docker/issues/581
- / data / dev / docker_cookiecutter / postgres:/ var / lib / postgresql / data

django:
build:
上下文:。
dockerfile:./docker/django/Dockerfile
container_name:django
卷:
- 。:/ app
depends_on:
- selenium
- postgis
环境:
- SITE_DOMAIN = django
- DJANGO_SETTINGS_MODULE = settings.my_dev_settings
链接:
- 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] 



在Entrypoint文件中



 #!/ bin / bash 
set -e

#现在我们需要获取这个容器的ip地址,所以我们可以为django提供一个环境的
#变量,以便selenium知道测试服务器在哪个url
#使用下面或或者哟你可以使用
#,如$ @ --liveserver = $ THIS_DOCKER_CONTAINER_TEST_SERVER

如果[[''$ *'== *manage.py test*]] #仅在args
中添加'manage.py test',然后
#获取容器ID
THIS_CONTAINER_ID_LONG =`cat / proc / self / cgroup | grep'docker'| sed的/^.* \ ///'| tail -n1`
#取前12个字符 - 这是/ etc / hosts中使用的格式
THIS_CONTAINER_ID_SHORT = $ {THIS_CONTAINER_ID_LONG:0:12}
#search / etc / hosts for该行与ip地址将看起来像这样:
#172.18.0.4 8886629d38e6
THIS_DOCKER_CONTAINER_IP_LINE =`cat / etc / hosts | grep $ THIS_CONTAINER_ID_SHORT`
#从这个
取得ip地址THIS_DOCKER_CONTAINER_IP =`(echo $ THIS_DOCKER_CONTAINER_IP_LINE | grep -o'[0-9] \ + [。] [0-9] \ + [。] [0-9] \ + [。] [0-9] \ +')`
#添加你想要的端口
#这里的问题包括:django更改端口如果在使用(我想)
#和并行测试需要多个端口等
THIS_DOCKER_CONTAINER_TEST_SERVER =$ THIS_DOCKER_CONTAINER_IP:8081
echo这个docker容器测试服务器= $ THIS_DOCKER_CONTAINER_TEST_SERVER
export DJANGO_LIVE_TEST_SERVER_ADDRESS = $ THIS_DOCKER_CONTAINER_TEST_SERVER
fi


eval$ @



在您的django设置文件中



  SITE_DOMAIN ='django'



然后运行测试



  docker -compose运行django ./manage.py test 


I have Docker configured to run Postgres and Django using docker-compose.yml and it is working fine.

The trouble I am having is with Selenium not being able to connect to the Django liveserver.

Now it makes sense (to me at least) that django has to access selenium to control the browser and selenium has to access django to access the server.

I have tried using the docker 'ambassador' pattern using the following configuration for docker-compose.yml from here: 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"

When I check http://DOCKER-MACHINE-IP:4444/wd/hub/static/resource/hub.html I can see that firefox starts, but all the tests fail as firefox is unable to connect to django

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

I also tried this solution here https://github.com/docker/compose/issues/1991 however this is not working cause I can't get django to connect to postgis and selenium at the same time

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

I tried using the networking feature as listed below

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

but the result is the same

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

So how can I get selenium to connect to django?

I have been playing around with this for days - would really appreciate any help.

More Info

Another weird thing is that when the testserver is running not using docker (using my old config of virtualenv etc.) if I run ./manage.py test foo I can access the server through any browser at http://localhost:8081 and get served up webpages, but I can't access the test server when I run the equivalent command if I run it under docker. This is weird cause I am mapping port 8081:8081 - is this related?

Note: I am using OSX and Docker v1.9.1

解决方案

I ended up coming up with a better solution that didn't require me to hardcode the IP Address. Below is the configuration I used to run tests in django with docker.

Docker-compose file

# 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 (for Django)

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

In Entrypoint file

#!/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 "$@"

In your django settings file

SITE_DOMAIN = 'django'

Then to run your tests

docker-compose run django ./manage.py test

这篇关于Docker,Django和硒 - 硒无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆