How to Map a Hostname *And* a Port with /Etc/Hosts

Can I map a hostname *and* a port with /etc/hosts?

No, that's not possible. The port is not part of the hostname, so it has no meaning in the hosts-file.

Using port number in Windows host file

The hosts file is for host name resolution only (on Windows as well as on Unix-like systems). You cannot put port numbers in there, and there is no way to do what you want with generic OS-level configuration - the browser is what selects the port to choose.

So use bookmarks or something like that.

(Some firewall/routing software might allow outbound port redirection, but that doesn't really sound like an appealing option for this.)

Can I use /etc/hosts to map localhost:3000 to a specific domain?

No.

The HOSTS file helps map hostnames to IP addresses; this has nothing to do with TCP ports.

mapping containers to docker host's /etc/hosts automatically with the same port for each container

You can't bind all 4 containers to the same port on the host. Only one container per port. But there are some workarounds:

Option 1: Use Different Ports for Each Container

  • For exmaple, bind ports 8081, 8082, 8083, and 8084.
  • In /etc/hosts, map each containers IP correctly.
  • Specify the port in addition to the hostname when connecting. Like https://fares:8081

Your /etc/hosts might look like this:

192.168.50.1 fares
192.168.50.2 rabbit
...

Option 2: Use a Reverse Proxy

You can set up an additional Docker container as a reverse proxy in your docker-compose.yml. The reverse proxy container can bind to port 8080 and forward the request to the correct container depending on the hostname. You don't need to bind ports from the other containers on the host because your reverse proxy is forwarding the requests. There's a blog post that explains how this works in detail: http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/

etc/hosts with port number (fix for foreman)

Short answer: You can't.

The host table is meant to map hostnames to IP addresses (Wiki). Ports come in at a different point.

However, you can specify the port Foreman should run on:

-p, --port

Specify which port to use as the base for this application. Should be a multiple of 1000.

How to map a local ip to a hostname?

On the remote computer, you can update your HOSTS file (located in %systemroot%\system32\drivers\etc\hosts for Windows, and /etc/hosts for OS X) and add an entry similar to the one below:

192.xxx.xxx.xxx mydev    # Use actual IP of the dev box

You will need admin (or su) privileges to make this change. After updating the hosts file, if you have difficulty getting it to work, flush your DNS cache as so:

  • For Windows (from a command prompt):

    > ipconfig /flushdns

  • For OS X (from the Terminal window):

    $ sudo dscacheutil -flushcache;sudo killall -HUP mDNSResponder;

and try again.


(Updating with info for Android)

I don't have any experience editing system files in Android, but search results suggests that it's possible, if you've rooted your device, or if you're using Android Debug Bridge (ADB).

If you're dealing with multiple devices and don't want to deal with the headache of managing hosts file changes for every device, you can also look into using a free dynamic DNS service provider, such as noip.com or changeip.com. However, these will require you to run a client on your web server and you'll also need to access your server from an internet IP, which means editing your router settings to map the port on the router to the port on the local machine. If you plan to go this route, first see if your router already supports some sort of built-in dynamic DNS setup (e.g. my Motorola Cable Modem & Router offers this). If you're comfortable with the idea of replacing your router's firmware with something like DD-WRT, the custom O/S of DD-WRT also supports an internal DNS setup where it can resolve internal IP addresses to local machine names.



Related Topics



Leave a reply



Submit