简体   繁体   中英

I have Postgres server connection error when I use docker?

My application works fine on my system but when I build a docker image by docker composer and then run the application it shows the error below.

However, I resolved it by changing the ip address from 127.0.0.1 => 0.0.0.0 in the Flask setting as below and the docker image works fine on my system. But when I run it on another ubuntu it gives me the same error and changes the IP to 127.0.0.1.

if __name__ == "__main__":
    app.run(host='**0.0.0.0**',debug=False)

ERROR:

Traceback (most recent call last):
  File "./main.py", line 22, in <module>
    ps.create_table()  # create the table if is not existed
  File "/prj/Modules/postgres.py", line 49, in create_table
    connection = get_connection()
  File "/prj/Modules/postgres.py", line 36, in get_connection
    return psycopg2.connect(f"dbname={db_name} user={user} password={password} host={host}")
  File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
    Is the server running on host **"127.0.0.1"** and accepting
    TCP/IP connections on port 5432?

you can have access to my code and my docker hub:

docker hub: applia65/duplicatebugreportsearchengine Github: https://github.com/ghasemieh/Duplicated-Bug-Report-Detection-System/blob/master/main.py

Basically I have 3 container my-app, Postgres, and MongoDB.

I appreciate it if you help me with this problem. Thanks

From the trace, the PostgreSql isn't accessible. Can you try updating your docker compose and add expose statement like below:

expose:
- 5432

This will allow access only to the linked services unless "ports" is also specified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM