docker - 连接到同一个网络的两个容器(node和redis)似乎看不到对方
问题描述
节点server
和redis
容器在同一个中定义docker-compose.yml
:
version: '3.9'
services:
backend:
image: 127.0.0.1:5000/backend
container_name: backend
hostname: backend
restart: always
networks:
- wellerman
volumes:
- ./:/app/src
build: .
environment:
REDIS_HOST: 'localhost'
REDIS_PORT: 6379
ports:
- '3231:4000'
depends_on:
- redis
links:
- redis
redis:
container_name: redis
hostname: redis
restart: always
networks:
- wellerman
image: redis:latest
command: --port 6379
ports:
- '6379:6379'
networks:
wellerman:
external: true
网络配置如下(在后端节点server
起床时拍摄快照;redis
虽然工作正常,但未报告为已连接到网络):
docker network inspect wellerman
[
{
"Name": "wellerman",
"Id": "xiqats19pc6gb6tvmre1thyhh",
"Created": "2021-08-26T15:56:49.678973091Z",
"Scope": "swarm",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "10.0.15.0/24",
"Gateway": "10.0.15.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"dd114e62bb8dffe866e1293af30cb7ad86516dfaaae5eae04b9347507b26023c": {
"Name": "backend_backend.1.w1suc8ytt8jfxvs9syspx1uv6",
"EndpointID": "dd114e62bb8dffe866e1293af30cb7ad86516dfaaae5eae04b9347507b26023b",
"MacAddress": "02:42:0a:00:0f:25",
"IPv4Address": "10.0.15.37/24",
"IPv6Address": ""
},
"lb-wellerman": {
"Name": "wellerman-endpoint",
"EndpointID": "dd114e62bb8dffe866e1293af30cb7ad86516dfaaae5eae04b9347507b26023a",
"MacAddress": "02:42:0a:00:0f:26",
"IPv4Address": "10.0.15.38/24",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.driver.overlay.vxlanid_list": "4111"
},
"Labels": {},
"Peers": [
{
"Name": "ba2cce29a569",
"IP": "10.12.63.2"
}
]
}
]
server
如果没有与实例建立连接,则节点终止redis
,并且由于这是持续存在的情况,它会不断崩溃。
Redis
似乎按预期工作:
docker service inspect t7znrullne5x
[
{
"ID": "t7krullne5xvkze8xr3znlott",
"Version": {
"Index": 233743
},
"CreatedAt": "2021-08-26T15:55:05.529166159Z",
"UpdatedAt": "2021-08-26T15:55:05.534908594Z",
"Spec": {
"Name": "backend_redis",
"Labels": {
"com.docker.stack.image": "redis:latest",
"com.docker.stack.namespace": "backend"
},
"TaskTemplate": {
"ContainerSpec": {
"Image": "redis:latest@sha256:66ce9bc742609650afc3de7009658473ed601db4e926a5b16d239303383bacad",
"Labels": {
"com.docker.stack.namespace": "backend"
},
"Args": [
"--port",
"6379"
],
"Hostname": "redis",
"Privileges": {
"CredentialSpec": null,
"SELinuxContext": null
},
"StopGracePeriod": 10000000000,
"DNSConfig": {},
"Isolation": "default"
},
"Resources": {},
"RestartPolicy": {
"Condition": "any",
"Delay": 5000000000,
"MaxAttempts": 0
},
"Placement": {
"Platforms": [
{
"Architecture": "amd64",
"OS": "linux"
},
{
"OS": "linux"
},
{
"OS": "linux"
},
{
"Architecture": "arm64",
"OS": "linux"
},
{
"Architecture": "386",
"OS": "linux"
},
{
"Architecture": "mips64le",
"OS": "linux"
},
{
"Architecture": "ppc64le",
"OS": "linux"
},
{
"Architecture": "s390x",
"OS": "linux"
}
]
},
"Networks": [
{
"Target": "xiqats19pc6gb6tvmre1thyhh",
"Aliases": [
"redis"
]
}
],
"ForceUpdate": 0,
"Runtime": "container"
},
"Mode": {
"Replicated": {
"Replicas": 1
}
},
"UpdateConfig": {
"Parallelism": 1,
"FailureAction": "pause",
"Monitor": 5000000000,
"MaxFailureRatio": 0,
"Order": "stop-first"
},
"RollbackConfig": {
"Parallelism": 1,
"FailureAction": "pause",
"Monitor": 5000000000,
"MaxFailureRatio": 0,
"Order": "stop-first"
},
"EndpointSpec": {
"Mode": "vip",
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 6379,
"PublishedPort": 6379,
"PublishMode": "ingress"
}
]
}
},
"Endpoint": {
"Spec": {
"Mode": "vip",
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 6379,
"PublishedPort": 6379,
"PublishMode": "ingress"
}
]
},
"Ports": [
{
"Protocol": "tcp",
"TargetPort": 6379,
"PublishedPort": 6379,
"PublishMode": "ingress"
}
],
"VirtualIPs": [
{
"NetworkID": "iyu2r81xo9fjiyhxve36dcuxx",
"Addr": "10.0.0.143/24"
},
{
"NetworkID": "xiqats19pc6gb6tvmre1thyhh",
"Addr": "10.0.15.7/24"
}
]
}
}
]
从上面的信息看,它redis
与wellerman
网络连接(network inspect
虽然没有显示)。
查看来自节点的日志,server
我发现有关ECONNREFUSED (-111)
何时尝试访问redis
实例的信息。
如果我更换REDIS_HOST
环境。变量从localhost
to redis
,我得到ENOTFOUND (-3008)
错误。
此外,主机的设置127.0.0.1
或实际 IP 没有太大变化:这两个容器之间的连接似乎不存在。
您建议检查或更改什么?
解决方案
我设法让这件事发挥作用。
这是我所做的:
- 将 redis 作为一个单独的实例启动:
docker run --name redis -p 6379:6379 -d redis:latest
- 将所有环境变量移至项目的
Dockerfile
,将 redis 主机地址设置为服务器的 IP 地址。然后构建项目,标记并作为服务启动:
docker build . -t backend:v1
docker service create --detach=true --name backend -p 3231:4000 backend:v1
几秒钟后,整个实例开始运行并准备好进行扩展。
推荐阅读
- r - 如何在r中绘制两个正镜像条形图?
- flutter - 如何在 ThemeData 中实现 TextTheme
- android - Ktlint 与 lint 的区别?
- python - model.fit() 在 Keras 中给了我 'KeyError: input_1'
- python - 使用 python 构建本地数据库并且由于“关键错误”而无法继续(下面的完整代码)
- git - git 确保提交被完全推送
- c++ - Leetcode 987:二叉树的垂直顺序遍历:为什么我得到 TLE?
- javascript - 函数返回对象而不是数组,无法 .Map
- keycloak - 如何在 keycloak 中为电子邮件添加新的“重置操作”?
- c# - 向 ASP.NET Core 3.1 标准 JSON BadRequest 响应添加详细消息