Unable to access Docker container from Mac Osx host

Hi There,

I’m trying to hit my node app from the localhost. I’ve set the webpack server to listen on 0.0.0.0 but when I try to hit the URL I get nothing, curl does not work either. What else do I need to configure?

Expected behavior

Able to browse node app running on port 8082 from the browser using the ip of the container

Actual behavior

not found error when accessing from browser

Information

OS: 10.10.5 Yosmite, 10.10.5 | Docker: Version 1.12.0-beta21 (build: 11019)

[
{
“Id”: “ec5a86a805277352244e63c2f4f0a44b23ca7e904fab0626b35782002daad50a”,
“Created”: “2016-08-04T12:02:17.251458893Z”,
“Path”: “bash”,
“Args”: ,
“State”: {
“Status”: “exited”,
“Running”: true,
“Paused”: false,
“Restarting”: false,
“OOMKilled”: false,
“Dead”: false,
“Pid”: 0,
“ExitCode”: 0,
“Error”: “”,
“StartedAt”: “2016-08-04T12:02:17.450240084Z”,
“FinishedAt”: “2016-08-04T12:19:49.959934855Z”
},
“Image”: “sha256:0d90898532210246893d448740ee6ad83ad351f71dc188d4a5705d193cb3a580”,
“ResolvConfPath”: “/var/lib/docker/containers/ec5a86a805277352244e63c2f4f0a44b23ca7e904fab0626b35782002daad50a/resolv.conf”,
“HostnamePath”: “/var/lib/docker/containers/ec5a86a805277352244e63c2f4f0a44b23ca7e904fab0626b35782002daad50a/hostname”,
“HostsPath”: “/var/lib/docker/containers/ec5a86a805277352244e63c2f4f0a44b23ca7e904fab0626b35782002daad50a/hosts”,
“LogPath”: “/var/lib/docker/containers/ec5a86a805277352244e63c2f4f0a44b23ca7e904fab0626b35782002daad50a/ec5a86a805277352244e63c2f4f0a44b23ca7e904fab0626b35782002daad50a-json.log”,
“Name”: “/test”,
“RestartCount”: 0,
“Driver”: “aufs”,
“MountLabel”: “”,
“ProcessLabel”: “”,
“AppArmorProfile”: “”,
“ExecIDs”: null,
“HostConfig”: {
“Binds”: [
“/Users/xx/.ssh:/root/.ssh”,
“/Users/xxx/projects/”
],
“ContainerIDFile”: “”,
“LogConfig”: {
“Type”: “json-file”,
“Config”: {}
},
“NetworkMode”: “default”,
“PortBindings”: {
“8082/tcp”: [
{
“HostIp”: “”,
“HostPort”: “8080”
}
]
},
“RestartPolicy”: {
“Name”: “no”,
“MaximumRetryCount”: 0
},
“AutoRemove”: false,
“VolumeDriver”: “”,
“VolumesFrom”: null,
“CapAdd”: null,
“CapDrop”: null,
“Dns”: ,
“DnsOptions”: ,
“DnsSearch”: ,
“ExtraHosts”: null,
“GroupAdd”: null,
“IpcMode”: “”,
“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,
“CgroupParent”: “”,
“BlkioWeight”: 0,
“BlkioWeightDevice”: null,
“BlkioDeviceReadBps”: null,
“BlkioDeviceWriteBps”: null,
“BlkioDeviceReadIOps”: null,
“BlkioDeviceWriteIOps”: null,
“CpuPeriod”: 0,
“CpuQuota”: 0,
“CpusetCpus”: “”,
“CpusetMems”: “”,
“Devices”: ,
“DiskQuota”: 0,
“KernelMemory”: 0,
“MemoryReservation”: 0,
“MemorySwap”: 0,
“MemorySwappiness”: -1,
“OomKillDisable”: false,
“PidsLimit”: 0,
“Ulimits”: null,
“CpuCount”: 0,
“CpuPercent”: 0,
“IOMaximumIOps”: 0,
“IOMaximumBandwidth”: 0
},
“GraphDriver”: {
“Name”: “aufs”,
“Data”: null
},
“Mounts”: [
{
“Source”: “/Users/xxx/projects/”,
“Destination”: “/code”,
“Mode”: “”,
“RW”: true,
“Propagation”: “rprivate”
},
{
“Source”: “/Users/xxx/.ssh”,
“Destination”: “/root/.ssh”,
“Mode”: “”,
“RW”: true,
“Propagation”: “rprivate”
}
],
“Config”: {
“Hostname”: “ec5a86a8052”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: true,
“AttachStdout”: true,
“AttachStderr”: true,
“ExposedPorts”: {
“8082/tcp”: {}
},
“Tty”: true,
“OpenStdin”: true,
“StdinOnce”: true,
“Env”: [
“no_proxy=*.local, 169.254/16”,
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“NPM_CONFIG_LOGLEVEL=info”,
“NODE_VERSION=6.3.1”
],
“Cmd”: [
“bash”
],
“Image”: “node:6”,
“Volumes”: null,
“WorkingDir”: “”,
“Entrypoint”: null,
“OnBuild”: null,
“Labels”: {}
},
“NetworkSettings”: {
“Bridge”: “”,
“SandboxID”: “335eda2be2e1b7bf3f044aa703f0bb7634528ff7e33fc8efc6e0b1a09568123e”,
“HairpinMode”: false,
“LinkLocalIPv6Address”: “”,
“LinkLocalIPv6PrefixLen”: 0,
“Ports”: null,
“SandboxKey”: “/var/run/docker/netns/335eda2be2e1”,
“SecondaryIPAddresses”: null,
“SecondaryIPv6Addresses”: null,
“EndpointID”: “”,
“Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“IPAddress”: “”,
“IPPrefixLen”: 0,
“IPv6Gateway”: “”,
“MacAddress”: “”,
“Networks”: {
“bridge”: {
“IPAMConfig”: null,
“Links”: null,
“Aliases”: null,
“NetworkID”: “db93a5c17dfacc773902b4e8bd41b3e25939a00697cdc1c3ad7bb6a9b0addff4”,
“EndpointID”: “”,
“Gateway”: “”,
“IPAddress”: “”,
“IPPrefixLen”: 0,
“IPv6Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“MacAddress”: “”
}
}
}
}

Steps to reproduce the behavior

docker run -ti -v ~/.ssh:/root/.ssh -v $(pwd):/code -p 8080:8082 --name test node:6 bash

When running

docker run -ti -v ~/.ssh:/root/.ssh -v $(pwd):/code -p 8080:8082 --name test  node:6 bash

no server is started and so no server is reachable. Do you have another reproduction?

hi David,

I ran the machine and this command: docker network inspect

{
“Name”: “bridge”,
“Id”: “32b8f5272479bafd63769958624903d75e48b1e3447fdd4bef6b65885156fafe”,
“Scope”: “local”,
“Driver”: “bridge”,
“EnableIPv6”: false,
“IPAM”: {
“Driver”: “default”,
“Options”: null,
“Config”: [
{
“Subnet”: “172.17.0.0/16”,
“Gateway”: “172.17.0.1”
}
]
},
“Internal”: false,
“Containers”: {
“a4f70a9527317d4daafc4ffbf382393d026947455a008c077428ed0241dfce57”: {
“Name”: “test”,
“EndpointID”: “e6d638d632b012a5157f4ba780254ecbec9c2b8b33de5f30e7466d8835eecdb2”,
“MacAddress”: “02:42:ac:11:00:02”,
“IPv4Address”: “172.17.0.2/16”,
“IPv6Address”: “”
}
},
“Options”: {
“com.docker.network.bridge.default_bridge”: “true”,
“com.docker.network.bridge.enable_icc”: “true”,
“com.docker.network.bridge.enable_ip_masquerade”: “true”,
“com.docker.network.bridge.host_binding_ipv4”: “0.0.0.0”,
“com.docker.network.bridge.name”: “docker0”,
“com.docker.network.driver.mtu”: “1500”
},
“Labels”: {}
}

docker run -ti -v ~/.ssh:/root/.ssh -v $(pwd):/code -p 8080:8082 --name test node:6 bash

Running this would have dropped you in bash shell inside the container. Can you check if the node app is running using ps?

hi @ganesshkumar, my node app seems to be running fine, I changed the port number etc. Do I need to enable another setting or is this yet another Docker issue?

70% 1/1 build moduleshttp://localhost:8080/
webpack result is served from /0.0.0.0:8085/
content is served from ./src/public
404s will fallback to /index.html
chunk {0} app.bundle.js (app) 1.42 MB [rendered]
Child html-webpack-plugin for “index.html”:
chunk {0} index.html 35

!— docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
88e471aea3cf node:6 “bash” 5 minutes ago Up 5 minutes 0.0.0.0:8085->8085/tcp test

@dsheets, Is there anything else I can try for this one?

Yes, please supply an actual reproduction that starts something similar to your application and has the same issue.

@dsheets, I posted this already, it’s just before @ganesshkumar’s comment. Or am I missing what you actually mean by that?

Yes, your command does not start a server inside the container and there is no information about how/what to run. We need a single command that, alone, starts a server that has the problem.

ah ok. I’m running a web app using webpack, node etc. I use npm server to run the application.

The command I run is npm start

I’ve tried to change the host to 0.0.0.0, I have changed the port as well.

config.output = isTest ? {} : {
path: root(‘dist’),
publicPath: isProd ? ‘/’ : ‘http://localhost:8080/’,
filename: isProd ? ‘js/[name].[hash].js’ : ‘js/[name].js’,
chunkFilename: isProd ? ‘[id].[hash].chunk.js’ : ‘[id].chunk.js’
};

Is that what you’re after? Or do you need more?

It’s a start. I need a sequence of steps to run to reproduce the issue. I don’t use npm or webpack. A Dockerfile or docker-compose.yml or both as well as any needed metadata like package.json or perhaps a git repository is necessary. I cannot guess what you are running. I need you to write down precisely what you are running (or something similar that has the same bad behavior).

can I post the project here? A zip file or something?

I don’t think the forum software supports attaching files for most users. A pastebin, public server, GitHub repository, BitBucket repository, etc would be best. Make sure that you are OK posting the full project if you do and that it does not contain sensitive information (e.g. cryptographic secret keys).

ok.

  1. docker run -ti -v ~/.ssh:/root/.ssh -v $(pwd)/angular2-webpack.:/code -p 8080:8082 --name test node:6 bash

  2. git clone https://github.com/preboot/angular2-webpack.git

  3. npm i

  4. npm run start

  5. from the host hit localhost:8082 and you should see an Angular page

That should do it.

I appreciate your help.

@dsheets any luck with running it?

@dsheets, I’m out of Ideas, where you able to run the install?

There are three issues here:

  • A webpack-dev-server feature that only exposes the server to the loopback interface by default which is fine if you are running natively but is not supported by Docker for Mac. You need to provide --host 0.0.0.0 to the server invocation in the package.json file. I used this Dockerfile:
FROM node:6

RUN git clone https://github.com/preboot/angular2-webpack.git

WORKDIR angular2-webpack

RUN sed -i.bak 's/--port 8080/--host 0.0.0.0 --port 8080/' package.json
RUN npm i

CMD [ "npm", "run", "start" ]
  • The ordering of ports in your docker run invocation is backwards – you need to use -p 8082:8080 which is outside:inside rather than inside:outside.

  • There is an bug in webpack-dev-server or some other component of that repository that you are using which results in its not using relative URLs so if you map ports it does not know about it, generates absolute URLs with ports included, and then fails to load resources. To workaround this, you can either use -p 8080:8080 or change the webpack-dev-server line in the package.json file (the same one where I added --host 0.0.0.0 to have the port match the outside port and then use something like --port 8082:8082.

Hi @dsheets, thanks so much for putting that together, much appreciated.

I tried to run the docker file but it fails.It seems not clone the repo at all, it just moves into the next task, when it can’t find a package.json, it fails. Do I need to add something else to the file?

!-- npm log

0 info it worked if it ends with ok
1 verbose cli [ ‘/usr/local/bin/node’, ‘/usr/local/bin/npm’, ‘run’, ‘start’ ]
2 info using npm@3.10.3
3 info using node@v6.3.1
4 verbose stack Error: ENOENT: no such file or directory, open '/Users/xxxx/Sites/docker-test/package.json’
4 verbose stack at Error (native)
5 verbose cwd /Users/xxxx/Sites/docker-test
6 error Darwin 14.5.0
7 error argv “/usr/local/bin/node” “/usr/local/bin/npm” “run” "start"
8 error node v6.3.1
9 error npm v3.10.3
10 error path /Users/xxxx/Sites/docker-test/package.json
11 error code ENOENT
12 error errno -2
13 error syscall open
14 error enoent ENOENT: no such file or directory, open '/Users/xxxx/Sites/docker-test/package.json’
15 error enoent ENOENT: no such file or directory, open '/Users/xxxx/Sites/docker-test/package.json’
15 error enoent This is most likely not a problem with npm itself
15 error enoent and is related to npm not being able to find a file.
16 verbose exit [ -2, true ]