3

I define postgres server in docker-compose.yml:

db:
    image: postgres:9.5
    expose:
        - 5432

Then in another docker container I tried to connect to this postgres container. But it gives an error with warning:

Is the server running on host "db" (172.22.0.2) and accepting
data-service_1  |   TCP/IP connections on port 5432?

Why container can't to connect to another by provided information (host="db" and port=5432)?

PS Full docker-compose.yml:

version: "2"
services:
    data-service:
        build: .
        depends_on:
            - db
        ports:
          - "50051:50051"
    db:
        image: postgres:9.5
        depends_on:
            - data-volume
        environment:
            - POSTGRES_USER=cobrain
            - POSTGRES_PASSWORD=a
            - POSTGRES_DB=datasets
        ports:
          - "8000:5432"
        expose:
            - 5432
        volumes_from:
            - data-volume
            # - container:postgres9.5-data
        restart: always

    data-volume:
        image: busybox
        command: echo "I'm data container"
        volumes:
            - /var/lib/postgresql/data
2
  • please post full docker-compose.yml Commented May 16, 2017 at 0:05
  • @AlexKarshin I added it. Commented May 16, 2017 at 0:11

2 Answers 2

3

Solution #1. Same file.

To be able to access the db container, you have to define your other containers in context of docker-compose.yml. When containers are started, each container gets all other containers mapped in /etc/hosts.

Just do

version: '2'
services:
  web:
    image: your/image
  db:
    image: postgres:9.5

If you do not wish to put your other containers into the same docker-compose.yml, there are other solutions:

Solution #2. IP

Do docker inspect <name of your db container> and look for IPAddress directive in the result listing. Use that IPAddress as host to connect to.

Solution #3. Networks

Make your containers join same network. For that, under each service, define:

services:
  db:
    networks:
     - myNetwork

Don't forget to change db for each container you are starting.

I usually go with the first solution during development. I use apache+php as one container and pgsql as another one, a separate DB for every project. I do not start more than one setting of docker-compose.yml, so in this case defining both containers in one .yml config is perfect.

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

6 Comments

But build: . is the same as image: your/image. I can't see difference between solution 1 and mine one.
actually, on closer inspection, I think depends_on might give you problems, because you are defining a recursive dependency
recursive? No, it's just a chain of dependencies data-service --> db --> data-volume.
ah, yes, sorry, it's naming getting mixed up :) I will delete my answer and will try to come up with the right solution
@RomaKarageorgievich what happens if you define expose as string? I.e. - "5432"? All docs define it as string.
|
-1

the depends on is not correct. i would try to use other paramters like LINKS and environment:

version: "2"
    services:
        data-service:
            build: .
            links:
                - db
            ports:
              - "50051:50051"
            volumes_from: ["db"]
            environment:
                DATABASE_HOST: db
        db:
            image: postgres:9.5
            environment:
                - POSTGRES_USER=cobrain
                - POSTGRES_PASSWORD=a
                - POSTGRES_DB=datasets
            ports:
              - "8000:5432"
            expose:
                - 5432
            #volumes_from:
                #- data-volume
                # - container:postgres9.5-data
            restart: always

        data-volume:
            image: busybox
            command: echo "I'm data container"
            volumes:
                - /var/lib/postgresql/data

this one works for me (not postgres but mysql)

1 Comment

depends_on has nothing to do with getting containers communicate with each other..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.