无法连接到在Docker容器中运行的Cassandra
对于我的应用程序,有一个docker容器数据库
运行Cassandra。我将端口9160
和9042
公开到主机机器,但是由于某种原因,我无法从主机上连接到Cassandra。我能够使用cqlsh
从其他Docker容器连接到它,仅当我将其他容器放入同一网络中时。
我的docker-compose
file(我使用Docker-compose
):
database:
image: cassandra
container_name: database
depends_on:
- kafka
ports:
- 9042:9042
- 9160:9160
volumes:
- ./:/code
environment:
- CASSANDRA_START_RPC=true
- CASSANDRA_BROADCAST_ADDRESS=database
entrypoint: "/code/scripts/cassandra.sh"
frontend:
container_name: frontend
build:
context: driver/
volumes:
- ./:/code
network_mode: service:database
environment:
- BOOTSTRAP_SERVER=kafka:9092
- CASSANDRA_HOST=localhost
entrypoint: "/code/scripts/frontend.sh"
where cassandra.sh
和front> frontend.sh 只需通过运行脚本并分别安装某些软件包来初始化数据库。
使用上面的设置,我可以通过简单地在frontend
容器中调用cqlsh
来使用CQLSH。但是,如果我从frontend
中删除行network_mode:network_mode:database
,然后尝试使用cqlsh database
或cqlsh $进行连接Cassandra_host
,即使我设置了cassandra_host =数据库
。执行上述任何命令给我:
连接错误:('无法连接到任何服务器',{'172.27.0.5:9042':ConnectionRefusedError(111,“ 111,”尝试连接到[('172.27.0.5',,,,,,,,,, 9042)]。
结果,即使我曝光了端口9042,我也无法从主机计算机运行cqlsh
。主机是2020年iMac运行MacOS MACOS MONTEREY 12.3.1
> netstat -anvp tcp | awk 'NR<3 || /LISTEN/'
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state) rhiwat shiwat pid epid state options
tcp46 0 0 *.29092 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.9042 *.* LISTEN 131072 131072 7099 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.7000 *.* LISTEN 131072 131072 7099 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.54196 *.* LISTEN 131072 131072 7099 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.7199 *.* LISTEN 131072 131072 7099 0 0x0100 0x00000006
tcp46 0 0 *.8080 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.7077 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.4040 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.9160 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.9042 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.2181 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.6463 *.* LISTEN 131072 131072 1241 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.49390 *.* LISTEN 131072 131072 826 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.45623 *.* LISTEN 131072 131072 826 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.49380 *.* LISTEN 131072 131072 826 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.49379 *.* LISTEN 131072 131072 826 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.15292 *.* LISTEN 131072 131072 770 0 0x0000 0x0000020f
tcp6 0 0 *.5000 *.* LISTEN 131072 131072 465 0 0x0100 0x00000006
tcp4 0 0 *.5000 *.* LISTEN 131072 131072 465 0 0x0100 0x00000006
tcp6 0 0 *.7000 *.* LISTEN 131072 131072 465 0 0x0100 0x00000006
tcp4 0 0 *.7000 *.* LISTEN 131072 131072 465 0 0x0100 0x00000006
tcp6 0 0 *.49198 *.* LISTEN 131072 131072 494 0 0x0100 0x00000006
tcp4 0 0 *.49198 *.* LISTEN 131072 131072 494 0 0x0100 0x00000006
。花了几个小时来寻找解决方案,并在Stackoverflow和其他网站上查看了几乎所有可能的帖子,但是这些解决方案对我来说都不适用。我非常感谢您的帮助。
For my application, there is a Docker container database
which runs Cassandra. I expose the ports 9160
and 9042
to the host machine, but for some reason, I cannot connect to Cassandra from my host. I am able to connect to it from other Docker containers using cqlsh
only if I put the other container in the same network.
My docker-compose
file (which I run with docker-compose up
):
database:
image: cassandra
container_name: database
depends_on:
- kafka
ports:
- 9042:9042
- 9160:9160
volumes:
- ./:/code
environment:
- CASSANDRA_START_RPC=true
- CASSANDRA_BROADCAST_ADDRESS=database
entrypoint: "/code/scripts/cassandra.sh"
frontend:
container_name: frontend
build:
context: driver/
volumes:
- ./:/code
network_mode: service:database
environment:
- BOOTSTRAP_SERVER=kafka:9092
- CASSANDRA_HOST=localhost
entrypoint: "/code/scripts/frontend.sh"
where cassandra.sh
and frontend.sh
simply initialise the database by running a script, and installing some packages respectively.
Using the above setup, I am able to use cqlsh by simply calling cqlsh
in the frontend
container. If, however, I remove the line network_mode: service:database
from frontend
, and try to connect using cqlsh database
or cqlsh $CASSANDRA_HOST
, even after I set CASSANDRA_HOST=database
. Doing any of the aforementioned commands gives me:
Connection error: ('Unable to connect to any servers', {'172.27.0.5:9042': ConnectionRefusedError(111, "Tried connecting to [('172.27.0.5', 9042)]. Last error: Connection refused")})
.
As a result, I am not able to run cqlsh
from my host machine either, even though I exposed the port 9042. The host machine is a 2020 iMac running macOS Monterey 12.3.1.:
> netstat -anvp tcp | awk 'NR<3 || /LISTEN/'
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state) rhiwat shiwat pid epid state options
tcp46 0 0 *.29092 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.9042 *.* LISTEN 131072 131072 7099 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.7000 *.* LISTEN 131072 131072 7099 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.54196 *.* LISTEN 131072 131072 7099 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.7199 *.* LISTEN 131072 131072 7099 0 0x0100 0x00000006
tcp46 0 0 *.8080 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.7077 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.4040 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.9160 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.9042 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp46 0 0 *.2181 *.* LISTEN 131072 131072 911 0 0x0100 0x00000006
tcp4 0 0 127.0.0.1.6463 *.* LISTEN 131072 131072 1241 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.49390 *.* LISTEN 131072 131072 826 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.45623 *.* LISTEN 131072 131072 826 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.49380 *.* LISTEN 131072 131072 826 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.49379 *.* LISTEN 131072 131072 826 0 0x0100 0x00000106
tcp4 0 0 127.0.0.1.15292 *.* LISTEN 131072 131072 770 0 0x0000 0x0000020f
tcp6 0 0 *.5000 *.* LISTEN 131072 131072 465 0 0x0100 0x00000006
tcp4 0 0 *.5000 *.* LISTEN 131072 131072 465 0 0x0100 0x00000006
tcp6 0 0 *.7000 *.* LISTEN 131072 131072 465 0 0x0100 0x00000006
tcp4 0 0 *.7000 *.* LISTEN 131072 131072 465 0 0x0100 0x00000006
tcp6 0 0 *.49198 *.* LISTEN 131072 131072 494 0 0x0100 0x00000006
tcp4 0 0 *.49198 *.* LISTEN 131072 131072 494 0 0x0100 0x00000006
I've spent hours looking for a solution for this and have viewed nearly every possible post on StackOverflow and other sites on this topic, but none of the solutions work for me. I would greatly appreciate some help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案
命令“ Docker Pull Cassandra:最新” 正在利用容器化平台Docker,从官员中下载Cassandra数据库管理系统(DBMS)的最新版本Docker Hub存储库。用技术术语:
docker:一个平台,使开发人员可以将应用程序及其依赖项打包成轻巧的便携式容器。容器在不同环境中提供一个一致的环境,从而更容易部署和扩展应用程序。
拉动:此Docker命令用于从容器注册表中获取容器图像。在这种情况下,它正在拉动Cassandra图像。
Cassandra:最新:指定要拉的图像。 “ Cassandra”是Cassandra DBM的Docker映像的名称,并且“:最新”表示应检索最新的图像版本。
Docker Hub:基于云的注册表服务,Docker用户可以共享和访问容器图像。卡桑德拉官方图像托管在Docker Hub上。
因此,当您运行时,“ Docker Pull Cassandra:最新” Docker将从Docker Hub下载Cassandra Image的最新版本,使其可在计算机上进行本地使用。
Docker Pull Cassandra:最新的
命令:
docker run - 名字卡桑德拉-p 127.0.0.0.1:9042:9042:9042 -p 127.0.0.0.0.0.0.1:9160:9160:9160 -d cassandra
您提供的命令是用于运行Cassandra的docker命令容器。让我们用技术术语将其分解:
Docker Run:此命令用于创建和启动基于指定图像的新容器。
- 名称Cassandra:将名称“ Cassandra”分配给新创建的容器。这有助于在随后的Docker命令中引用容器。
-p 127.0.0.1:9042:9042:将容器的端口9042(卡桑德拉的本机运输端口)映射到主机端口9042。这意味着您可以在127.0.0.1:1::::: 9042。
-p 127.0.0.1:9160:9160:将容器的端口9160(用于节俭通信)映射到主机端口9160。这使您可以在127.0.0的主机上使用Thrift在127.0.0 .0.1:9160。
-d:在背景(分离模式)中运行容器,从而可以继续使用终端。
Cassandra:指定用于创建容器的Docker映像的名称。在这种情况下,这是Cassandra图像。
因此,当您运行命令时,“ docker run -name cassandra -p 127.0.0.0.1:9042:9042 -p 127.0.0.0.0.1:9160:9160:9160 -d cassandra,”使用指定的端口映射,使Cassandra在提供的端口的主机机上可以访问。
利用此命令将使(com.datastax.driver.core.core.exceptions.transportexception:[/127.0.0.1:9042]无法连接))
例外像魔术一样消失!
如何连接到Cassandra CQL Shell?
artwellmamvura@artwells-macbook-pro〜%docker exec-it cassandra cqlsh
连接到127.0.0.1:9042的测试群集
[CQLSH 6.1.0 |卡桑德拉4.1.3 | CQL规格3.4.6 |本机协议V5]
使用帮助来帮助。
cqlsh&gt;
Solution
The command "docker pull cassandra:latest" is utilizing Docker, a containerization platform, to download the latest version of the Cassandra database management system (DBMS) image from the official Docker Hub repository. In technical terms:
Docker: A platform that enables developers to package applications and their dependencies into lightweight, portable containers. Containers provide a consistent environment across different environments, making it easier to deploy and scale applications.
pull: This Docker command is used to fetch container images from a container registry. In this case, it's pulling the Cassandra image.
cassandra:latest: Specifies the image to be pulled. "cassandra" is the name of the Docker image for the Cassandra DBMS, and ":latest" indicates that the latest available version of the image should be retrieved.
Docker Hub: A cloud-based registry service where Docker users can share and access container images. The official Cassandra image is hosted on Docker Hub.
So, when you run "docker pull cassandra:latest," Docker will download the most recent version of the Cassandra image from Docker Hub, making it available for local use on your machine.
docker pull cassandra:latest
Now for the command:
docker run --name cassandra -p 127.0.0.1:9042:9042 -p 127.0.0.1:9160:9160 -d cassandra
The command you provided is a Docker command used to run a Cassandra container. Let's break it down in technical terms:
docker run: This command is used to create and start a new container based on a specified image.
--name cassandra: Assigns the name "cassandra" to the newly created container. This is helpful for referencing the container in subsequent Docker commands.
-p 127.0.0.1:9042:9042: Maps the container's port 9042 (Cassandra's native transport port) to the host machine's port 9042. This means you can access Cassandra on the host machine at 127.0.0.1:9042.
-p 127.0.0.1:9160:9160: Maps the container's port 9160 (used for Thrift communication) to the host machine's port 9160. This allows you to interact with Cassandra using Thrift on the host machine at 127.0.0.1:9160.
-d: Runs the container in the background (detached mode), allowing you to continue using the terminal.
cassandra: Specifies the name of the Docker image to use for creating the container. In this case, it's the Cassandra image.
So, when you run the command "docker run --name cassandra -p 127.0.0.1:9042:9042 -p 127.0.0.1:9160:9160 -d cassandra," it starts a new Cassandra container with specified port mappings, making Cassandra accessible on the host machine at the provided ports.
Utilising this command will make the (com.datastax.driver.core.exceptions.TransportException: [/127.0.0.1:9042] Cannot connect))
Exception to disappear like magic!
How Connect to Cassandra CQL Shell ?
artwellmamvura@Artwells-MacBook-Pro ~ % docker exec -it cassandra cqlsh
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.3 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
cqlsh>
问题是由设置入口点引起的:
事实证明,如果没有提供入口点,则仅由图像初始化一些Cassandra设置。如果提供了一个,则必须自己设置这些设置,我没有这样做。我添加了入门点
cassandra.sh
,因为我想通过运行文件create.cql
:我最终创建了另一个Docker容器来初始化数据库数据库已经启动:
The issue was caused by setting the entrypoint:
As it turns out, some Cassandra settings are only initialised by the image if no entrypoint is provided. If one is provided, you must set these settings yourself, which I didn't do. I added the entrypoint
cassandra.sh
because I wanted to initialise the database by running a filecreate.cql
:I ended up creating another Docker container that simply initialises the database once the database has started up: