7

I'm following the following tutorial on how to start a basic nginx server in a docker container. However, the example's nginx docker container runs on localhost (0.0.0.0) as shown here: nginx example image

Meanwhile, when I run it it for some reason it runs on the IP 10.0.75.2: enter image description here

Is there any particular reason why this is happening? And is there any way to get it to run on localhost like in the example?


Edit: I tried using --net=host but had no results: Result of --net=host

sgarcia.dev
  • 5,671
  • 14
  • 46
  • 80
  • I'm trying to understand your attempt to `ping 0.0.0.0:8080` since the ping command pings IP's, not tcp ports, and 0.0.0.0 is an IP reference to listen on all interfaces. The typical command I would run to see what ports have tcp services listening is `netstat -ant` – BMitch Jun 23 '16 at 02:34
  • Whoops...missed that. Thanks. Still, I tried accessing either route on chrome to no results. Also, the port status disappeared from the `docker ps` output after I ran the containers using `--net=host`. Is this normal? considering that the tutorial I'm following displays the port status as `0.0.0.0:49166->443/tcp` – sgarcia.dev Jun 23 '16 at 02:38
  • 1
    Docker doesn't forward ports to the container when you run as the host, there's nothing to forward from the host to the bridge because you're running as the host, which is not advised. The answer to the question you asked is different from the problem you are facing. You should ask how to access the port on your container from the host. For that, go to something like `https://127.0.0.1:49166`. Note with virtual machines used to run docker, there's an added network complication. – BMitch Jun 23 '16 at 11:14

2 Answers2

12

The default network is bridged. The 0.0.0.0:49166->443 shows a port mapping of exposed ports in the container to high level ports on your host because of the -P option. You can manually map specific ports by changing that flag to something like -p 8080:80 -p 443:443 to have port 8080 and 443 on your host map into the container.

You can also change the default network to be your host network as you've requested. This removes some of the isolation and protections provided by the container, and limits your ability to configure integrations between containers, which is why it is not the default option. That syntax would be:

docker run --name nginx1 --net=host -d nginx

Edit: from your comments and a reread I see you're also asking about where the 10.0.75.2 ip address comes from. This is based on how you launch the docker daemon. That IP binding is assigned when you pass the --ip flag to the daemon documentation here. If you're running docker in a vm with docker-machine, I'd expect this to be the IP of your vm.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • That makes reasonable sense, but it's not working so far (the `--net=host`), at least not on windows using docker for windows. Also, I already knew about the `-P` and `-p` options. Check my updated question for a screenshot of the output of the `--net=host` – sgarcia.dev Jun 23 '16 at 02:21
  • 2
    you can't ping ports. it looks like it works. try using telnet or netstat to check the port 8080 – Dimitrie Mititelu Jun 23 '16 at 09:42
  • @DimitrieMititelu could you write an example on how to do this? I tried netstat but I'm not exaclty sure what I'm looking for – sgarcia.dev Jun 23 '16 at 12:52
  • I like this answer Mitch, but I just can't get this to work :/ I tried running various combinations of the `--net=host` by sending the `-P` flag, not sending it at all, and sending it and mapping the ports 80 and 443 to the same ports on the outside machine in hopes of being able to access it on `localhost:80` or `localhost:8080` but so far have had no results. Could you be clearer on why when I run my machine without the `--net=host` it uses up a random ip like `10.0.75.2`, when on the example I linked it starts in `0.0.0.0`? – sgarcia.dev Jun 23 '16 at 12:58
  • @sgarcia The 10.0.75.2 ip comes from the `--ip` flag to the daemon. I've updated my answer with more details. – BMitch Jun 23 '16 at 14:22
  • I don't think I get it yet since I'm lacking a lot of knowledge of how docker works. For now, I'll make this the correct answer until I learn more about docker to understand how it works. Thank you for your time and patience Mitch – sgarcia.dev Jun 23 '16 at 15:00
  • From your [similar question](http://stackoverflow.com/q/37992816/596285) it looks like you've already answered your problem. Note that specifying how you are running Docker is important to getting good answers, an install native on linux vs a VM faces different issues especially with networking. – BMitch Jun 23 '16 at 15:06
0

A good turnaround is to set using -p flag (--publish short)

docker run -d -p 3000:80 --name <your_image_name> nginx:<version_tag>

Rafo
  • 511
  • 3
  • 17