Selenium无法通过docker-compose run到达docker容器

时间:2019-06-30 14:37:18

标签: selenium docker docker-compose

我有以下docker-compose.yml,它启动了一个chrome独立容器和一个nodejs应用程序:

version: '3.7'

networks:
  selenium:

services:
  selenium:
    image: selenium/standalone-chrome-debug:3
    networks:
      - selenium
    ports:
      - '4444:4444'
      - '5900:5900'
    volumes:
      - /dev/shm:/dev/shm
    user: '7777:7777'

  node:
    image: node_temp:latest
    build:
      context: .
      target: development
      args:
        UID: '${USER_UID}'
        GID: '${USER_GID}'
    networks:
      - selenium
    env_file:
      - .env
    ports:
      - '8090:8090'
    volumes:
      - .:/home/node
    depends_on:
      - selenium
    command: >
      sh -c 'yarn install &&
             yarn dev'

我正在如下运行容器:

docker-compose up -d selenium
docker-compose run --service-ports node sh

,然后从外壳中启动e2e。

运行e2e测试时,可以从节点容器到达硒(通过http://selenium:4444),但是无法从硒容器到达节点。

我已经通过VNC进入硒容器并将浏览器指向http://node:8090进行了测试。 (但是,可以通过http://localhost:8090主机上访问节点容器)。

我首先认为docker-compose run不会将正在运行的容器添加到正确的网络中,但是通过运行docker network inspect test_app我会得到以下信息:

[
{
    "Name": "test_app_selenium",
    "Id": "df6517cc7b6446d1712b30ee7482c83bb7c3a9d26caf1104921abd6bbe2caf68",
    "Created": "2019-06-30T16:08:50.724889157+02:00",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
        "Driver": "default",
        "Options": null,
        "Config": [
            {
                "Subnet": "172.31.0.0/16",
                "Gateway": "172.31.0.1"
            }
        ]
    },
    "Internal": false,
    "Attachable": true,
    "Ingress": false,
    "ConfigFrom": {
        "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {
        "8a76298b237790c62f80ef612debb021549439286ce33e3e89d4ee2f84de3aec": {
            "Name": "test_app_node_run_78427bac2fd1",
            "EndpointID": "04310bc4e564f831e5d08a0e07891d323a5953fa936e099d20e5e384a6053da8",
            "MacAddress": "02:42:ac:1f:00:03",
            "IPv4Address": "172.31.0.3/16",
            "IPv6Address": ""
        },
        "ef087732aacf0d293a2cf956855a163a081fc3748ffdaa01c240bde452eee0fa": {
            "Name": "test_app_selenium_1",
            "EndpointID": "24a597e30a3b0b671c8b19fd61b9254bea9e5fcbd18693383d93d3df789ed895",
            "MacAddress": "02:42:ac:1f:00:02",
            "IPv4Address": "172.31.0.2/16",
            "IPv6Address": ""
        }
    },
    "Options": {},
    "Labels": {
        "com.docker.compose.network": "selenium",
        "com.docker.compose.project": "test_app",
        "com.docker.compose.version": "1.24.1"
    }
}
]

其中显示了两个在“硒”网络上运行的容器。但是,我不确定节点容器是否在网络上正确地别名以及这是否正确。

我在这里缺少一些配置吗?

1 个答案:

答案 0 :(得分:0)

就像docker-compose run一样,容器的名称也不同,以逃避docker-compose.yml中提到的服务名称空间。 http://node:8090因此无法访问。

我通过添加--name标志来解决此问题,如下所示:

docker-compose run --service-ports --name node node sh

编辑:

我花了一段时间才注意到,但是我使实现复杂化了很多。可以通过添加主机网络来简化上述docker-compose.yml。这只是公开了localhost上所有正在运行的容器,并使它们通过其指定端口在localhost上可访问。考虑到我不需要任何封装(用于开发),以下docker-compose.yml就足够了:

version: '3.7'

services:
  selenium:
    image: selenium/standalone-chrome:3
    # NOTE: port definition is useless with network_mode: host
    network_mode: host
    user: '7777:7777'

  node:
    image: node_temp:latest
    build:
      context: .
      target: development
      args:
        UID: '${USER_UID}'
        GID: '${USER_GID}'
    network_mode: host
    env_file:
      - .env
    volumes:
      - .:/home/node
    command: >
      sh -c 'yarn install &&
             yarn dev'