Windows 10 Home :can't reach ‘localhost:8000’ using Docker - windows

I'm beginning to use Docker and following the tutorial (https://docs.docker.com/get-started/part2/) and I am stuck at this point:
docker run --publish 8000:8080 --detach --name bb bulletinboard:1.0
When I try to reach localhost:8080 it says "impossible to reach the site".
When I run docker-machine ip and visiting http://192.168.99.100:8080 with a web client, it does not work.

Please access with port 8000 like localhost:8000 because you mapping docker port from 8080 to 8000 to access from outsaide docker container

Change Port from 8080 to 8000 like this http://192.168.99.100:8000 because you connect machine port 8000 to docker port 8080.
I hope it may help you.

When you use docker commands, always remember..
outside_world -> docker_world
In your 8000:8080 means. You open your container to outside world on port 8000 , inside docker its 8080
Or simply mapping docker port 8080 to 8000 outside.
In this configuration, you should use localhost:8000
docker-machine ip this is completely different network, thats why you can not access it. Its container network.

Related

Localhost vs 0.0.0.0 with Docker on Mac OS

I am reading the docs here and I find myself a bit confused, since running
docker run --name some-mysql -p 3306:3306 -d mysql
or
docker run --name some-mysql -p 127.0.0.1:3306:3306 -d mysql
then mysql --host localhost --port 3306 -u root gives me the following error :
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2).
But running mysql -u root -p --host 0.0.0.0 works.
Does someone have an explanation ?
With docker port forwarding, there are two network namespaces you need to keep track of. The first is inside your container. If you listen on localhost inside the container, nothing outside the container can connect to your application. That includes blocking port forwarding from the docker host and container-to-container networking. So unless your container is talking to itself, you always listen on 0.0.0.0 with the application you are running inside the container.
The second network namespace is on your docker host. When you forward a port with docker run -p 127.0.0.1:1234:5678 ... that configures a listener on the docker host interface 127.0.0.1 port 1234, and forwards it to the container namespace port 5678 (that container must be listening on 0.0.0.0). If you leave off the ip, docker will publish the port on all interfaces on the host.
So when you configure mysql to listen on 127.0.0.1, there's no way to reach it from outside of the container's networking namespace. If you need to prevent others outside of your docker host from reaching the port, configure that restriction when publishing the port on the docker run cli.
As described in the mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/connecting.html), when you connect to 127.0.0.1 with the client, it'll try to use the unix sockets to perform this operation. Normally this would work fine since it's on the same host. In Docker the socket file is not available.

Docker Toolbox - Localhost not working

