Cannot access docker container from the host

I’m testing the native Mac OSX docker (Version 1.11.1-beta10 (build: 6662)) locally.

I have a simple docker image that is a simple HTTP server with port mapping to 6127. It used to work using the docker-machine.

I’m not able to connect to this machine, here a list of things that I tried:

  • telnet to that port using both localhost and the ip provided by network inspection
  • curl to test the http request for both endpoints
  • adding a route to that subnet and repeating the tests above

If I start a new container and execute a interactive bash I’m able to execute the request.

What should I be missing?

Here is docker ps output:

$ docker ps
    CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS              PORTS                     NAMES
    6abe7986aa50        pocketmath/nickel-infinite-budget:1.3   "java -jar /app/nicke"   44 minutes ago      Up 25 minutes       0.0.0.0:32768->6127/tcp   nickel-infinite-budget

Network inspect output:

$ docker network inspect testsuite_default
    [
        {
            "Name": "testsuite_default",
            "Id": "f1a4dbfda87dc34409b50ba6177066770397f71b4ca6c9496efca42af6874167",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": null,
                "Config": [
                    {
                        "Subnet": "172.18.0.0/16",
                        "Gateway": "172.18.0.1/16"
                    }
                ]
            },
            "Internal": false,
            "Containers": {
                "6abe7986aa50222c8bda3a03c5821f233f74c6f6bc27cb13714aca265deab946": {
                    "Name": "nickel-infinite-budget",
                    "EndpointID": "d38c18180409ea577bae2e2fc68866d260202a431ef9090cfc272dfdd6deb140",
                    "MacAddress": "02:42:ac:12:00:02",
                    "IPv4Address": "172.18.0.2/16",
                    "IPv6Address": ""
                }
            },
            "Options": {},
            "Labels": {}
        }
    ]

Pinata diagnose output:

$sudo pinata diagnose -u
OS X: version 10.11.4 (build: 15E65)
Docker.app: version v1.11.1-beta10
Running diagnostic tests:
[OK]      docker-cli
[OK]      Moby booted
[OK]      driver.amd64-linux
[OK]      vmnetd
[OK]      osxfs
[OK]      db
[OK]      slirp
[OK]      menubar
[OK]      environment
[OK]      Docker
[OK]      VT-x
Docker logs are being collected into /tmp/20160510-173201.tar.gz
Most specific failure is: No error was detected
Your unique id is: A583A23F-7FA3-492B-A208-7616BB7B38FC
Please quote this in all correspondence.

Without the docker run command it is hard to be sure. I think you didn’t specify the port mapping, ie your docker run is missing -p 6127:6127 hence docker chooses a random arbitrary port on the host side to map to 6127 on the container side.

This I deduce from the output of the docker ps command you provided :

Try connecting to http://localhost:32768 and you should see your HTTP server.

Actually it was a docker-compose file:

nickel-infinite-budget:
    image: nickel-infinite-budget:1.3
    container_name: nickel-infinite-budget
    hostname: nickel-infinite-budget
    command: -initialOrderIds "0,123,333,888,1001,1002,1003,9999,49753,71984"
    ports:
      - "6127"

I changed to:

nickel-infinite-budget:
    image: nickel-infinite-budget:1.3
    container_name: nickel-infinite-budget
    hostname: nickel-infinite-budget
    command: -initialOrderIds "0,123,333,888,1001,1002,1003,9999,49753,71984"
    ports:
      - "6127:6127"

And it worked! :slight_smile:

I don’t know why docker would map a random port. :slight_smile:

Thanks!

1 Like

You need to change it to :

nickel-infinite-budget:
    image: nickel-infinite-budget:1.3
    container_name: nickel-infinite-budget
    hostname: nickel-infinite-budget
    command: -initialOrderIds "0,123,333,888,1001,1002,1003,9999,49753,71984"
    ports:
      - ["6127:6127"]
```
to force it to bind on 6127 on localhost 

The current definition only says that the container exposes port 6127 but doesn't give any instruction on which port to bind it to locally.

-- edit
you figured it out as I was answering. 

Mapping to a random port lets you start as many instances of the same service as you want. The service is happy and can listen on priviledged ports in the container (port 80 or 443 for instance) while the docker host actually maps it to a non priviledged port.
1 Like