cannot access to http://localhost:4000 after I map this port number to docker

5

2

This is my first time to use docker. I just followed the steps on docker's webiste: https://docs.docker.com/get-started/part2/#run-the-app After I finished every steps, I cannot access http://localhost:4000 from the browser.(These steps in the link will map 80 to 4000 in the docker) Here is the configuration for my docker:

$ docker ps
CONTAINER ID        IMAGE               COMMAND
STATUS              PORTS                  NAMES
d204920463a9        firendlyhello       "python app.py"
Up 6 minutes        0.0.0.0:4000->80/tcp   pensive_bell

When I run this app:

liyuan.liu@USEUG-98T5N32 MINGW64 ~/test
$ docker run -p 4000:80 firendlyhello
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

Then I was trying to open http://localhost:4000 in my browser, but it just said:This site can’t be reached. This is the app.py content:

liyuan.liu@USEUG-98T5N32 MINGW64 ~/test
$ cat app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr('counter')
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv('NAME', "world"), hostname=socket.gethostname(), visits=visits)


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

beasone

Posted 2017-04-27T18:04:36.127

Reputation: 169

Firstly, can you access it at http://localhost:80? What about http://0.0.0.0:80 and http://0.0.0.0:4000? – Thebluefish – 2017-04-27T18:44:01.933

I tried http://localhost:80, http://0.0.0.0:80 and http://0.0.0.0:4000 and http://127.0.0.1:4000, none of them work. ( superuser website always hides "http://" when I type them in the comment......)

– beasone – 2017-04-27T18:46:20.057

Is it possible that the typo in your command is causing this? The docker image is "friendlyhello" but you typed "firendlyhello" – Myles Keating – 2017-04-27T18:09:10.463

If it is because of the typo, the docker command line still display that it is running. If it is running, http://localhost:4000 should work, correct ?

– beasone – 2017-04-27T18:18:30.450

Answers

2

Docker configures its own default IP address, which you can see whenever you launch Docker Quickstart Terminal.

Instead of http://localhost:4000, type http://[IP address:4000]

You can also use this command to find the IP that Docker has configured:

docker-machine ip default

Richard

Posted 2017-04-27T18:04:36.127

Reputation: 21