Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker access localhost port from container

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.

like image 615
Euphe Avatar asked Oct 27 '17 10:10

Euphe


2 Answers

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

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

like image 165
t-reksio Avatar answered Oct 03 '22 21:10

t-reksio


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

like image 39
Ben A Avatar answered Oct 03 '22 21:10

Ben A