Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to local machine from inside of docker container created by docker-compose?

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?

like image 336
bellyflop Avatar asked Apr 27 '18 07:04

bellyflop


People also ask

How to connect to a docker container?

How to Connect to a Docker Container 1 Attach to a Container #. Although it is possible to run multiple processes in a container, most docker containers are running only a single process. 2 Get a Shell to a Container #. The docker exec command allows you to run commands inside a running container. ... 3 Conclusion #. ...

How do I use the exec command in a docker container?

The docker exec command allows you to run commands inside a running container. To see how the exec command works and how it can be used to enter the container shell, first, start a new container. We’ll use the official MySQL image: This will create a container named “my_mysql”. To execute a command inside the container run the following command:

How do I run multiple processes in a docker container?

Although it is possible to run multiple processes in a container, most docker containers are running only a single process. The command that is executed when starting a container is specified using the ENTRYPOINT and/or RUN instruction. The docker attach command allows you to attach your terminal to the running container.

What happens when I mount a socket in a docker container?

Mounting your host’s socket to this path means docker commands run inside the container will execute against your existing Docker daemon. This means containers created by the inner Docker will reside on your host system, alongside the Docker container itself.


1 Answers

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

like image 65
yamenk Avatar answered Oct 18 '22 09:10

yamenk