Docker、Django和Selenium-Selenium无法连接
我已经将Docker配置为使用Docker-compose.yml运行Postgres和Django,它工作正常 我遇到的问题是Selenium无法连接到Django liveserver 现在(至少对我来说)django必须访问selenium才能控制浏览器,selenium必须访问django才能访问服务器 我已经尝试使用docker“ambassador”模式,从这里使用docker-compose.yml的以下配置: 当我检查 我可以看到firefox启动了,但是所有的测试都失败了,因为firefox无法连接到djangoDocker、Django和Selenium-Selenium无法连接,django,selenium,docker,docker-compose,Django,Selenium,Docker,Docker Compose,我已经将Docker配置为使用Docker-compose.yml运行Postgres和Django,它工作正常 我遇到的问题是Selenium无法连接到Django liveserver 现在(至少对我来说)django必须访问selenium才能控制浏览器,selenium必须访问django才能访问服务器 我已经尝试使用docker“ambassador”模式,从这里使用docker-compose.yml的以下配置: 当我检查 我可以看到firefox启动了,但是所有的测试都失败了,因为
'Firefox can't establish a connection to the server at localhost:8081'
我也在这里尝试过这个解决方案
但是,这不起作用,因为我无法让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
我已经玩了好几天了-非常感谢任何帮助
更多信息
另一件奇怪的事情是,当testserver运行时,如果我运行./manage.py test foo,那么当我运行而不是使用docker(使用我以前的virtualenv配置)时,我可以通过任何浏览器访问服务器并获得服务网页,但是如果我在docker下运行相同的命令,我就无法访问测试服务器。这很奇怪,因为我正在映射端口8081:8081-这有关系吗
注意:我正在使用OSX和Docker v1.9.1每当您看到本地主机时,请首先尝试将该端口向前移植(在VM级别) 见“ (用docker机器的名称替换
默认值
:请参阅docker机器ls
)
这与docker主机级别的端口映射不同(docker主机是基于boot2docker的Linux主机)
委员会确认:
输入网络的IP地址解决了问题
我最终想出了一个更好的解决方案,不需要我硬编码IP地址。下面是我使用docker在django中运行测试时使用的配置 Docker编写文件 Dockerfile(适用于Django) 在入口点文件中 在django设置文件中 然后运行测试
我也一直在努力解决这个问题,我终于找到了一个适合我的解决方案。您可以尝试以下方法:
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容器的链接直接访问它
[Edit]我发现您也不需要显式公开django容器的8081端口。此外,我还为测试服务器使用了一系列端口,因为如果并行运行测试,您可能会得到一个“地址已在使用”错误,如前所述。我尝试了此解决方案,并在此处阅读了您的较长答案-不幸的是,这对我不起作用。还有其他想法吗?@luke_aus你需要检查一下这是什么背景。如果从一个容器访问另一个容器,“localhost”并不指实际的主机,但应该改为“django”(因为它是公开8081的容器),这样就可以工作了。因此,django启动了一个livetestserver,默认情况下它位于localhost:8081。您可以使用
/manage.py test myapp--liveserver=django:8081
更改此设置,但django抱怨该错误:[Errno-2]名称或服务未知
。正在运行curl-Ihttp://localhost:8081
在运行的django容器上返回状态“HTTP/1.0 200 OK”。运行curl-Ihttp://localhost:8081
在运行的selenium容器上返回curl:(7)连接到本地主机端口8081失败:连接被拒绝
输入网络的IP地址解决了问题!比我的答案更准确。很好的反馈+1为什么不在docker compose文件中设置DJANGO_LIVE_TEST_SERVER_地址?这是您必须指定容器名称的地方,也是进行此类设置的地方
VBoxManage controlvm "default" natpf1 "tcp-port8081,tcp,,8081,,8081"
VBoxManage controlvm "default" natpf1 "udp-port8081,udp,,8081,,8081"
# 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"
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 "$@"
SITE_DOMAIN = 'django'
docker-compose run django ./manage.py test
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"