docker: Says connection refused when attempting to

2019-08-14 05:41发布

问题:

I'm a newbie at docker. I'm creating a Hello, World example. All I'm trying to do is bring up Apache in a docker and then view the default website from the host machine.

Dockerfile

FROM centos:latest

RUN yum install epel-release -y

RUN yum install wget -y

RUN yum install httpd -y

EXPOSE 80

ENTRYPOINT ["/usr/sbin/httpd", "-D", "FOREGROUND"]

And then I build it: > docker build .

And then I tag it:

docker tag 17283f566320 my:apache

And then I run it:

> docker run -p 80:9191 my:apache
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message

It then runs....

In another terminal window, I attempt to issue the curl command to view the default web site.

> curl -XGET http://0.0.0.0:9191
curl: (7) Failed to connect to 0.0.0.0 port 9191: Connection refused

> curl -XGET http://localhost:9191
curl: (7) Failed to connect to localhost port 9191: Connection refused

> curl -XGET http://127.0.0.1:9191
curl: (7) Failed to connect to 127.0.0.1 port 9191: Connection refused

or I try localhost

Just to make sure that I got the port correct, I run this:

> docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                          NAMES
5aed4063b1f6        my:apachep      "/usr/sbin/httpd -D F"   43 seconds ago      Up 42 seconds       80/tcp, 0.0.0.0:80->9191/tcp   angry_hodgkin

回答1:

Despite you created the containers in your local machine. These are actually running on a different machine (a virtual machine)

First, check what is the IP of your docker machine (the virtual machine)

$docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM
default   *        virtualbox   Running   tcp://192.168.99.100  

Then run curl command to view the default web site on your apache web server inside the container

curl http://192.168.99.100:9191


回答2:

If you are running docker on Ubuntu machine as native you should be able to access your container with localhost. If you are using Mac or Windows your docker container runs not on local host but on its IP. you can get your container ip with command docker inspect <container id> | grep IPAddress or if your are using docker-machine docker-machine ip <docker_machine_name> Related info:

  • http://networkstatic.net/10-examples-of-how-to-get-docker-container-ip-address/
  • https://docs.docker.com/machine/reference/ip/
  • How to get a Docker container's IP address from the host?

so your curl call should be something like this curl <container_ip>:<container_exposed_port>

also you can tag your image on build command with param -t like this: docker build -t my:image .

Another tip you can optimize your dockerfile by combining yum install commands like this:

RUN yum install -y \ 
    epel-release \
    wget \
    httpd

http://blog.tutum.co/2014/10/22/how-to-optimize-your-dockerfile/



回答3:

Thanks to all. My ports were reversed:

> docker run -p 9191:80 my:apache