How to access Docker container's web server from host
Asked Answered
J

9

71

I'm running under boot2docker 1.3.1.

I have a Docker container running a web server via uwsgi --http :8080.

If I attach to the container I can browse the web site using lynx http://127.0.0.1:8080 so I know the server is working.

I ran my container with:

$ docker run -itP --expose 8080 uwsgi_app:0.2

It has the following details:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
5248ad86596d        uwsgi_app:0.2     "bash"              11 minutes ago      Up 11 minutes       0.0.0.0:49159->8080/tcp   cocky_hypatia
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' 5248ad86596d
172.17.0.107

I thought I could access that web site from my host by going to http://172.17.0.107:49159.

This does not work. I just see 'connecting...' in Chrome, getting nowhere.

What am I doing wrong?

Jebel answered 28/11, 2014 at 15:31 Comment(1)
Are you running Boot2Docker on Mac or Windows PC? I'm going to assume Mac because of the '$'Saiff
J
38

Ok, stupid me, I found the answer in the docs for boot2docker https://docs.docker.com/installation/mac/#container-port-redirection

I needed to use the ip address of the boot2docker vm, rather than the ip of the container, i.e.

$ boot2docker ip
192.168.59.103

and I am able to browse my site from the host at http://192.168.59.103:49159/

I did not need to add any route on the host

Jebel answered 28/11, 2014 at 15:51 Comment(4)
Ah, I didn't spot the -P option.Saiff
In my case I reversed ports with run -p option. -p 80:5000. This would map port 5000 inside our container to port 80 on our local host. I did the opposite. See docs.docker.com/userguide/usingdockerSymbiosis
This solved my problem after 1 day of troubleshootingSubstandard
boot2docker is now decrecated... use docker-machine insteadExhortative
T
98

Extending Anentropic's answer: boot2docker is the old app for Mac and Windows, docker-machine is the new one.

Firstly, list your machines:

$ docker-machine ls

NAME      ACTIVE   DRIVER       STATE     URL                         SWARM
default   *        virtualbox   Running   tcp://192.168.99.100:2376

Then select one of the machines (the default one is called default) and:

$ docker-machine ip default

192.168.99.100
Tillman answered 5/9, 2015 at 23:19 Comment(4)
Solved my Windows issues.Rebane
Is this info still correct in 2016? I get no results from running docker-machine ls, even though docker ps shows things currently running.Pelf
Still works for me on my Mac. No error messages? Try installing again docs.docker.com/docker-for-mac / docs.docker.com/docker-for-windows. Older versions needed to eval some shell config ala docs.docker.com/machine/get-started.Tillman
Use docker inspect $MY_CONTAINER | grep IPAddress. Don't install anything newGaiter
J
38

Ok, stupid me, I found the answer in the docs for boot2docker https://docs.docker.com/installation/mac/#container-port-redirection

I needed to use the ip address of the boot2docker vm, rather than the ip of the container, i.e.

$ boot2docker ip
192.168.59.103

and I am able to browse my site from the host at http://192.168.59.103:49159/

I did not need to add any route on the host

Jebel answered 28/11, 2014 at 15:51 Comment(4)
Ah, I didn't spot the -P option.Saiff
In my case I reversed ports with run -p option. -p 80:5000. This would map port 5000 inside our container to port 80 on our local host. I did the opposite. See docs.docker.com/userguide/usingdockerSymbiosis
This solved my problem after 1 day of troubleshootingSubstandard
boot2docker is now decrecated... use docker-machine insteadExhortative
G
20

To find the IP address of your container, you should need NO additional installs:

docker inspect <container>

This provides a wealth of info. grep it for the IPAddress.

Gaiter answered 10/8, 2016 at 16:6 Comment(3)
The OP already shows that he knows how to find the IPAddress using docker inspect. His question is specifically in regard to why it seems that the connection fails when that IP address is used.Argos
however this is a good answer that matches the question title, an it helped meSheikdom
The IP address this gives is unusable on boot2docker, Docker Machine, Docker Desktop, or basically any environment other than calling from the same native-Linux host.Kenzi
T
7

You could use boot2docker port mapping option -L, as described here.

So, in your case it would be

boot2docker ssh -L 0.0.0.0:8080:localhost:8080

and then

docker run -it -p 8080:8080 uwsgi_app:0.2

That way, you do not have to use boot2docker's IP address: you can use localhost or your own IP address (and your docker container can be accessed from outside).

Tilsit answered 1/12, 2014 at 16:2 Comment(0)
S
5

docker build -t {imagename} .

docker build -t api-rest-test .

docker run -dp {localport}:{exposeport} image:name

 docker run -dp 8080:8080 api-rest-test:latest

make sure you are using the same port for yourlocalport and exposeport

then you can access your rest service in your local machine http://localhost:8080

Slowmoving answered 26/5, 2021 at 14:4 Comment(0)
L
4

Boot2docker is outdated, but you may still have this problem on Docker for Windows or Mac, even though the same container works on Linux. One symptom is that trying to access a page on the server inside the container gives the error "didn't send any data" as opposed to "could not connect."

If so, it may be because on Win/Mac the container host has its own IP, it's not localhost as it is on linux. Try running Django on IP 0.0.0.0, meaning accept connections from all IPs, like this:

python manage.py runserver 0.0.0.0:8000

Alternatively, if you need to make sure the server only responds to local requests (such as from your local proxy like nginx, apache, or gunicorn) you can use the host IP returned by hostname -i.

And make sure you are using the -p port forwarding option correctly in the docker run command.

Assuming all is well, you should be able to access your server at http://localhost in a browser running on the host machine.

Linker answered 10/4, 2017 at 22:17 Comment(0)
S
1

[EDIT: original version was ignoring the -P in question]

If you want to get to the containers without having to 'publish' the port (which changes its number) there is a good run-through here.

The key is this line:

sudo route -n add 172.17.0.0/16 172.16.0.11

which tells the Mac how to route to the private network inside the VirtualBox VM that the Docker containers are on.

Saiff answered 28/11, 2014 at 15:38 Comment(1)
just adding that route does not fix for me. I am wondering what is this IP 172.16.0.11 ? should that be my container's IP? I tried it but that doesn't work eitherJebel
H
1

Had the same issue and in my case i was using AWS EC2 instance. I was trying with the container IP which did not work. Then I used the actual public IP of the AWS host as the IP, which worked.

Hamner answered 6/9, 2016 at 13:21 Comment(1)
OMG. Wasted so much time behind this. Wonder what is the reason for this behaviourAlgebra
G
0

How to troubleshoot the issue on hosting application on local host browser

For this launch the container with below command, in my case it was:

[root@centoslab3 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1b81d8a0e3e1        centos:baseweb      "/bin/bash"         8 minutes ago       Exited (0) 24 seconds ago                       webtest
[root@centoslab3 ~]# docker run --name=atul -v /root/dockertest:/var/www/html -i -t -p 5000:8000 centos:baseweb /bin/bash

In the httpd configuration:

[root@adb28b08c9ed /]# cd /etc/httpd/conf
[root@adb28b08c9ed conf]# ll
total 52
-rw-r--r--. 1 root root 34419 Sep 19 15:16 httpd.conf

edit the file with the port 8000 in listner and update the container ip and port under Servername.

Restart the httpd service and you are done.

Hope this helps

Galley answered 15/12, 2017 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.