19

I have a container running a Nuxt.js (Vue.js) app, defined by docker_composes.yml:

version: '3'
services:
  vue:
    build: .
    image: registry.gitlab.com/something/app:${TAG}
    environment:
      - apiPath="http://localhost:5000/"
      - filePath="http://localhost:3000/"
    ports:
     - "${EXPOSED_PORT:-80}:${NUXT_PORT:-3000}"
    restart: always

The app is a frontend. I am running the backend on localhost, on port 5000. When the containerized app tries to connect to http://localhost:5000/ it fails.

What are my options? I don't want to put the backend into the docker_composes.yml and connect them by link. If it's the only option though, I will.

Euphe
  • 2,991
  • 5
  • 33
  • 66

4 Answers4

36

localhost in your docker-compose.yaml file refers to container.

In order to access your local machine use host.docker.internal instead of localhost.

Eduardo Cuomo
  • 13,985
  • 3
  • 93
  • 80
t-reksio
  • 1,961
  • 15
  • 10
  • Or run on the host network rather than the default bridge. – masseyb Sep 10 '19 at 17:34
  • 3
    I have tried above, but same error, unable to connect localhost, it refuses my system network `http://host.docker.internal:8042/changes?` – 151291 Mar 27 '20 at 12:18
  • I searched for this setting for _far too long_. thank you. – Eugene Jun 12 '20 at 13:45
  • @151291 did you ever find a solution to your problem? If you're running Linux this might be why: https://github.com/docker/for-linux/issues/264 – piedpiper Jan 16 '21 at 00:35
4

You could try using

network_mode: "host"

https://docs.docker.com/compose/compose-file/#network_mode

If you're on Windows or Mac you should be able to use:

docker.for.win.localhost
or
docker.for.mac.localhost

Ben A
  • 142
  • 6
4

if you want to access the host's localhost you can use the IP address of your host. in Linux use ifconfig command to get your IP address. Eg: if your host IP is 192.168.1.7 you can use 192.168.1.7:5000

Dasun_96
  • 103
  • 6
  • This is helpful if you're not managing your container, for example running an AWS Lambda function locally. Much easier to change local IP in a config file for local dev than start interfering with the container. – Craig.C Sep 22 '20 at 15:23
1

One way in which containers can communicate with one another is when they are attached to a common network. They can refer to each other using the container name. Example frontend can reach backend using backend:5000 where backend is the name the backend container.

To acheive this, create a docker network using docker network create app-network. Attach the backend container to this network using docker container attach <backend-container> app-network. Finally, update the compose file to the following:

version: '3'
services:
  vue:
    build: .
    image: registry.gitlab.com/something/app:${TAG}
    environment:
      - apiPath="http://backend:5000/" # replace backend-container name
      - filePath="http://localhost:3000/"
    ports:
     - "${EXPOSED_PORT:-80}:${NUXT_PORT:-3000}"
    restart: always
    networks:
     - app-network

networks:    
  app-network:
    external: true
yamenk
  • 33,910
  • 9
  • 61
  • 67
  • 1
    I will try it. What if backend is not a container, can I just specify an IP address? Can backend be localhost (the host running the docker-composes)? – Euphe Oct 27 '17 at 13:23
  • 1
    @Euphe If the backend is not running in a container rather is running on the host machine, you need to connect to the host. Note that localhost from the perspective of the container is not the host machine. There several ways to connect to the host machine from with the container. Check https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container – yamenk Oct 27 '17 at 13:33