11

I'm trying to make a docker machine available to my Windows by a host name. After creating it like

docker-machine create -d virtualbox mymachine

and setting up a docker container that exposes the port 80, how can I give that docker machine a host name such that I can enter "http://mymachine/" into my browser to load the website? When I change "mymachine" to the actual IP address then it works.

There is an answer to this question but I would like to achieve it without an entry in the hosts file. Is that possible?

Steffen Harbich
  • 2,639
  • 2
  • 37
  • 71
  • Why you don't want to use `hosts` file ? What kind of solution do you expect ? – Mickael Nov 27 '17 at 15:55
  • I try to keep it as simple as possible for multiple developers running a development environment. So I would like to avoid manual steps. – Steffen Harbich Nov 27 '17 at 17:44
  • And as far as I know the IP address of the docker machine can change e.g. when restarting. – Steffen Harbich Nov 28 '17 at 08:49
  • If your Virtualbox network is `bridged` (so VM connected to the same "physical" network), depending on your router or other `DNS` resolution "mechanisms, you might add an entry on your router or DNS server. E.g. a Fritzbox always resolves the hostname/computer name to the IP (without further administration, even dynamically with DHCP) and you can also set "Always give this device the same IP". – arne Dec 02 '17 at 18:29
  • @arnold unfortunately this is not an option in my company network. – Steffen Harbich Dec 03 '17 at 17:44
  • What docker version are you using? – fragmentedreality Dec 05 '17 at 21:51
  • @fragmentedreality 17.07.0-ce but I can update if necessary. – Steffen Harbich Dec 06 '17 at 07:28

6 Answers6

7

You might want to refer to docker documentaion: https://docs.docker.com/engine/userguide/networking/#exposing-and-publishing-ports

You expose ports using the EXPOSE keyword in the Dockerfile or the --expose flag to docker run. Exposing ports is a way of documenting which ports are used, but does not actually map or open any ports. Exposing ports is optional.

You publish ports using the --publish or --publish-all flag to docker run. This tells Docker which ports to open on the container’s network interface. When a port is published, it is mapped to an available high-order port (higher than 30000) on the host machine, unless you specify the port to map to on the host machine at runtime. You cannot specify the port to map to on the host machine when you build the image (in the Dockerfile), because there is no way to guarantee that the port will be available on the host machine where you run the image.

I also suggest reviewing the -P flag as it differs from the -p one.

Also i suggest you try "Kitematic" for Windows or Mac, https://kitematic.com/ . It's much simpler (but dont forget to commit after any changes!)

Now concerning the network in your company, it has nothing to do with docker, as long as you're using docker locally on your computer it wont matter what configuration your company set. Even you dont have to change any VM network config in order to expose things to your local host, all comes by default if you're using Vbox ( adapter 1 ==> NAT & adapter 2 ==> host only )

hope this is what you're looking for

Viktova
  • 552
  • 7
  • 19
  • 1
    And how does this help to reach my docker machine by host name? – Steffen Harbich Dec 06 '17 at 13:03
  • the docker container must be mapped to your localhost so you can access it through your webbrowser. it cannot work in any other way but only if you replicate your adapter's address and assign it to **bridge** mode so it can connect directly to the physical network with its own IP address then you can access it by typing its IP address in the browser instead of `localhost` + if you want to type a name instead of an IP address in the browser then that's a different story and you have to spoof a name for that IP using `unbound DNS` or `dnsmasq` etc.. – Viktova Dec 06 '17 at 13:12
4

If the goal is to keep it as simple as possible for multiple developers, localhost will be your best bet. As long as the ports you're exposing and publishing are available on host, you can just use http://localhost in the browser. If it's a port other than 80/443, just append it like http://localhost:8080.

If you really don't want to go the /etc/hosts or localhost route, you could also purchase a domain and have it route to 127.0.0.1. This article lays out the details a little bit more.

Example:

dave-mbp:~ dave$ traceroute yoogle.com
traceroute to yoogle.com (127.0.0.1), 64 hops max, 52 byte packets
1  localhost (127.0.0.1)  0.742 ms  0.056 ms  0.046 ms

Alternatively, if you don't want to purchase your own domain and all developers are on the same network and you are able to control DHCP/DNS, you can setup your own DNS server to include a private route back to 127.0.0.1. Similar concept to the Public DNS option, but a little more brittle since you might allow your devs to work remote, outside of a controlled network.

