testcontainers / testcontainers/testcontainers-python

Can't connect to PostgreSQL container from another container when running on Mac as host

Đang mở
#138 6 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

🍏 macos 🐛 bug 👀 requires attention 📦 package: postgres
Ngôn ngữ chính
Python
Star
2.3k
Fork
386
Merge trung bình
4 giờ 40 phút
Pull request đã merge (30 ngày)
1

Mô tả

In our current setup we have some Python code running on a docker container which uses testcontainers to create a PostgreSQL database container we can test with. All necessary requirements to start a new container from a docker image are met, and the host machine's docker client is used to start the new container. When looking at the DB container, it starts up successfully, we can access it from localhost with the port assigned to it when exposing and with the container's IP with the port we wanted to expose (5432). The problem comes if we try to access it with the Gateway IP of the network the 2 containers are in, and the port assigned to it when exposing (which is what testcontainers is doing as per my understanding).

This last part only fails when the host the 2 containers are started on is a MacOS machine, it works fine on Linux. To replicate the issue the following script can be run on a docker container that is started on a MacOS machine (with the host's docker client mounted up as per the requirements in this repo's README):

from testcontainers.postgres import PostgresContainer
import sqlalchemy

with PostgresContainer("postgres:9.6") as postgres:
    e = sqlalchemy.create_engine(postgres.get_connection_url())
    print(e.execute("select version()"))

We ran with Python 3.6 and newest versions of testcontainers, sqlalchemy and psycopg2. The above script should fail in a container started on Mac and work in a container started on Linux.

This is because the networking implementation is different between Linux and Mac. When exposing ports on Mac without providing a binding port (so docker finds a port for itself), the HostConfig/PortBindings for the newly started container gets set to "0". In contrast, it is left as an empty String on Linux. The "0" value causes the issue. We've opened a ticket about this on the docker for mac repo which has more info about this particular issue: https://github.com/docker/for-mac/issues/5588

Currently the work around we use in our code is to explicitly bind ports instead of just exposing them like so:

from testcontainers.postgres import PostgresContainer
import sqlalchemy

with PostgresContainer("postgres:9.6").with_bind_ports(5432, 47000) as postgres:
    e = sqlalchemy.create_engine(postgres.get_connection_url())
    print(e.execute("select version()"))

This version works on both Mac and Linux as the host because HostConfig/PortBindings gets updated with the port we've provided on containers started on both Mac and Linux. The downside is we always have to manually bind and find a port that is available.

The suggestion in the issue that I linked above is that for portability, the internal DNS and IP of containers should be used for communicating between containers rather than the Gateway IP. Would it make sense to make the change in testcontainers so it would use those to access the DB container rather than the Gateway IP? What was the specific reason for which Gateway IP was chosen as the preferred method of communication between containers?

I am happy to have a go at making the change in this repo if you accept contributions.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với các entry point get_connection_url và with_bind_ports của PostgresContainer, sau đó chạy bản tái hiện SQLAlchemy được cung cấp trong các container trên Mac và Linux. Theo dõi cách địa chỉ của container cơ sở dữ liệu và cổng được expose được chọn; hoàn thành khi truy vấn kết nối mà không cần chọn thủ công một cổng host và hoạt động nhất quán trên cả hai host.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
docker, postgresql, python, sqlalchemy
Lĩnh vực
databases, devops, networking
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.