4

I have two services - one is app and the other is db. This is a sketch of a docker compose file:

version: "2"
services:
  company:
    container_name: company-app
    extends:
      file: ../base-config.yml
      service: company
    links:
      - company-db
    env_file: ./company.env
    extra_hosts:
     - "local.company.com:127.0.0.1"

  company-db:
    container_name: company-db
    hostname: localhost
    extends:
      file: ../base-config.yml
      service: company-db
    env_file: ../db.env

Application service tries to connect to database on url jdbc:mysql://localhost/company_db

Which results in Communications link failure when running in docker container.

I've attached to running app container and check etc/hosts file and find next content:

root@d1a4391a83f4:/# 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
127.0.0.1   local.company.com
172.18.0.3  d1a4391a83f4

As I understand the last entry added by docker compose is the ip by which i can reference db container, also I find out that it could be referenced by company-db domain.

Also as you see I've tried to add hostname option to company-db service, with no success.

My question is can I somehow by changing only compose configuration achieve ability to reference company-db service via same localhost url ?

marknorkin
  • 3,904
  • 10
  • 46
  • 82

4 Answers4

0

Each container sees itself as localhost. Docker uses an internal DNS. So you can call your DB container like this:

jdbc:mysql://company-db/company_db

And remove the hostname of your DB.

Fiber Optic
  • 184
  • 5
  • 2
    the question is strictly about ability to do that via compose or other docker configuration, but not about doing it via changing the app configuration – marknorkin Nov 19 '17 at 22:32
  • 1
    Your DB is not on the same server than your app. So you have to change the configuration of your app. If you want to call a remote container and name it "localhost", you make a mistake. – Fiber Optic Nov 19 '17 at 23:26
  • @FiberOptic assume much? I have the same situation with a proxy server and a webapp. The webapp has to reference itself by its external URL which uses `localhost`. That is because 2 physical services form one logical server. – Martynas Jusevičius Oct 30 '19 at 10:52
0

You can use extra_hosts:

extra_hosts:
  - localhost:${COMPANY_DB_IP}

The downside is that you have to set the IP manually, and it can change :/

0

This is answered here: https://stackoverflow.com/a/43554707/4417769

In my use-case, it was enough to add network_mode: "host" to the central application that ends up talking with all other services. By using this, it doesn't make a difference anymore if my service runs from a terminal, or from within a docker container. It can talk to databases, etc. that are configured in my compose file by using localhost:port now.

sezanzeb
  • 816
  • 8
  • 20
-3

You can achieve this by mapping your localhost ports with containers. For your company-db service write the following

ports:
    - 3306:3306
avpav
  • 452
  • 4
  • 7