0

I am fairly new to docker and I do not know what's causing the issue to not run my python script on docker.. here is how i am creating my docker-compose.yml

version: "3.6"
services:
  app :
    build: ./app/
  db:
    build: ./database/

Here is the error :

File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
app_1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
app_1  | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
app_1  |        Is the server running on host "127.0.0.1" and accepting
app_1  |        TCP/IP connections on port 5432?

upon running docker-compose ps:

             Name                            Command               State     Ports  
------------------------------------------------------------------------------------
542132_app_final_db_1   docker-entrypoint.sh postgres    Up       5432/tcp
app_1               python abc ...               Exit 1

How do I solve it? Please help. I am fairly new to Docker/Docker-compose. Thanks!

5
  • I think you should use container name instead of 127.0.01. Try db in database connection info which is the name in your compose file. Commented Mar 26, 2021 at 13:10
  • @Saeed I am using docker-compose up --build to run it.. Commented Mar 26, 2021 at 13:12
  • @Simon ubuntu 18.04 Commented Mar 26, 2021 at 13:15
  • Also see Networking in Compose in the Docker documentation, which has lots of details about connecting between containers. Commented Mar 26, 2021 at 13:26
  • It seems you can find your answer due to the reason your question is closed Commented Mar 26, 2021 at 18:52

1 Answer 1

0

I suppose you did not configure the docker network as host network (containers live on the same network interface of the host maschine). You either need to connect the containers via a link in the docker-compose.yml file or you need to put the containers into a custom network (not the default one). I just read this is the default, so you might not need the link. Furthermore you need you python app to connect to the hostname of the database resp. the linked name:

In your case you need to take the following to measures for the standard solution:

Config connectivity between app and db

version: "3.6"
services:
  app :
    build: ./app/
    links:
      - db:database
  db:
    build: ./database/

config a proper db connection in the app

In your app you need to connect to: database:5432, not to localhost:5432

Alternate solutions

  • use network to generate a custom network, so you have dns resolution and can use db:5432 without the link
  • use host network (docker run --networking host). Not sure if that is possible in docker compose

See https://docs.docker.com/compose/networking/

Sign up to request clarification or add additional context in comments.

8 Comments

I changed the docker-compose part and added links..however that still didn't help.
Did you change the postgres connection url in your app?
links: is completely unnecessary in modern Docker; the default network that Compose provides for you will work fine.
unless you want to have an alias for your db, right.
@DavidMaze the default networks?? sorry I didn;t get you.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.