无法连接到Cassandra Docker Container

时间:2017-05-08 01:22:55

标签: c# docker cassandra

我有正式的Cassandra docker容器正在运行,但我无法从示例控制台应用程序访问。我使用Hyper-V使用docker for Windows

我使用以下命令创建容器:An unhandled exception of type 'Cassandra.NoHostAvailableException' occurred in Cassandra.dll

当我运行应用程序时,它会出现以下错误

class Program
{
    static void Main(string[] args)
    {

        var cluster = Cassandra.Cluster.Builder().AddContactPoint("172.17.0.2").Build();
        var session = cluster.Connect("Excelsior");
        var rs = session.Execute("SELECT * FROM sample_table");
        //Iterate through the RowSet
        foreach (var row in rs)
        {
            var value = row.GetValue<int>("sample_int_column");
            //do something with the value
        }
        Console.WriteLine("Hello World!");
    }
}

示例代码

docker ps

容器正在运行我通过运行以下命令进行检查:network inspect bridge

enter image description here

要获取网络信息,我运行了以下命令:[ { "Name": "bridge", "Id": "010bdd729d44a99183e45723626576fa6ce607069fb5ca0f52606eaa5395b573", "Created": "2017-05-05T22:46:13.4234063Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.17.0.0/16", "Gateway": "172.17.0.1" } ] }, "Internal": false, "Attachable": false, "Containers": { "6f6a0c0d2f77a03bc0f665d17d7c1d134667bc614e1ed53025b8b1c1c251960b": { "Name": "dev-cassandra", "EndpointID": "bded37dd1e6694705e045848b918ef35f9b7983e00f267f311d66a560a5f2f13", "MacAddress": "02:42:ac:11:00:02", "IPv4Address": "172.17.0.2/16", "IPv6Address": "" } }, "Options": { "com.docker.network.bridge.default_bridge": "true", "com.docker.network.bridge.enable_icc": "true", "com.docker.network.bridge.enable_ip_masquerade": "true", "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", "com.docker.network.bridge.name": "docker0", "com.docker.network.driver.mtu": "1500" }, "Labels": {} } ]

docker inspect cassandra