So I'm using Docker Toolbox because I don't have Hyper-V on my machine since it's not Windows 10 pro. Everything seems to work fine, but when I try to go on my browser 0.0.0.0:80 it always returns me: This site can’t be reached
But when I run the command: docker container ps I get the following: 0.0.0.0:80->80/tcp meaning that this address should work. I searched across stackoverflow and github issues. Now I'm stuck.
Am I missing something?
Thanks,
Mark
EDIT:
Using docker-machine ip default returns me 192.168.99.100. I run that on port 80. I still get the same result except that the address becomes the container id: https://fd677edg12
I run that command on cmd to find my ipv4: cmd /k ipconfig /all. Put the result with the port and it returns the same thing: https://fd677edg12
Docker Toolbox doesn't get as many conveniences as Docker for Windows, but you're correct in using it since you're on Home edition.
In Toolbox, nothing will be localhost, and will be 192.168.99.100 by default, since it's running a Linux VM in VirtualBox.
So if you run docker run -p 80:80 nginx
(notice I had to publish a port for 192.168.99.100 to listen on that port)
Then going to http://192.168.99.100 should work.
I initially had a few issues with accessing my Applications at localhost:8080 while using DockerToolBox and OracleVM VirtualBox.
In VirtualBox:
Click the appropriate machine (probably the one labeled "default")
Settings
Network > Adapter 1 > Advanced > Port Forwarding
Click "+" to add a new Rule
Set Host Port 8080 & Guest Port 8080; be sure to leave Host IP and Guest IP empty
Run the command:
docker run -p 8080:8080 ${image_id}
I was following docker for windows tutorial in https://docs.docker.com/docker-for-windows/#set-up-tab-completion-in-powershell and got stuck in step #6 when test nginx in the web browser. Seems I faced a similar problem since I also use Windows Home and don't have Hyper-V. My workaround is quite simple:
check your docker IP default
$ docker-machine ip default
192.168.99.100
Go to Oracle Virtual Machine to set for port forwarding. Make sure the network setting is NAT, and add port forwarding.
Host IP: 127.0.0.1, Guest IP: 192.168.99.100, port all set to 80
like this
Try again to your browser and run http://localhost or http://127.0.0.1 (can add the port 80 also). It should run.
The thing is that the nginx IP is meant to be accessible within the docker Virtual Machine, so that we need that port forwading setting in order to access it directly in the host machine's browser
You can use localhost instead of '192.168.99.100' by following the instructions:
Step #01:
docker-machine ip default
You will see the default IP
Step #02:
docker-machine stop default
Step #03:
Open VirtualBox Manager (from the start programs in windows search for VirtualBox Manager)
Select your Docker Machine VirtualBox image (e.g.: default)
Open Settings -> Network -> Advanced -> Port Forwarding
Add your app name, the desired host port and your guest port
i.e, app name : nginx, host: 127.0.0.1, host port: 80, guest port: 80
Step #04:
Now you’re ready to start your Docker Machine by executing the following:
docker-machine start default
Then just start your Docker container and you will be able to access it via localhost.
Have a look here for details.
To map the ports expected to localhost instead of hitting the docker-machine IP directly, you can use the VirtualBox CLI.
If the docker-machine VM (here called default) is running, add and delete rules like this:
> VBoxManage.exe controlvm "default" natpf1 "nginx,tcp,,8888,,8888"
> VBoxManage.exe controlvm "default" natpf1 delete nginx
If the VM is not running, or you want to stop before altering it:
> docker-machine stop
> VBoxManage.exe modifyvm "default" --natpf1 "nginx,tcp,,8888,,8888"
> VBoxManage.exe modifyvm "default" --natpf1 delete "nginx"
> docker-machine start
Where the format of the port forwarding rule is [<name>],tcp|udp,[<hostip>],<hostport>,[<guestip>], <guestport>.
Note that in VirtualBox, you want to map to the host port of Docker map, not the internal container port. You're mapping host -> VM, then Docker maps VM -> container.
See the VirtualBox docs.
This is another easy way to avoid typing the ip 192.168.99.100.
Go to C:\Windows\System32\drivers\etc\hosts and add at the end of the file:
192.168.99.100 docker.awesome or any name of your liking.
Save the file (You need to have admin rights so make sure you right click on the file and run as administrator to be able to save it when you edit it).
Go to your chosen domain name, docker.awesome:8080 in this case and there you have it.
After lot of trials, I was able to get this bulletin board.
The docker run command I used - docker run -p 4680:8080 --name bb bulletinboard:1.0
Here, 4680 is localhost port number.
8080 is container port number, the port at which the container will be listening. This port number is mentioned in the EXPOSE command in the Dockerfile.
Then, go to web-browser and type 192.168.99.100:4680
Here, 192.168.99.100 is the docker machine IP address (use command -> docker-machine ip)
After this, your browser page should open to -
Hope this helps you all!!

Can't access docker container on port 80 on OSX

In my current job we have development environment made with docker-compose.
One container is nginx, which provide routing to other containers.
Everything seems fine and work to my colleague on windows and osx. But on my system (osx El Capitan), there is problem with accessing nginx container on port 80.
There is setup of container from docker-compose.yml
nginx:
build: ./dockerbuild/nginx
ports:
- 80:80
links:
- php
volumes_from:
- app
... and more
In ./dockerbuild/nginx there is nothing special, just nginx config as we know it from everywhere.
When I run everyting with docker-compose create and docker-compose start. Then docker ps give me
3b296c1e4775 docker_nginx "nginx -g 'daemon off" About an hour ago Up 47 minutes 0.0.0.0:80->80/tcp, 443/tcp docker_nginx_1
But when I try to access it for example via curl I get error. curl: (7) Failed to connect to localhost port 80: Connection refused
I try to run container with port 81 and everything works fine.
Port is really binded to docker
22:47 $ sudo lsof -i -n -P | grep TCP
...
com.docke 14718 schovi 38u IPv4 0x6e9c93c51ec4b617 0t0 TCP *:80 (LISTEN)
...
Firewall in osx is turned off and I have no other security.
if you are using docker-for-mac:
Accessing by localhost:80 is correct, though you still have to ensure you do not have a local apache/nginx service running. Often leftovers from boxen/homebrew exist binding that port, because thats what developers did back then :)
if you are using dockertoolbox/virtualbox/whatever hypervisor
You will not be able to access it by localhost, by by the docker-machine ip, so write docker-machine ip default and the use http://$ip:80 in your browser
if that does not help
Ensure your nginx container actually does work, so connect to the container: docker exec -i -t <containerid> bash
and then run ps aux nginx or if telnet is installed try to connect to localhost
Solved!
Problem was, that long long time ago I installed pow (super simple automated rails server which run application on app_name.local domain). And this beast left LaunchAgent script which update pf to forward port 80 to pow port.
In my current job we have development environment made with docker-compose.
A privilege to use.
[W]hen I try to access [nginx on port 80] for example via curl I get error.
Given there's nothing from causing you from accessing docker on your host os you should look at the app running inside the container to ensure it's binding to the correct host, e.g. 0.0.0.0 and not localhost.
For example, if you're running Nuxt inside a container with nuxt-ts observe Nuxt will default to localhost thereby causing the container not to connect to the docker network whereas npx nuxt-ts -H 0.0.0.0 gets things squared away with the container's internal server connecting to the ip of the docker network used (verify ip like docker container inspect d8af01990363).