HammerMeetNail
  • 350
  • 1
  • 12
  • Using `localhost` is an option. How can I map the virtual box docker VM's ports to localhost? Is there an automatic way like specifiying it in the `docker-machine` command? – Steffen Harbich Dec 03 '17 at 17:26
  • With ```docker-machine``` I believe you'll need to interact with VirtualBox. It's a quick change in the settings to map Host Port to VM Port. Your devs should just need to run a VBoxManage command after they've installed Docker. See this [comment](https://stackoverflow.com/a/36286446/4108883) for specifics. – HammerMeetNail Dec 03 '17 at 21:43
4

Connecting by hostname requires that you go through hostname to IP resolution. That's handled by the hosts file and falls back to DNS. This all happens before you ever touch the docker container, and docker machine itself does not have any external hooks to go out and configure your hosts file or DNS servers.

With newer versions of Docker on windows, you run containers with HyperV and networking automatically maps ports to localhost so you can connect to http://localhost. This won't work with docker-machine since it's spinning up virtualbox VM's without the localhost mapping.

If you don't want to configure your hosts file, DNS, and can't use a newer version of docker, you're left with connecting by IP. What you can do is use a free wildcard DNS service like http://xip.io/ that maps any name you want, along with your IP address, back to that same IP address. This lets you use things like a hostname based reverse proxy to connect to multiple containers inside of docker behind the same port.

One last option is to run your docker host VM with a static IP. Docker-machine doesn't support this directly yet, so you can either rely on luck to keep the same IP from a given range, or use another tool like Vagrant to spin up the docker host VM with a static IP on the laptop. Once you have a static IP, you can modify the host file once, create a DNS entry for every dev, or use the same xip.io URL, to access the containers each time.

BMitch
  • 231,797
  • 42
  • 475
  • 450
4

If you're on a machine with Multicasting DNS (that's Bonjour on a Mac), then the approach that's worked for me is to fire up an Avahi container in the Docker Machine vbox. This lets me refer to VM services at <docker-machine-vm-name>.local. No editing /etc/hosts, no crazy networking settings.

I use different Virtualbox VMs for different projects for my work, which keeps a nice separation of concerns (prevents port collisions, lets me blow away all the containers and images without affecting my other projects, etc.)

Using docker-compose, I just put an Avahi instance at the top of each project:

version: '2'
services:
    avahi:
        image: 'enernoclabs/avahi:latest'
        network_mode: 'host'

Then if I run a webserver in the VM with a docker container forwarding to port 80, it's just http://machine-name.local in the browser.

Greg Thole
  • 41
  • 2
1
  • You can add a domain name entry in your hosts file :

    X.X.X.X  mymachine # Replace X.X.X.X by the IP of your docker machine
    
  • You could also set up a DNS server on your local network if your app is meant to be reachable from your coworkers at your workplace and if your windows machine is meant to remain up as a server.

    that would require to make your VM accessible from local network though, but port forwarding could then be a simple solution if your app is the only webservice running on your windows host. (Note that you could as well set up a linux server to avoid using docker-machine on windows, but you would still have to set up a static IP for this server to ensure that your domain name resolution works).

  • You could also buy your own domain name (or get a free one) and assign it your docker-machine's IP if you don't have rights to write in your hosts file.

But these solution may not work anymore after some time if app host doesn't have a static IP and if your docker-machine IP changes). Not setting up a static IP doesn't imply it will automatically change though, there should be some persistence if you don't erase the machine to create a new one, but that wouldn't be guaranteed either.

Also note that if you set up a DNS server, you'd have to host it on a device with a static IP as well. Your coworkers would then have to configure their machine to use this one.

vmonteco
  • 14,136
  • 15
  • 55
  • 86
0

I suggest nginx-proxy. This is what I use all the time. It comes in especially handy when you are running different containers that are all supposed to answer to the same port (e.g. multiple web-services).

nginx-proxy runs seperately from your service and listens to docker-events to update it's own configuration. After you spun up your service and query the port nginx-proxy is listening to, you will be redirected to your service. Therefore you either need to start nginx-proxy with the DEFAULT_HOST flag or send the desired host as header param with the request.

As I am running this only with plain docker, I don't know if it works with docker-machine, though.

If you go for this option, you can decide for a certain domain (e.g. .docker) to be completely resolved to localhost. This can be either done company-wide by DNS, locally with hosts file or an intermediate resolver (the specific solution depends on your OS, of course). If you then try to reach http://service1.docker nginx-proxy will route to the container that has then ENV VIRTUAL_HOST=service1.docker. This is really convenient, because it only needs one-time setup and is from then on dynamic.

fragmentedreality
  • 1,287
  • 9
  • 31