compose: Links between containers doesn't work with version 2

I use docker-compose v1.6.0 and docker engine 1.10.0.

I have the following docker-compose.yml:

version: '2'

services:
  db:
    image: postgres
    environment:
      POSTGRES_DB: test
      POSTGRES_USER: test
      POSTGRES_PASSWORD: test1234
    volumes:
      - db-data:/var/lib/postgresql/data
  web:
    build: .
    command: /bin/bash -c "bundle exec rake db:migrate && bundle exec rails s -p 3000 -b 0.0.0.0"
    environment:
      RAILS_ENV: production
      DB_HOST: db
      DB_PORT: 5432
      DB_USER: test
      DB_DB: test
      DB_PASSWORD: test1234
      SECRET_KEY_BASE: 5ffbf99dcae1dee8500abd759114f473a942cb23a23db2ef8ce61972d2eaeedbb656ddb184ff0175ab4f8c5bcb29fa860505a140a6442608192dee1e3b168928
    ports:
      - "3000:3000"
    volumes:
      - public:/myapp/public
volumes:
  db-data:
    driver: local
  public:
    driver: local

But while starting containers with docker-compose up command, I see following error: web_1 | PG::ConnectionBad: could not translate host name "db" to address: Name or service not known.

I tried to find the reasons for this error:

[user@localhost test_project]$ docker-compose run web bash
Starting test_project_db_1
root@9bd26d6496bd:/myapp# cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.3  9bd26d6496bd
root@9bd26d6496bd:/myapp# ping db
ping: unknown host

I added the following to web section in the docker-compose, but this has no effect:

    links:
      - db:db
    depends_on:
      - db

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15

Most upvoted comments

I have the same issue, and I didn’t find any way to fix it.

My environment:

OS:

Ubuntu 16.04

Docker:

Client: Version: 1.12.1 API version: 1.24 Go version: go1.6.3 Git commit: 23cf638 Built: Thu Aug 18 05:33:38 2016 OS/Arch: linux/amd64

Server: Version: 1.12.1 API version: 1.24 Go version: go1.6.3 Git commit: 23cf638 Built: Thu Aug 18 05:33:38 2016 OS/Arch: linux/amd64

Compose:

docker-compose version 1.8.1, build 878cff1 docker-py version: 1.10.3 CPython version: 2.7.12 OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016

Note that even for some containers, I need to run them with –net==host to be able to access them form host

Since version 1.10 Docker embeded a DNS server. The /etc/hosts file is not modified.

According to docker-compose network documentation, it should work without link declaration.

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

With my own containers, it’s OK. You can list the networks via docker network ls and inspect your default network with docker network inspect XXX_default