我还运行了此命令[ { "Id": "sha256:069269e2357899979df679173096db557fbdd84a82ffd2b08b139f974a36457a", "RepoTags": [ "cassandra:latest" ], "RepoDigests": [ "cassandra@sha256:b7fca4d04fdaa10ba4f187d74649f4cd59a4b5018253d58b837cdd1b02ddfe3f" ], "Parent": "", "Comment": "", "Created": "2017-03-21T20:13:44.022375976Z", "Container": "ec6b65101d9f5d335e2820809d2dda12eabaebfb21b2f362d67066521468b809", "ContainerConfig": { "Hostname": "7e9ec6cde4d1", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "7000/tcp": {}, "7001/tcp": {}, "7199/tcp": {}, "9042/tcp": {}, "9160/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "GPG_KEYS=514A2AD631A57A16DD0047EC749D6EEC0353B12C \tA26E528B271F19B9E5D8E19EA278B781FE4B2BDA", "CASSANDRA_VERSION=3.10", "CASSANDRA_CONFIG=/etc/cassandra" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD [\"cassandra\" \"-f\"]" ], "ArgsEscaped": true, "Image": "sha256:ada42f145ac8582ed811da1a1353369d4ac65d5830f9a80f657433676f1802de", "Volumes": { "/var/lib/cassandra": {} }, "WorkingDir": "", "Entrypoint": [ "/docker-entrypoint.sh" ], "OnBuild": [], "Labels": {} }, "DockerVersion": "1.12.6", "Author": "", "Config": { "Hostname": "7e9ec6cde4d1", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "7000/tcp": {}, "7001/tcp": {}, "7199/tcp": {}, "9042/tcp": {}, "9160/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "GPG_KEYS=514A2AD631A57A16DD0047EC749D6EEC0353B12C \tA26E528B271F19B9E5D8E19EA278B781FE4B2BDA", "CASSANDRA_VERSION=3.10", "CASSANDRA_CONFIG=/etc/cassandra" ], "Cmd": [ "cassandra", "-f" ], "ArgsEscaped": true, "Image": "sha256:ada42f145ac8582ed811da1a1353369d4ac65d5830f9a80f657433676f1802de", "Volumes": { "/var/lib/cassandra": {} }, "WorkingDir": "", "Entrypoint": [ "/docker-entrypoint.sh" ], "OnBuild": [], "Labels": {} }, "Architecture": "amd64", "Os": "linux", "Size": 385729595, "VirtualSize": 385729595, "GraphDriver": { "Name": "overlay2", "Data": { "LowerDir": "/var/lib/docker/overlay2/5cdd50711a30b35eac48952b286bb61e9c3d443ee07d5715c20c536a81d810e0/diff:/var/lib/docker/overlay2/b602b88eae488165e8c401065eea689acd103950f7ebdf65347ac28e87ef936f/diff:/var/lib/docker/overlay2/81c6762aa1ddd5b7c44ca8c1cff05f718cedf2cc0ba40f6f42775a166e218205/diff:/var/lib/docker/overlay2/529349dc7ce84eb4fc54ed4f677d96c5f0fdce90a26c1ed15251304dfc0e35ac/diff:/var/lib/docker/overlay2/f121b1f27ad21ceb0e2d7dbadf8c8ef86483fb52d37652fb3a1527e8d555408d/diff:/var/lib/docker/overlay2/07df2fac46233c52197c3f2c0604cbc99b7e1cc2958f22688f6e2fbd8b94840f/diff:/var/lib/docker/overlay2/373f1208a1f44a068bcf92f9d976b67f541700bee1cbd339b7c63507752581c0/diff:/var/lib/docker/overlay2/9a89ae5b436a90085a5026ed21aa094faa070b856ef375b5b8f8718543c3b281/diff:/var/lib/docker/overlay2/9ca3fdc3437863f6626a39f0b60cc73bb1bda813317b630e011d481a81703e43/diff:/var/lib/docker/overlay2/66b2788945d88001b6fa13001952691c7039e634237d434d80b224352284ac2a/diff:/var/lib/docker/overlay2/1834e9892519424500e3e5088cadb542f24054979974cf89fbe28ad4053ca73d/diff", "MergedDir": "/var/lib/docker/overlay2/900c01f3f2a541d26cde9813f21c36ae4cf44568ac7a33d24246ef14b3c610d8/merged", "UpperDir": "/var/lib/docker/overlay2/900c01f3f2a541d26cde9813f21c36ae4cf44568ac7a33d24246ef14b3c610d8/diff", "WorkDir": "/var/lib/docker/overlay2/900c01f3f2a541d26cde9813f21c36ae4cf44568ac7a33d24246ef14b3c610d8/work" } }, "RootFS": { "Type": "layers", "Layers": [ "sha256:5d6cbe0dbcf9a675e86aa0fbedf7ed8756d557c7468d6a7c64bde7fa9e029636", "sha256:aa4c186185aa90ac9c821a3c328cfbb21672dad969c9f9bc5151a5f4c341f970", "sha256:9d82a392a50d6b0a8cb4e717d02b83727429118f7f215c7a5ed5f7006557b525", "sha256:23f60ed07f46279b0e9b13521bb74570bf2d0c6b2763d5b072de4f6fd59cff3f", "sha256:8a2c71106b17a02037ab131a45d9b076338121abc9d12aa9a1bc2f818514cda0", "sha256:da91891b3c4c6cdfaff6960288166789da406078a20eddb5e961f5383d62a69f", "sha256:43ab122f258f04014a344119e7cf8187c055a618993398973f0c7eeafa5def83", "sha256:bcdcd03011e6ae49a63e85a099fd0067d7676e04e85eebecf65baa43823214ab", "sha256:08adfaf9cb5b340c7a3abbe98be3c30cc0a789a049206bf4735c6e297e12eb55", "sha256:6a2d68da5af1253a40cf952283f66b5e3ed0c8a55e34c6aac544358ed310a452", "sha256:93cb898347afdcaf66fbe8ebf1d1aa1e8ce77f4e9d73fb20e5f5531672b24bbd", "sha256:5b4b685f0f11fa1d4479fdd127852b16322b85ec1568d36cf18f40f99d37bd67" ] } } ] 来获取配置。搬运工。

ObservableList

1 个答案:

答案 0 :(得分:3)

假设您的C#代码在本地计算机上的Visual Studio中运行(而不是在Docker容器中),那么您在这里遇到了两个问题。首先,您没有将Cassandra容器上的任何端口暴露给主机。您需要的端口是9042,因为CQL二进制协议接受客户端连接。因此,例如,要将容器端口9042公开为主机上的端口9042,您需要执行以下操作:

> docker run --name dev-cassandra -d -p 9042:9042 cassandra:latest

由于您正在使用Docker for Windows,因此现在可以通过localhost127.0.0.1使用您在主机上公开的所有端口。因此,您遇到的第二个问题是,当您从主机上运行的代码连接到Docker容器时,您尝试使用的IP地址。您使用的IP地址是Docker内部网桥上的容器,因此只有当您的C#代码也在Docker容器(连接到同一个桥接网络)中运行时,IP地址才有效。

创建群集的代码应为:

var cluster = Cassandra.Cluster.Builder().AddContactPoint("127.0.0.1").Build();

驱动程序将自动默认为端口9042。希望有所帮助!