英文:
How to connect to RabbitMQ from a program that is running in another container?
问题
我有一个项目,其中启动了 Rabbit:
services:
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
- ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
networks:
- rabbitmq_go_net
networks:
rabbitmq_go_net:
driver: bridge
然后我创建了另一个项目来连接到这个队列。
在没有使用 Docker 的情况下,我只是使用 localhost 作为主机和端口号 5672。
我想在 Docker 中运行另一个带有数据库的项目:
version: '3'
services:
postgres:
image: postgres:12
restart: always
networks:
- rabbitmq_go_net
ports:
- '5432:5432'
volumes:
- ./db_data:/var/lib/postgresql/data
- ./app/internal/config/dbconfig/init.sql:/docker-entrypoint-initdb.d/create_tables.sql
env_file:
- ./app/internal/config/dbconfig/.env
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "devdb", "-U", "postgres" ]
timeout: 45s
interval: 10s
retries: 10
app:
build: app
networks:
- rabbitmq_go_net
depends_on:
postgres:
condition: service_healthy
volumes:
db_data:
networks:
rabbitmq_go_net:
driver: bridge
现在我无法连接到 Rabbit。我尝试创建一个不同的网络并使用相同的名称,但每次都会收到相同的错误:
FATA[2022-07-31T13:24:09Z]ipstack/internal.(*app).startConsume() app.go:43 failed to connect to RabbitMQ due dial tcp 127.0.0.1:5672: connect: connection refused
连接代码如下:
addr := fmt.Sprintf("amqp://%s:%s@%s:%s/", cfg.Username, cfg.Password, cfg.Host, cfg.Port)
其中主机是容器名称 rabbitmq。
是否可以这样做,还是需要以其他方式将程序放入容器中?我很乐意提供帮助。
英文:
i have project in which Rabbit is launched:
services:
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: 'rabbitmq'
ports:
- 5672:5672
- 15672:15672
volumes:
- ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
- ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
networks:
- rabbitmq_go_net
networks:
rabbitmq_go_net:
driver: bridge
Then I made another project that connects to this queue.
Without the doker, I just used localhost as the host and port number 5672.
I wanted to run another project in docker with a database:
version: '3'
services:
postgres:
image: postgres:12
restart: always
networks:
- rabbitmq_go_net
ports:
- '5432:5432'
volumes:
- ./db_data:/var/lib/postgresql/data
- ./app/internal/config/dbconfig/init.sql:/docker-entrypoint-initdb.d/create_tables.sql
env_file:
- ./app/internal/config/dbconfig/.env
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "devdb", "-U", "postgres" ]
timeout: 45s
interval: 10s
retries: 10
app:
build: app
networks:
- rabbitmq_go_net
depends_on:
postgres:
condition: service_healthy
volumes:
db_data:
networks:
rabbitmq_go_net:
driver: bridge
And now I can't connect to Rabbit.I tried to make a different network and with the same name, but every time I get the same error:
FATA[2022-07-31T13:24:09Z]ipstack/internal.(*app).startConsume() app.go:43 failed to connect to RabbitMQ due dial tcp 127.0.0.1:5672: connect: connection refused
Connect:
addr := fmt.Sprintf("amqp://%s:%s@%s:%s/", cfg.Username, cfg.Password, cfg.Host, cfg.Port)
Where host is the container name rabbitmq.
Is it possible to do this, or is it necessary to put programs in a container in some other way?I will be glad to help
答案1
得分: 1
我认为问题在于您的Docker网络在两个不同的compose文件中使用相同的名称,但实际上并不相同。Docker在compose文件中声明的网络名称前面会加上项目名称的前缀,以避免冲突。
您可以在这个SO回答和相关线程中找到您需要的所有信息。它展示了如何在网络上使用external
标志,以便第二个compose文件不会创建一个新的网络,并且还讨论了Docker如何使用项目名称作为网络名称的前缀,这样您就可以预测生成的网络名称。
另外,您还可以使用docker network create
提前创建网络,并在两个compose文件中都使用external
标志,这样您就不需要担心Docker的命名约定。
英文:
I think the issue is that your Docker networks aren't the same despite using the same name in the two different compose files. Docker prefixes networks declared inside compose files with the project name to avoid collisions.
Everything you're looking for can be found in this SO response and corresponding thread. It shows how to use the external
flag on a network so that the second compose doesn't create a new network, and also talks about how Docker uses the project name as a prefix for the network name so you can predict what the generated network name can be.
Alternatively, you can create the network in advance with docker network create
and use the external
flag inside both compose files so you don't need to worry about docker's naming convensions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论