# Localhost and docker-compose networking issue

**URL:** <https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100>\
**Category:** Docker Desktop\
**Tags:** docker, macos\
**Created:** [October 6, 2016, 8:47pm UTC](https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100 "2016-10-06T20:47:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremypumphrey](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@jeremypumphrey](https://forums.docker.com/u/jeremypumphrey)\
**Post date:** [October 6, 2016, 8:47pm UTC](https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100/1 "2016-10-06T20:47:59Z")

</div>

Now that Docker for Mac uses localhost instead of an IP, should communication between docker containers and non-docker containers be possible using localhost for all? I have docker services and non-docker services running and I need them to read from a DB in docker, but I keep getting this when calling the container for dynamodb-local.

> Failed to open TCP connection to localhost:8000 (Connection refused - connect(2) for “localhost” port 8000)

In my docker-compose.yml I define the dependency

> patient-processor:  
> image: matchbox/nci-match-patient-processor:latest  
> entrypoint: /docker-compose-env.sh  
> depends\_on:  
> - dynamodb  
> ports:  
> - “3010:3010”  
> environment:  
> - AWS\_ACCESS\_KEY\_ID=${AWS\_ACCESS\_KEY\_ID}  
> - AWS\_SECRET\_ACCESS\_KEY=${AWS\_SECRET\_ACCESS\_KEY}  
> - RAILS\_ENV=development  
> dynamodb:  
> image: matchbox/dynamodb-local:latest  
> ports:  
> - “8000:8000”

From the docker-compose networking doc ([https://docs.docker.com/compose/networking/](https://docs.docker.com/compose/networking/)) it looks like all of the containers should be set up on a shared network to talk to each other. I can reach the running services from a browser with [http://localhost](http://localhost):port, but not the dynamo-local container. I’m trying to figure out if I have a dynamo issue, or a docker networking issue.  
My questions are

1. Cross-container networking: Can the containers call each other by localhost:port (localhost:8000 for example) or can they only use the compose name like dynamodb:8000?
2. Cross-system networking: Can the non-docker services running locally use the db at localhost:8000?

---

<div class="post-metadata">

**Author:** ![dmaze](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dmaze](https://forums.docker.com/u/dmaze)\
**Post date:** [October 6, 2016, 10:11pm UTC](https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100/2 "2016-10-06T22:11:32Z")

</div>

> [@jeremypumphrey](#):
>
> Now that Docker for Mac uses localhost instead of an IP, should communication between docker containers and non-docker containers be possible using localhost for all?

localhost _always_ (every platform, every Docker setup) refers to the container itself and never to anything running in any other container or the host system. (Unless you’re running a container with `--net host`, which is odd on Docker for Mac.)

On every platform with every Docker setup, if you can identify a “real” IP address for the host system, you can use it to connect to services that are running there, including other Docker containers that publish ports with `docker run -p`. Some platforms/setups provide a bridge address that you can use to access the host system (frequently 172.17.0.1/16 on native Linux and 192.168.99.100/24 on Docker Toolbox/Docker Machine) but Docker for Mac does not have this.

> [@](#):
>
> 1. Cross-container networking: Can the containers call each other by localhost:port (localhost:8000 for example) or can they only use the compose name like dynamodb:8000?

localhost _always_ refers to the current container. You have to use the compose name.

> [@](#):
>
> 1. Cross-system networking: Can the non-docker services running locally use the db at localhost:8000?

Since you declare that as a published port with a Docker Compose `ports` section, yes.

---

<div class="post-metadata">

**Author:** ![jeremypumphrey](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@jeremypumphrey](https://forums.docker.com/u/jeremypumphrey)\
**Post date:** [October 7, 2016, 4:44am UTC](https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100/3 "2016-10-07T04:44:52Z")

</div>

Thanks for the response @dmaze. The struggle I’m having is that when I bring up a docker application such as a web server, it registers port 80:80 with the host and I can reach it by calling localhost:80 on my Mac. Yes the service’s localhost is the docker container, but my host machine has 80 registered to it, left side right side of the 80:80 -p flag. This works for most of my docker containers, but when I start dynamodb-local and register 8000:8000 I don’t seem to be able to reach it as localhost:8000 as I do other containers with host mapped ports. Any idea why?

My objective is the easiest way to use dynamodb-local (running in a docker container) by both other dockers, and applications on my Mac. I’m open to suggestions.

---

<div class="post-metadata">

**Author:** ![aleveille](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/aleveille/32/9905_2.png) [@aleveille](https://forums.docker.com/u/aleveille)\
**Post date:** [October 11, 2016, 4:56pm UTC](https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100/4 "2016-10-11T16:56:48Z")

</div>

Below you’ll find a subset of a docker-compose file I’m using. I’m able to access DDB shell at [http://localhost:8000/shell/](http://localhost:8000/shell/)

```
dynamodb:
  container_name: dynamodb
  image: aleveille/dynamodb:latest
  volumes:
    - dynamodb-home:/var/lib/docker/dynamo/home
    - dynamodb-data:/var/lib/docker/dynamo/data
  ports:
   - "8000:8000"
  networks:
   - default
volumes:
  dynamodb-home:
  dynamodb-data:

```

- Note: there are much better DynamoDB images out there. We just happen to use a custom one and I didn’t bother trying with another image.

---

<div class="post-metadata">

**Author:** ![miguelsavignano](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@miguelsavignano](https://forums.docker.com/u/miguelsavignano)\
**Post date:** [July 26, 2018, 1:08am UTC](https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100/5 "2018-07-26T01:08:05Z")

</div>

> [@jeremypumphrey](#):
>
> - Cross-container networking: Can the containers call each other by localhost:port (localhost:8000 for example) or can they only use the compose name like dynamodb:8000?
> - Cross-system networking: Can the non-docker services running locally use the db at localhost:8000?

Using network\_mode: host on the service patient-processor, your patient-processor container it’s in your host network and dynamodb export the port 8000 in you host network; in this way  
you can call dynamodb using localhost:8000 inside you patient-processor container

```
patient-processor:
  image: matchbox/nci-match-patient-processor:latest
  entrypoint: /docker-compose-env.sh
  network_mode: host
  depends_on:
    - dynamodb
  ports:
    - "3010:3010"
  environment:
    - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
    - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
    - RAILS_ENV=development
dynamodb:
  image: matchbox/dynamodb-local:latest
  ports:
    - “8000:8000”

```

---

<div class="post-metadata">

**Author:** ![rhlsthrm](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rhlsthrm/32/21227_2.png) [@rhlsthrm](https://forums.docker.com/u/rhlsthrm)\
**Post date:** [October 3, 2018, 1:22am UTC](https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100/6 "2018-10-03T01:22:02Z")

</div>

Did a lot of digging around this issue and finally came across this in Docker’s own docs: [Explore networking features on Docker Desktop | Docker Docs](https://docs.docker.com/docker-for-mac/networking/). This line in particular:

> The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name `host.docker.internal` , which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker for Mac.

This worked like a charm for me!

---

<div class="post-metadata">

**Author:** ![xasbr](https://avatars.discourse-cdn.com/v4/letter/x/eada6e/32.png) [@xasbr](https://forums.docker.com/u/xasbr)\
**Post date:** [May 10, 2024, 6:16am UTC](https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100/7 "2024-05-10T06:16:51Z")

</div>

you’re beautiful. Thanks
