我有一个这样设置的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状态跳转到 普通cassandra_1 INFO [main] 2018-11-04 10:06:37,737 Gossiper.java:1692-等待八卦解决... cassandra_1 INFO [主要] 2018-11-04 10:06:45,745 Gossiper.java:1723-没有八卦 积压;进行cassandra_1 INFO [主要] 2018-11-04 10:06:46,379 NativeTransportService.java:70-使用本机Epoll的Netty 事件循环cassandra_1 | INFO [主要] 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
}
}
docker 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
让我连接到数据库-显然已经建立了
EDIT2:
我做了进一步的测试并将nmap安装在myApp容器上(并将其更改为在启动时运行/ bin / bash而不是我的入口点脚本)
这是在“网络”容器上运行的
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暴露了
答案 0 :(得分: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
希望它能工作!