13

I have a docker container that has been created using docker-compose. Here is the yml file.

version: "3"
services:
  redis:
    image: "redis"
  web:
    image: "myimage"
    ports:
      - "8000:8000"
    environment:
      REDIS_HOST: redis
    volumes:
      - .:/usr/src/app
    depends_on:
      - "redis"
    command: ["npm", "start"]

From this container my web app needs to connect to the local machine because my local machine is running another web app that my docker container's web app needs to access. How do I do this? The localhost's webapp is hosted on a different port (7777).

I have already seen From inside of a Docker container, how do I connect to the localhost of the machine?, and I got it to work using "extra_hosts" option, but I want to know if there is another way to do this?

bellyflop
  • 145
  • 1
  • 1
  • 6
  • What OS are you using? – tgogos Apr 27 '18 at 07:16
  • 1
    Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – yamenk Apr 27 '18 at 07:20
  • @tgogos My local machine is Mac OS X – bellyflop Apr 27 '18 at 07:28
  • @yamenk I saw this question, but that only applies to a container created by docker run, rather than docker-compose. I tried out the solutions provided for that question, but I couldn't get anything to work. That is why I wanted a new question to focus on docker-composed containers. – bellyflop Apr 27 '18 at 07:30
  • @yamenk is right, if you want to also read the docs you can check this: [Networking features in Docker for Mac / Known limitations, use cases, and workarounds](https://docs.docker.com/docker-for-mac/networking/#known-limitations-use-cases-and-workarounds) – tgogos Apr 27 '18 at 07:46

1 Answers1

17

Docker compose is just a utility for starting multiple Docker containers. Thus almost all things that apply to starting a container using docker run are the same for containers started with docker-compose.

In particular, since you are on a MAC, there is a special DNS name inside the container that resolved to the host machine.

Depending on the docker version that you have the DNS may be a slightly different.

For Docker 18.03 and onward you can use host.docker.internal

For and above Docker 17.06 use docker.for.mac.host.internal

Below 17.06 use docker.for.mac.localhost

Thus you can connect to the web app on the machine using <dns-name>:7777

UPDATE:

To avoid hard coding the value in the code, pass this DNS name as an env variable to the container.

environment:
    REDIS_HOST: redis
    WEB_APP: host.docker.internal

Inside the app, fetch the WEB_APP environment variable and use it to connect

yamenk
  • 46,736
  • 10
  • 93
  • 87
  • host.docker.internal works, but what I am hardcoding this value in my node app. Example: axios.get(`http://host.docker.internal:7777')... What if I am not running this app from Docker container anymore, and currently only from my local machine. Is there a way that I can set host.docker.internal as an env variable in my Dockerfile, and then grab it in my script? Example: axios.get(`http://:process.env.LOCAL_HOST:7777')... – bellyflop Apr 27 '18 at 07:46