P
P
PtrGrd2021-10-30 17:29:19
Docker
PtrGrd, 2021-10-30 17:29:19

Docker. docker compose. How to connect a container to another container?

I have a python script that requests data from postgres inside docker. Locally everything works fine, but in the docker it's a disaster. Python cannot connect to Postgres container. I connected them in docker-compose, tried to add them to the general network, nothing happened. Please help, we need to make sure that the container with the script contacts the database container for data transfer.

My docker-compose:

version: '3.8'

services:
 script:
  build: ./script/
  command: /script/src/script.py
  container_name: script-mtg
  depends_on:
    - db
 db:
  image: postgres:latest
  container_name: dbpg-mtg
  restart: always
  volumes:
    - ./ .database/postgres/data:/var/lib/postgresql/data
  environment:
    - POSTGRES_DB=users
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres
  ports:
    - "5432"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
antares4045, 2021-10-30
@antares4045

docker-compose raises its own isolated dns.
There is a suspicion that you are connecting to postgres on the localhost:5432 server, and under compose you need db:5432

V
Vadim, 2021-10-30
@Viji

Here is an example...
https://github.com/stefanopassador/Tutorial_Docker...
also wonder how you run these containers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question