Jason Strimpel
Jason Strimpel

Reputation: 15466

Map container to hostname other than localhost in Docker for Mac

I am creating an Nginx container that I would like to access locally at http://api. Using Docker Machine, I assumed running docker-machine create default and docker-machine ip default to receive the IP and editing my hosts file to something like this:

# docker-machine ip default --> 192.168.99.100
192.168.99.100 api

should map requests to api\ to the Docker Machine IP and serve my content.

Two things are confusing me:

  1. I launch Docker through the Mac App and can create Nginx containers and access content at http://localhost. However, running docker-machine ls returns no machines. This is confusing because I thought Docker had to run on a VM.
  2. Starting from scratch and starting Docker Machine, then spinning up containers seems to have no effect. In other words, I still can access content at http://localhost but not http://api

Instead of accessing my container at http://localhost I want to access it at http://api. How do I do this?

I'm using Docker for Mac 17.12 and Docker Machine 0.14.

Upvotes: 1

Views: 1805

Answers (1)

Adiii
Adiii

Reputation: 59896

On the base of your this question:

Instead of accessing my container at http://localhost I want to access it at http://api. How do I do this?

Your docker run command:

docker run -it --rm --name test --add-host api:192.168.43.8 -p 80:80 apachehttpd

1st Thing: The --add-host flag add value to /etc/hosts in your container /etc/hosts so http://api will also response inside the container if ping inside that container. This is how will ping response inside container enter image description here

2nd Thing: Edit your host etc/hosts file and add

api 192.168.43.8 [your ip]

This is how you can see in Browser. enter image description here

Upvotes: 2

Related Questions