Docker Community Forums

Share and learn in the Docker community.

Access container from dev machine (by ip/dns name)

docker

(Whitecolor) #1

Questions like seem to be asked but I really don’t get it at all.

I have a window 10 dev machine host with docker for windows installed. Besides others networks it has DockerNAT netwrok with IP 10.0.75.1

I run some containers with docker-compose:

version: '2’
services:
web:
build: .
container_name: web

It created some network bla_default, container has IP 172.18.0.4, of cource I can not connect to 172.18.0.4 from host - it doesn’t have any netwrok interface for this.

What should I do to be able to access this container from dev machine? (by IP) and if possible by some DNS name? What should I add to my docker-compose.yml, how to configure networks?


Connecting to containers IP address
(Michael Friis) #2

Containers that expose ports are available on localhost: https://docs.docker.com/docker-for-windows/#/step-4-explore-the-application-and-run-examples


(Whitecolor) #3

Thanks, I know, but I wan’t to understand is it possible to get access to the container directly to its full ip source, and better to access it using

currently from my dev machine (docker for windows beta) I can not connect to 172.x.x.x container addresses because on this machine I have only NAT interface 10.0.75.0 related to docker. Any advice on this?


(David Maze) #4

No(*), it’s not. Publish ports using docker run -p and access them through localhost, as @friism recommended.

(*) I know for sure that this is impossible on “native” Docker for Mac. I’m guessing “native” Docker for Windows is the same. On a Docker Toolbox installation, if you were really familiar with both the host and Linux IP routing setups, you could probably make it work. But it’s a lot of effort to work around functionality that Docker provides out of the box.


(Whitecolor) #5

Well for now I’ve been able at least to acess 172.x.x.x ips from dev machine by adding needed route:

route /P add 172.0.0.0 MASK 255.0.0.0 10.0.75.2

Now want to figure aout is it possible to connect to them using some DNS names (maybe http://docs.master.dockerproject.org/engine/userguide/networking/configure-dns/) to get immutable connectivity way.


(Whitecolor) #6

Using this https://github.com/aacebedo/dnsdock - we can quite easiely enable advance DNS discovery and access containers by custom DSN name from dev machine.

SO, YES WE CAN! Oh this not actual.
MAKE DOCKER GREAT AGAIN!


(Dvdarias) #7

To specifically solve this problem I created a simple “etc/hosts” domain injection tool that resolves names of local Docker containers on the host. Just run:

docker run -d \
    -v /var/run/docker.sock:/tmp/docker.sock \
    -v /etc/hosts:/tmp/hosts \
    --name docker-hoster \
    dvdarias/docker-hoster

You will be able to access a container using the container name, hostname, container id and vía the network aliases they have declared for each network.

Containers are automatically registered when they start and removed when they are paused, dead or stopped.


(Eaverdeja) #8

Fit as a glove for me! Thanks @dvdarias :slight_smile: