How to connect to docker container from localhost

10,917

Solution 1

As you can see from the output in your question, your container's ip address is 172.17.0.2. Just point your browser at http://172.17.0.2:4444.

Alternately, you could forward this to a host port, as in:

docker run -p 4444:4444 ...

And then on your host run you could access localhost:4444.

Solution 2

You have to bind the 4444 port to a port of your host. You can do this in the run command with a -p [hostport]:[containerport]

Exemple : docker run -p 8081:80 php:5.6-apache

Share:
10,917
Adi Ohana
Author by

Adi Ohana

Updated on June 06, 2022

Comments

  • Adi Ohana
    Adi Ohana almost 2 years

    I created a docker container which is running 2 java processes - one of the processes is listening on port 4444 and should have access from browser hitting the following URL:

    http://{host}:4444/grid/console

    How do I hit this URL from my browser on the host - connect to the container on port 4444?

    This is the response from docker inspect:

    [
        {
            "Id": "3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde",
            "Created": "2018-02-22T14:54:37.025854258Z",
            "Path": "/bin/bash",
            "Args": [],
            "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 12320,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2018-02-22T14:54:37.487535484Z",
                "FinishedAt": "0001-01-01T00:00:00Z"
            },
            "Image": "sha256:422dc563ca3260ad9ef5c47a1c246f5065d7f177ce51f4dd208efd82967ff182",
            "ResolvConfPath": "/var/lib/docker/containers/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde/resolv.conf",
            "HostnamePath": "/var/lib/docker/containers/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde/hostname",
            "HostsPath": "/var/lib/docker/containers/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde/hosts",
            "LogPath": "/var/lib/docker/containers/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde-json.log",
            "Name": "/hint-env",
            "RestartCount": 0,
            "Driver": "overlay2",
            "Platform": "linux",
            "MountLabel": "",
            "ProcessLabel": "",
            "AppArmorProfile": "",
            "ExecIDs": null,
            "HostConfig": {
                "Binds": null,
                "ContainerIDFile": "",
                "LogConfig": {
                    "Type": "json-file",
                    "Config": {}
                },
                "NetworkMode": "default",
                "PortBindings": {},
                "RestartPolicy": {
                    "Name": "no",
                    "MaximumRetryCount": 0
                },
                "AutoRemove": false,
                "VolumeDriver": "",
                "VolumesFrom": null,
                "CapAdd": null,
                "CapDrop": null,
                "Dns": [],
                "DnsOptions": [],
                "DnsSearch": [],
                "ExtraHosts": null,
                "GroupAdd": null,
                "IpcMode": "shareable",
                "Cgroup": "",
                "Links": null,
                "OomScoreAdj": 0,
                "PidMode": "",
                "Privileged": false,
                "PublishAllPorts": false,
                "ReadonlyRootfs": false,
                "SecurityOpt": null,
                "UTSMode": "",
                "UsernsMode": "",
                "ShmSize": 67108864,
                "Runtime": "runc",
                "ConsoleSize": [
                    0,
                    0
                ],
                "Isolation": "",
                "CpuShares": 0,
                "Memory": 0,
                "NanoCpus": 0,
                "CgroupParent": "",
                "BlkioWeight": 0,
                "BlkioWeightDevice": [],
                "BlkioDeviceReadBps": null,
                "BlkioDeviceWriteBps": null,
                "BlkioDeviceReadIOps": null,
                "BlkioDeviceWriteIOps": null,
                "CpuPeriod": 0,
                "CpuQuota": 0,
                "CpuRealtimePeriod": 0,
                "CpuRealtimeRuntime": 0,
                "CpusetCpus": "",
                "CpusetMems": "",
                "Devices": [],
                "DeviceCgroupRules": null,
                "DiskQuota": 0,
                "KernelMemory": 0,
                "MemoryReservation": 0,
                "MemorySwap": 0,
                "MemorySwappiness": null,
                "OomKillDisable": false,
                "PidsLimit": 0,
                "Ulimits": null,
                "CpuCount": 0,
                "CpuPercent": 0,
                "IOMaximumIOps": 0,
                "IOMaximumBandwidth": 0
            },
            "GraphDriver": {
                "Data": {
                    "LowerDir": "/var/lib/docker/overlay2/aa6ee2cab028cc33b0af19c43a7da3977ef0cef29f3aa02536844955abff3cba-init/diff:/var/lib/docker/overlay2/3eb4156ba1e3e5ad6b066c1cb82c2547a7dee07afb3e0e64a41689f3c02f66fd/diff",
                    "MergedDir": "/var/lib/docker/overlay2/aa6ee2cab028cc33b0af19c43a7da3977ef0cef29f3aa02536844955abff3cba/merged",
                    "UpperDir": "/var/lib/docker/overlay2/aa6ee2cab028cc33b0af19c43a7da3977ef0cef29f3aa02536844955abff3cba/diff",
                    "WorkDir": "/var/lib/docker/overlay2/aa6ee2cab028cc33b0af19c43a7da3977ef0cef29f3aa02536844955abff3cba/work"
                },
                "Name": "overlay2"
            },
            "Mounts": [],
            "Config": {
                "Hostname": "3bef855324d8",
                "Domainname": "",
                "User": "",
                "AttachStdin": true,
                "AttachStdout": true,
                "AttachStderr": true,
                "Tty": true,
                "OpenStdin": true,
                "StdinOnce": true,
                "Env": [
                    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                    "DISTTAG=f27container",
                    "FGC=f27",
                    "FBR=f27"
                ],
                "Cmd": [
                    "/bin/bash"
                ],
                "Image": "422dc563ca32",
                "Volumes": null,
                "WorkingDir": "",
                "Entrypoint": null,
                "OnBuild": null,
                "Labels": {}
            },
            "NetworkSettings": {
                "Bridge": "",
                "SandboxID": "28bb071b33999759e2f523bb7e99e5de0ef23922f32b17cf0135ac8a389a40bc",
                "HairpinMode": false,
                "LinkLocalIPv6Address": "",
                "LinkLocalIPv6PrefixLen": 0,
                "Ports": {},
                "SandboxKey": "/var/run/docker/netns/28bb071b3399",
                "SecondaryIPAddresses": null,
                "SecondaryIPv6Addresses": null,
                "EndpointID": "12a5101b68c18ec32bc0b76ab95bb0960993390f7c420e040b39b1cad45eeb4b",
                "Gateway": "172.17.0.1",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "IPAddress": "172.17.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "MacAddress": "02:42:ac:11:00:02",
                "Networks": {
                    "bridge": {
                        "IPAMConfig": null,
                        "Links": null,
                        "Aliases": null,
                        "NetworkID": "f385270bd9603551c9eca046222ff0b9c7147a761f06c2095343b7960515eafb",
                        "EndpointID": "12a5101b68c18ec32bc0b76ab95bb0960993390f7c420e040b39b1cad45eeb4b",
                        "Gateway": "172.17.0.1",
                        "IPAddress": "172.17.0.2",
                        "IPPrefixLen": 16,
                        "IPv6Gateway": "",
                        "GlobalIPv6Address": "",
                        "GlobalIPv6PrefixLen": 0,
                        "MacAddress": "02:42:ac:11:00:02",
                        "DriverOpts": null
                    }
                }
            }
        }
    ]
    
  • larsks
    larsks about 6 years
    In a default docker environment, you can connect directly to containers without any special setup. The EXPOSE command is largely a no-op (it has uses for things like service discovery, but doesn't influence connectivity).