栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Docker,Django和Selenium-Selenium无法连接

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Docker,Django和Selenium-Selenium无法连接

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

Docker组成文件

# docker-compose base file for everythingversion: '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/bashset -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 argsthen  # 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_SERVERfieval "$@"

在您的Django设置文件中

SITE_DOMAIN = 'django'

然后运行测试

docker-compose run django ./manage.py test


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401263.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号