Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see a linked container as localhost?

I have these two containers:

api:
  image: social-learning
  ports:
    - "3000:3000"
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
    - ../api:/app
  expose:
    - "3000"
web:
  image: social-learning-frontend
  ports:
    - "4200:4200"
    - "9000:9000"
  command: ember serve -p 4200
  volumes:
    - .:/app
  links:
    - api
  expose:
    - "3000"

When I do:

docker exec `docker ps -a | grep 'frontend_web_1' | awk '{print $1 }'` curl http://localhost:3000

I always get connection refused. I can access the other container through its IP but I'd prefer to do it as localhost.

Is it possible?

like image 916
Dougui Avatar asked Jun 21 '15 09:06

Dougui


People also ask

How do I access localhost Docker?

Accessing the Host With the Default Bridge Mode You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

How do I run a localhost container?

A simple solution to this in a Linux machine is to use the --network=”host” option along with the Docker run command. After that, the localhost (127.0. 0.1) in your Docker container will point to the host Linux machine. This runs a Docker container with the settings of the network set to host.

What is localhost IP from Docker container?

Ok, your localhost server has a default docker interface docker0 with IP address 172.17. 0.1 .


1 Answers

There seems to be some confusion here.

It looks like you are trying to log into the web container and access the api container using the localhost address? This isn't going to work.

The best solution is just to use the link you've declared; within the web container you should be able to do curl http://api:3000. This works because Docker has added an entry to /etc/hosts for the api container for you.

Accessing via localhost will only work from the Docker host, where the container ports are published. I guess you could access the Docker host from within the container via the Docker bridge (e.g. curl 172.17.42.1:3000), but that seems pointlessly complicated and brittle.

like image 145
Adrian Mouat Avatar answered Oct 13 '22 20:10

Adrian Mouat