英文:
Cannot Connect to an EC2 PostgreSQL Server from a Docker Container running in an AWS EKS Cluster Node
问题
我们有一个在AWS EC2实例上运行的PostgreSQL数据库。我们的目标是从运行在AWS EKS集群内的EC2节点上的Docker容器连接到此数据库。我们使用Python(pyodbc
)来连接数据库。
我们遇到了这个错误:
2023-02-23T15:03:49.532858159Z return pyodbc.connect(connection_str)
2023-02-23T15:03:49.532902079Z pyodbc.OperationalError: ('08001', '[08001] could not connect to server: Network is unreachable\n\tIs the server running on host "****" and accepting\n\tTCP/IP connections on port 5432?\ncould not connect to server: Network is unreachable\n\tIs the server running on host "5432" (0.0.21.56) and accepting\n\tTCP/IP connections on port 5432?\n (101) (SQLDriverConnect)')
我们通过Putty访问了EKS EC2节点。当我们运行相同的Python代码来连接数据库时,连接成功。
我们认为Docker容器存在连接问题。但我们无法找出问题所在。
这是在构建Docker镜像时使用的docker-compose.yaml
文件。
version: "3.7"
services:
dev:
image: dbimage
container_name: dbimage
build:
context: .
target: dbimage
args:
- BUILDKIT_INLINE_CACHE=1
- BUILD_DATE
- GITREF
networks:
- cloud-network
volumes:
- .:/app
networks:
cloud-network:
driver: bridge
英文:
We have a PostgreSQL database running into an AWS EC2 instance. Our target is connecting to this database from a Docker container running into an EC2 node which is running inside an AWS EKS cluster. We use Python (pyodbc
) to connect to the database.
We get this error:
2023-02-23T15:03:49.532858159Z return pyodbc.connect(connection_str)
2023-02-23T15:03:49.532902079Z pyodbc.OperationalError: ('08001', '[08001] could not connect to server: Network is unreachable\n\tIs the server running on host "****" and accepting\n\tTCP/IP connections on port 5432?\ncould not connect to server: Network is unreachable\n\tIs the server running on host "5432" (0.0.21.56) and accepting\n\tTCP/IP connections on port 5432?\n (101) (SQLDriverConnect)')
We accessed the EKS EC2 node using Putty. When we run the same Python code for connecting to the database, the connection is successful.
We believe the Docker container has a connectivity issue. But we cannot figure out the issue.
This is the docker-compose.yaml
file used when building the Docker image.
version: "3.7"
services:
dev:
image: dbimage
container_name: dbimage
build:
context: .
target: dbimage
args:
- BUILDKIT_INLINE_CACHE=1
- BUILD_DATE
- GITREF
networks:
- cloud-network
volumes:
- .:/app
networks:
cloud-network:
driver: bridge
答案1
得分: 0
这可以通过在docker run
命令中添加--network host
选项来解决。
英文:
This is solved by adding the --network host
option in the docker run
command.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论