1

我有一个像这样设置的 docker compose 文件:

version: "3"
services:
  web:
    image: myApp
    container_name: web
    environment:
      CASSANDRA_HOSTS: "db"
    ports:
      - "8080:8080"
    restart: unless-stopped
    depends_on:
      - cassandra
    links:
      - cassandra:db
  cassandra:
    image: cassandra
    restart: unless-stopped
    ports:
      - "9042:9042"
    expose:
      - "9042"
      - "7000"
      - "7001"
      - "9160"
    volumes:
      - "/private/cassandra_docker/:/var/lib/cassandra"

docker-compose up -d以及使用“网络”日志启动服务时:

2018/11/04 13:01:33 gocql: unable to create session: control: unable to connect to initial hosts: dial tcp 172.19.0.2:9042: connect: connection refused
2018/11/04 13:01:39 gocql: unable to dial control conn 172.19.0.2: dial tcp 172.19.0.2:9042: connect: connection refused
2018/11/04 13:01:39 could not open session with cassandra
2018/11/04 13:01:39 gocql: unable to create session: control: unable to connect to initial hosts: dial tcp 172.19.0.2:9042: connect: connection refused
2018/11/04 13:01:53 gocql: Session.handleNodeUp: 172.19.0.2:9042
2018/11/04 13:01:53 not found
2018/11/04 13:02:19 gocql: Session.handleNodeUp: 172.19.0.2:9042
2018/11/04 13:02:19 not found

当我然后使用时,docker inspect docker_cassandra_1我看到它有这个 IP 地址 - 所以分辨率工作正常:

 "Networks": {
                "docker_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "101ae9d068ce",
                        "cassandra"
                    ],
                    "NetworkID": "9b416bdf21c152a7d751c264b9f42799534800f08b17ada6d90ee92d62c390bc",
                    "EndpointID": "bc2fe3cc3fb9967e0e056d24a98b97afbc856d54f7695f8e7cfb2583a397285a",
                    "Gateway": "172.19.0.1",
                    "IPAddress": "172.19.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:13:00:02"
                }
            }

在 cassandra 的日志中,我看到该节点知道它的 IP 地址是什么,并且它也在侦听正确的端口:

10:06:37,709 StorageService.java:2289 - 节点 /172.19.0.2 状态跳转到 NORMAL cassandra_1 INFO [main] 2018-11-04 10:06:37,737 Gossiper.java:1692 - 等待八卦解决... cassandra_1 INFO [main] 2018-11-04 10:06:45,745 Gossiper.java:1723 - 没有八卦积压;进行 cassandra_1 INFO [main] 2018-11-04 10:06:46,379 NativeTransportService.java:70 - Netty 使用本机 Epoll 事件循环 cassandra_1 | INFO [main] 2018-11-04 10:06:46,496 ...开始在 /0.0.0.0:9042(未加密)上侦听 CQL 客户端...

docker inspect web我看到它没有 IP 地址并且卡在重新启动时(除非强制删除图像,否则无法停止:

"Networks": {
                "docker_default": {
                    "IPAMConfig": null,
                    "Links": [
                        "docker_cassandra_1_9415bd9be111:cassandra_1_9415bd9be111",
                        "docker_cassandra_1_9415bd9be111:db",
                        "docker_cassandra_1_9415bd9be111:docker_cassandra_1_9415bd9be111"
                    ],
                    "Aliases": [
                        "82f1f884ee5f",
                        "web"
                    ],
                    "NetworkID": "9b416bdf21c152a7d751c264b9f42799534800f08b17ada6d90ee92d62c390bc",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }

码头工人ps:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                                                       NAMES
82f1f884ee5f        myApp               "/bin/bash -c '/runA…"   6 minutes ago       Restarting (1) 5 minutes ago                                                               web
b3713664e748        cassandra           "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes                   7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:9042->9042/tcp   docker_cassandra_1

我还检查了 cassandra 中的 system.local 表 - 所有地址都是正确的(docker dns 解析到的那个)

这里有什么问题?

编辑。:

没有提到:docker exec -it docker_cassandra_1 cqlsh让我连接到数据库 - 所以它显然是

编辑2:

我做了一些进一步的测试并在 myApp 容器上安装了 nmap(并将其更改为在启动时运行 /bin/bash 而不是我的入口点脚本)

这是在“web”容器上运行的

Starting Nmap 7.60 ( https://nmap.org ) at 2018-11-04 14:13 UTC
Nmap scan report for db (172.19.0.2)
Host is up (0.000061s latency).
rDNS record for 172.19.0.2: docker_cassandra_1.docker_default

PORT     STATE SERVICE
9042/tcp open  unknown
MAC Address: 02:42:AC:13:00:02 (Unknown)

Nmap done: 1 IP address (1 host up) scanned in 0.47 seconds
root@028dcea32e53:/run.sh 
2018/11/04 14:14:01 gocql: Session.handleNodeUp: 172.19.0.2:9042
2018/11/04 14:14:01 not found

docker exec -it docker_cassandra_1 /bin/bash

让我通过外部 IP 连接

csqlsh 172.19.0.2
Connected to Test Cluster at 172.19.0.2:9042.
[cqlsh 5.0.1 | Cassandra 3.11.3 | CQL spec 3.4.4 | Native protocol v4]

这是我连接的代码:

cluster := gocql.NewCluster("db")
    cluster.Keyspace = "myApp"
    cluster.ProtoVersion = 4
    cluster.Consistency = gocql.One
    var err error
    DataSource.Client, err = cluster.CreateSession()

编辑:

当我在主机上启动我的应用程序并尝试连接到 127.0.0.1 时,它也不起作用,而端口 9042 通过 -p 9042:9042 暴露

4

1 回答 1

0

打开您的终端并首先使用以下命令验证端口是否已被使用

sudo lsof -i :9042

如果它已被使用,您将获得一个包含进程 ID 的列表。您可以使用以下命令终止该操作

sudo kill <process ID>

在此之后,使用以下命令检查所有正在运行的 docker 容器

docker ps -a

现在我们可以使用以下命令删除所有 docker 容器

docker rm -f $(docker ps -aq)

在所有这些操作之后,使用您在项目中使用的 docker compose up 命令。

docker-compose up -d

希望它会奏效!

于 2018-11-05T07:18:54.883 回答