5

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...

2 Answers 2

9

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".

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for reply & for clarifying that! just tried: docker run -it -p 8888:8888 learn-docker bash but still cannot access page when visiting: 127.0.0.1:8888 or localhost:8888 what am I doing wrong?
Hmm, I tried it out and I have the same problem. But nginx is running. I connected to the container at the commandline ( docker exec -it <CONTAINER> bash) then installed wget (apt-get install wget) and was able to get a response from nginx from within the container (wget http://localhost:8888) so it looks like it's actually a networking problem of some sort. Not sure of any more details at this stage though ...
Are you running on a Mac? The problem could be with boot2docker. See the edit to my answer.
Yes, Host OS is Mac. But I'm using Docker Toolkit to run docker as Boot2Docker is deprecated.
I think it's the same scenario, i.e. there's another VM that you need to find the IP address of with docker-machine ip <machine name>. Probably your machine name is default.
|
0

You may need to check if your container is running:

  • docker ps ( you should have an active container)

If no container is active:

  1. docker run -p 80:80 -it /bin/bash
  2. you will then be on your image terminal
  3. start nginx - sudo service nginx start
  4. ctrl p + ctrl q to quit docker without exiting the container
  5. if you are on mac and using boot2docker you cannot use localhost to check your running nginx
  6. so use boot2docker ip
  7. browse using the boot2docker ip

1 Comment

Is this still relevant with latest Docker for OS X?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.