BugHunterUK
BugHunterUK

Reputation: 8948

Docker OSX Unable to Access Container via IP Address

I'm having some strange issues with Docker on Mac. When I set up a container I am unable to access it via the containers IP address.

I installed Docker, along with Docker Quickstart Terminal. I've created a basic Dockerfile to test:

FROM ubuntu:14.04
MAINTAINER Me <[email protected]>

RUN apt-get update
RUN apt-get install -y nginx
RUN echo "Testing" > /usr/share/nginx/html/index.html

CMD ["nginx", "-g", "daemon off;"]

EXPOSE 80

I build with:

docker build -t james/nginx .

And then run with:

docker run -d james/nginx

I didn't use -p 80:8080 in this instance because at the moment I'm just trying to figure out why I can't access via the containers IP. I can't even ping it.

The container has been giving the title admiring_snyder (Got to love Docker's naming scheme). So I run docker inspect admiring_snyder to get the machines IP address:

[
    {
        "Id": "2d9ac50726ea5d20905c50f2453aa20cc46eebbcac21966d7cdb29a469ad3c7f",
        "Created": "2016-03-14T18:22:24.179971826Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 6837,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2016-03-14T18:22:24.245675122Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:921ad9259637823006e092e9ee6441a410fd1c60f2d599ace0ad8a3200d170c7",
        "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/2d9ac50726ea5d20905c50f2453aa20cc46eebbcac21966d7cdb29a469ad3c7f/resolv.conf",
        "HostnamePath": "/mnt/sda1/var/lib/docker/containers/2d9ac50726ea5d20905c50f2453aa20cc46eebbcac21966d7cdb29a469ad3c7f/hostname",
        "HostsPath": "/mnt/sda1/var/lib/docker/containers/2d9ac50726ea5d20905c50f2453aa20cc46eebbcac21966d7cdb29a469ad3c7f/hosts",
        "LogPath": "/mnt/sda1/var/lib/docker/containers/2d9ac50726ea5d20905c50f2453aa20cc46eebbcac21966d7cdb29a469ad3c7f/2d9ac50726ea5d20905c50f2453aa20cc46eebbcac21966d7cdb29a469ad3c7f-json.log",
        "Name": "/admiring_snyder",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "ShmSize": 67108864,
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "KernelMemory": 0,
            "Memory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null
        },
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "Mounts": [],
        "Config": {
            "Hostname": "2d9ac50726ea",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "james/nginx",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {},
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "aab80a46210e9ad79b53c60294912ed57a77968ac3178d0cf32ad40f2207e236",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/aab80a46210e",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "f5610deaced494b6a6c692a6e79bbe838f04574bb95f86b3c2b688477f724087",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "af5e68119aa41c3fbae80be8760b9382773533ae2c971253970740083ac9e202",
                    "EndpointID": "f5610deaced494b6a6c692a6e79bbe838f04574bb95f86b3c2b688477f724087",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04"
                }
            }
        }
    }
]

Which shows the IP address:

"IPAddress": "172.17.0.4"

I am unable to connect to this IP address. I have a feeling it's something to do with docker being ran on a VM (obviously there's no alternative to this). But, I've no idea how to solve it.

The VM has the IP 192.168.99.100. I got this from docker-machine ip default.

Any advice?

Upvotes: 1

Views: 820

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180937

You can launch the image with a port mapping;

docker run -d -p 8080:80 james/nginx

This will map port 8080 on the docker vm to port 80 in the docker container as can be seen in docker ps under PORTS;

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS
6c8b6d1a7d3d        james/nginx         "nginx -g 'daemon off"   About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp                          focused_spence

In your case, http://192.168.99.100:8080 will show your test page.

Upvotes: 1

Related Questions