Communicate between docker containers

时间:2017-08-05 11:53:29

标签: selenium docker selenium-webdriver

I have a server running two docker containers.

One docker container is a web server, and another is a selenium-chromedriver.

From the container with the web server, I want to be able to connect to the chrome driver.

And the web server is started like this:

docker run -i -p 80:80 -d '<name>:<version>' /sbin/my_init

The selenium driver is started like this:

docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:3.4.0

From the host machine I am able to get information from the selenium container with curl:

curl http://localhost:4444/wd/hub/status
# => {"state":"success","sessionId":..........

However, from the web server container I only get:

curl: (7) Failed to connect to localhost port 4444: Connection refused

What can I do to let the container containing the web server be able to connect to the other container?

1 个答案:

答案 0 :(得分:3)

I think you have to create networks: docker.com work with networks help

Basic container networking example:

First, create and run two containers, container1 and container2:

$ docker run -itd --name=container1 busybox

18c062ef45ac0c026ee48a83afa39d25635ee5f02b58de4abc8f467bcaa28731

$ docker run -itd --name=container2 busybox

498eaaaf328e1018042c04b2de04036fc04719a6e39a097a4f4866043a2c2152

Create an isolated, bridge network to test with.

$ docker network create -d bridge --subnet 172.25.0.0/16 isolated_nw

06a62f1c73c4e3107c0f555b7a5f163309827bfbbf999840166065a8f35455a8

Connect container2 to the network and then inspect the network to verify the connection:

$ docker network connect isolated_nw container2

$ docker network inspect isolated_nw

[
    {
        "Name": "isolated_nw",
        "Id": "06a62f1c73c4e3107c0f555b7a5f163309827bfbbf999840166065a8f35455a8",
        "Scope": "local",
        "Driver": "bridge",
        "IPAM": {
            "Driver": "default",
            "Config": [
                {
                    "Subnet": "172.25.0.0/16",
                    "Gateway": "172.25.0.1/16"
                }
            ]
        },
        "Containers": {
            "90e1f3ec71caf82ae776a827e0712a68a110a3f175954e5bd4222fd142ac9428": {
                "Name": "container2",
                "EndpointID": "11cedac1810e864d6b1589d92da12af66203879ab89f4ccd8c8fdaa9b1c48b1d",
                "MacAddress": "02:42:ac:19:00:02",
                "IPv4Address": "172.25.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {}
    }
]

Notice that container2 is assigned an IP address automatically. Because you specified a --subnet when creating the network, the IP address was chosen from that subnet.

As a reminder, container1 is only connected to the default bridge network.