如何为使用 Kafka 和 Cassandra 的应用程序设置/创建 Docker 镜像。

huangapple 未分类评论47阅读模式
英文:

How to set/create docker images for application that uses kafka and cassandra

问题

我有一个Java Spring Boot应用程序。
这个应用程序使用Cassandra作为数据存储,使用Kafka进行消息传递。
我在本地使用Cassandra、Zookeeper和Kafka的镜像来运行它们。

我还为我的Web应用程序创建了Docker文件,并构建了该文件以创建一个镜像。

我的应用程序在运行时会连接到Cassandra数据库。

当我运行应用程序(在Intelij中)时,通过连接到在Docker中运行的Cassandra,它可以正常工作。但是,当我在本地运行相同应用程序的Docker镜像时,应用程序镜像容器无法连接到本地运行的Cassandra,因此失败了。

我需要做什么来使我能够运行应用程序的Docker容器,并且还能连接到Cassandra呢?

英文:

I have a java spring boot application.
This application make use of cassandra as data storage and kafka for messaging.
I am using the image for cassandra and zookeeper and kafka locally to run them.

I also created DockerFile for my web app, and build the file to create a image.

My application when run, connect to cassandra database.

When I run the application(intelij), it works fine by connecting to cassandra(running as docker) but when I run docker image locally for the same application, the app image container is not able to connect to cassandra running locally and hence fails.

What do I need to do such that I can run the docker container for the app and also connecting to cassandra.

答案1

得分: 0

我不知道你在应用程序中使用的连接详细信息(主机:端口)的确切细节,但在这种情况下最常见的错误是使用localhost

在 Docker 容器中,localhost与在容器外部启动应用程序时的 localhost 不同。因此,在 Docker 容器内部启动应用程序时,必须为 Cassandra 部署指定不同的主机名。

默认情况下,Docker 将使用容器 ID 作为容器主机名,因此您需要将localhost替换为您的 Cassandra 容器 ID,或者可以使用--hostname选项启动 Cassandra 容器,以定义要使用的主机名。

更优雅的解决方案是使用 Docker Compose 文件,其中每个服务的主机名都是服务名称。这对于同时启动相互“通信”的多个服务非常有用。

关于容器主机名的所有解释,您可以在这里找到。

此外,您可以在这里找到有关 Docker Compose 的简介。

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

在这个示例中,每个服务的主机名分别是 webredis。您可以将此应用于您的 Compose 文件,并对应使用 Cassandra 和 Kafka 的主机名。

英文:

I don't know the exact specifics of your connection details (host:port) that you are using in your application but the most common mistake in this situation is using localhost.

localhost in the docker container is not the same as localhost when you are starting your application outside the docker container. Therefore when starting the application inside the docker container you have to specify a different hostname for your cassandra deployment.

By default docker will use the container id as the container hostname so you would have to replace localhost with your cassandra container id or alternatively start the cassandra container using --hostname in order to define a hostname that you wish to use.

In the end a more elegant solution would be to use a docker compose file where the hostname of each service is the service name. This is very helpful for starting together multiple services that "talk" to each other.

You can find everything I explained to you regarding the hostnames of containers here.

Also here you can find a nice intro into docker compose.

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

In this example the hostnames for each service are web and redis. You can apply this to your compose file and use the corresponding hostnames for cassandra and kafka.

huangapple
  • 本文由 发表于 2020年5月4日 08:30:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61583415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定