0

I have two containers - one is a postgres container, the other a python server. I'm trying to connect the two, to no avail.

The command for postgres container is the following. The postgres image is the official one:

docker run -dp 1000:5432 --name postgres_db_mapped -e POSTGRES_PASSWORD=mysecretpassword 
-v postgre_sql_config:/etc/postgresql 
-v postgre_sql_logs:/var/log/postgresql 
-v postgre_sql_data:/var/lib/postgresql 
postgres

I can use the psql command at port 1000 locally to access the db, and commands run fine so I know its working.

The python server consists of a dockerfile, and obviously some python scripts. I won't paste the whole docker file because I think most is straightforward. I'll share the final CMD however:

CMD [ "gunicorn", "src.wsgi:app","--bind","0.0.0.0:5001" ]

This is the settings.py associated with the server, and I think maybe the root of the issues. Out the box, this is the file:

FINCREX_CS="postgres:[email protected]:1000/my-db"
SQLALCHEMY_DATABASE_URI =  'postgresql://' + FINCREX_CS
SQLALCHEMY_TRACK_MODIFICATIONS = False

There are a set of flask files that I believe are fine, and atleast try to connect to the postgres db.

I build the python image as my-api then run it as so:

docker container run  -p 5001:5001 my-api

The server looks like it boots successfully. But when I try to hit an endpoint that attempts to hit the db, I get the following error

could not connect to server: Connection refused
    Is the server running on host "127.0.0.1" and accepting
    TCP/IP connections on port 1000?

I've been changing the FINCREX_CS in a few ways to try to get it to connect. I've tried to following:

"postgres:mysecretpassword@postgres_db_mapped:1000/my-db" //use container name instead of localhost (localhost would be referring to the localhost of the python container)
"postgres:mysecretpassword@postgres_db_mapped:5432/my-db" // 5432 is docker port of the postgres container"

But neither have seemed to work. I know they are both in the bridge network. docker inspect bridge prints the following:

       "Containers": {
            "4e138792bd2d257ad169d0100f1be39c176dd0a955e2792ad6284b3966c93dcd": {
                "Name": "eloquent_bouman", //the python server
                "EndpointID": "f87bdf444fda6d67a78e44b3f6cd186c1d4265f161d58d918edd4a22c91da7a5",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "63e1a299f44d092558786d8c01e9a8d322f5731c6d6b16425568138bf12ba9b1": {
                "Name": "postgres_db_mapped",
                "EndpointID": "de519b83a7dcf026139edf183092f0c4c89f73d7679477c8035bd4f08c8f0ff4",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
3
  • 1
    You need to docker network create a network, and then docker run --net both containers on the same network. They can use each other's docker run --name container names as host names. 127.0.0.1 or localhost in Docker typically means "this container", not another container or the host system. Commented Jun 28, 2021 at 23:07
  • I didn't specify a network - doesn't that mean they're both in the bridge network? How would I refer to the postgres container in the url string? I believe I had tried that as "postgres:mysecretpassword@postgres_db_mapped:5432/my-db" Commented Jun 28, 2021 at 23:47
  • 1
    If you don't specify --net they're in the default bridge network which isn't very useful; it doesn't support things like connecting to other containers by name without additional options, and it's generally considered obsolete. Commented Jun 29, 2021 at 0:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.