riyoz
riyoz

Reputation: 431

Connecting couchdb container from another container

I'm trying to connect to my couchdb container from nodejs app which is running in another container. My docker-compose file is like this;

version: '3.1'
services:
    couchdb:
        image: couchdb
        container_name: my-db
        ports:
            - 5984:5984
        environment:
            COUCHDB_USER: admin
            COUCHDB_PASSWORD: password
    api:
       image: my-api
       container_name: my-api
       build: .
       command: npm run dev
       ports:
           - 8080:8080
       depends_on:
           - couchdb
       links:
           - couchdb

But I always get an error which says;

Error: connect ECONNREFUSED 172.19.0.2:5984
    at Object._errnoException (util.js:1003:13)
    at _exceptionWithHostPort (util.js:1024:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1195:14)

I saw this post mentioning the same issue; how to map database from couchdb container to another containers webapp in same docker-compose file

When I try that, I'm getting the same error. New docker compose file;

version: '3.1'
services:
    couchdb:
        image: couchdb
    container_name: my-db
    ports:
        - 5984:5984
    environment:
        COUCHDB_USER: admin
        COUCHDB_PASSWORD: password
api:
    image: my-api
    container_name: my-api
    build: .
    command: npm run dev
    ports:
        - 8080:8080
    depends_on:
        - couchdb
    links:
        - couchdb
    environment:
        DB_URL: http://admin:password@couchdb:5984

Still the same error..

I'm trying to connect the couchdb container with its service name ('couchdb') in nodejs side. I also checked if the containers are on the same network. Both are on a network named 'my_default' using bridge driver.

When I try to connect to db from local running nodejs app, there is no problem.

Is there anything that I forgot?

Upvotes: 5

Views: 1373

Answers (2)

tephyr
tephyr

Reputation: 1051

To add to your finding that couchdb had been started but was not ready, the Docker article Control startup order in Compose lists a few ways to handle that situation, including tools like wait-for-it, dockerize, or a script to poll for the state of the dependency.

Upvotes: 0

yamenk
yamenk

Reputation: 51738

You forgot to include the port for the connection:

DB_URL: http://admin:password@couchdb:5984

Upvotes: 2

Related Questions