How to access webserver running on localhost from a docker container on a network?

I have the following system configuration:
Docker container running on user defined network
docker-machine (with VirtualBox on OS:X forwarding port 9000 to 9000)
Local webserver running on http://localhost:9000
I do not know how to make a basic http request against this webserver, from within my docker container.
To test this I am using:
docker exec testcontainer curl --data "foobaz=foo" http://{hostname}:9000/
where I have tried, for hostnames:
'localhost'
'127.0.0.1'
'192.168.99.100' (docker-machine IP)
Each time I receive errors or timeouts. When I run the curl command locally (not in docker and on my host OS:X machine) I am able to successfully post the http request.
I cannot disconnect the docker container from my user-defined network. I also cannot add my webserver to that network, as it is not running in a container. Also, I know it is trivial to connect the other way (curl to a webserver running in a docker container) but this is not my use case.
How can I successfully route that http request from the docker container which is part of a user defined network to my localhost webserver?
You can do this with the actual IP address of your local computer.
So for example, if your en0 IP is 10.100.20.32 on your host OS, you can run:
docker exec testcontainer curl --data "foobaz=foo" http://10.100.20.32:9000/
which will successfully allow you to make the http requests.
Note that if you are doing this from a container on the host docker network, this is trivial, as you can directly access localhost or 0.0.0.0 without having to use the actual machine IP.
You might want to check what the IP address of the container is - you can find this out by running docker inspect.
However, if you want to access the server process running in your container using the docker machine IP, then you should "expose" port 9000 that your contained app is listening on (using Dockerfile) - in this way, you will still need to figure out which port the 9000 container port is mapped to (this shows when you list your containers via $ docker ps. You can also specify port binding option in command line when starting your container like this: $ docker run -p 9000:9000 <your-container>.

How to access web page served by nginx web server running in docker container

We are trying to use docker to run nginx but for some reason I'm unable to access the nginx web server running inside the docker container.
We have booted a Docker Container using the following Dockerfile: https://github.com/dwyl/learn-docker/blob/53cca71042482ca70e03033c66d969b475c61ac2/Dockerfile
(Its a basic hello world using nginx running on port 8888)
To run the container we used:
docker run -it ubuntu bash
we determined the Container's IP address using the docker inspect command:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' a9404c168b21
which is: 172.17.0.11
when I try to visit the container's IP address and the nginx port in a browser http://172.17.0.11:8888/ we get ERR_CONNECTION_TIMED_OUT
or using curl:
curl 172.17.0.11:8888
curl: (7) Failed to connect to 172.17.0.11 port 8888: Connection refused
To attempt to solve this we googled extensively but suspect we might be asking the "wrong" questions...
You shouldn't be trying to hit the IP address of the container, you should be using the IP address of the host machine.
What you are missing is the mapping of the port of the host machine to the port of the container running the nginx server.
Assuming that you want to use port 8888 on the host machine, you need a parameter such as this to map the ports:
docker run ... -p 8888:8888 ...
Then you should be able to access you server at http://<HOST_MACHINE_IP>:8888
EDIT: There is another gotcha if you are running on a Mac. To use Docker on a Mac it's common to use boot2docker but boot2docker adds in another layer. You need determine the IP address of the boot2docker container and use that instead of localhost to access nginx.
$ boot2docker ip
The VM's Host only interface IP address is: <X.X.X.X>
$ wget http://<X.X.X.X>:8888
...
Connecting to <X.X.X.X>:8888... connected.
HTTP request sent, awaiting response... 200 OK
Reference: https://viget.com/extend/how-to-use-docker-on-os-x-the-missing-guide
EDIT: ... or with docker-machine the equivalent command would be docker-machine ip <machine-name> where <machine-name> is likely to be "default".
You may need to check if your container is running:
docker ps ( you should have an active container)
If no container is active:
docker run -p 80:80 -it /bin/bash
you will then be on your image terminal
start nginx - sudo service nginx start
ctrl p + ctrl q to quit docker without exiting the container
if you are on mac and using boot2docker you cannot use localhost to check your running nginx
so use boot2docker ip
browse using the boot2docker ip

Resources