Can't connect to Docker containers on OSX

2019-03-13 05:32发布

I'm new to Docker, and I can't seem to connect to any containers.

I installed Docker Toolbox. Now I'm trying to get Shipyard to work. I followed the steps inside of a Docker Quickstart Terminal. The instructions say:

Once deployed, the script will output the URL to connect along with credential information.

The Shipyard installer ended with:

Shipyard available at http://10.0.2.15:8080
Username: [elided] Password: [elided]

However, I went to http://10.0.2.15:8080 on my browser and it didn't connect.

In another Docker Quickstart Terminal, I did a docker ps to see what the container was and to get its IP Address and I got:

$ docker inspect a4755 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.8",
                    "IPAddress": "172.17.0.8",

I'm not sure why the IP was different, but I tried to connect to http://172.17.0.8:8080 and this didn't work either. http://localhost:8080 also failed.

This also happened when I tried to run docker-gunicorn-nginx - everything started, but I couldn't connect to the machine.

What gives?

标签: macos docker
2条回答
仙女界的扛把子
2楼-- · 2019-03-13 06:13

If you read through Docker's Installation on Mac OS X you'll see that on OSX, Docker containers don't run on the host machine itself:

In a Docker installation on Linux, your physical machine is both the localhost and the Docker host. In networking, localhost means your computer. The Docker host is the computer on which the containers run.

On a typical Linux installation, the Docker client, the Docker daemon, and any containers run directly on your localhost. This means you can address ports on a Docker container using standard localhost addressing such as localhost:8000 or 0.0.0.0:8376.

[...]

In an OS X installation, the docker daemon is running inside a Linux VM called default. The default is a lightweight Linux VM made specifically to run the Docker daemon on Mac OS X. The VM runs completely from RAM, is a small ~24MB download, and boots in approximately 5s.

In OS X, the Docker host address is the address of the Linux VM. When you start the VM with docker-machine it is assigned an IP address. When you start a container, the ports on a container map to ports on the VM. To see this in practice, work through the exercises on this page.

Indeed, opening a new Docker Quickstart Terminal, I see:

docker is configured to use the default machine with IP 192.168.99.100

And, opening http://192.168.99.100:8080 takes me to Shipyard. Success!

查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-13 06:28

You can try and execute this command:

docker-machine ip default

it will return some thing like:

192.168.99.100

To get port number:

docker ps

Example output (scroll right to see port mapping):

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
113346425f20        springio/spring1    "sh -c 'java $JAVA_OP"   34 minutes ago      Up 34 minutes       0.0.0.0:8080->8080/tcp   pensive_kirch

To verify if it is working do:

curl 192.168.99.100:8080

查看更多
登录 后发表回答