0

I'm working through a tutorial for React.js, and I decided to work within a Docker container. Here is the code for the tutorial.

The tutorial uses webpack-dev-server running on port 8080 on the container. I've exposed this port in my Dockerfile, and I ran my container with -P to publish the port. Inside the container, if I wget http://localhost:8080, it downloads index.html as expected. However, if I run the equivalent PowerShell command Invoke-WebRequest http://localhost:32769 from my host, I get Invoke-WebRequest : The underlying connection was closed: The connection was closed unexpectedly. Full details/repro steps below.

Dockerfile

FROM node
MAINTAINER Matthew Pirocchi <matthew.pirocchi@gmail.com>
RUN apt-get update && apt-get install -y vim
EXPOSE 8080

Container setup

docker run -itdP --name repro mpiroc/react-fundamentals
docker exec -it repro bash
# Following commands executed within container
mkdir -p /home/mpiroc/repos && cd /home/mpiroc/repos
git clone https://github.com/ReactjsProgram/React-Fundamentals.git
cd React-Fundamentals
git checkout video2
npm install
npm run start # start just runs `webpack-dev-server`

Testing the container

# In a separate terminal
PS C:\Users\matth> docker exec -it repro bash
root@fd6c3102bc51:/# wget http://localhost:8080
... # index.html is downloaded as expected
root@fd6c3102bc51:/# exit
exit
PS C:\Users\matth> docker ps
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS                     NAMES
fd6c3102bc51        mpiroc/react-fundamentals   "node"              28 minutes ago      Up 28 minutes       0.0.0.0:32769->8080/tcp   repro
PS C:\Users\matth> Invoke-WebRequest http://localhost:32769
Invoke-WebRequest : The underlying connection was closed: The connection was closed unexpectedly.
At line:1 char:1
+ Invoke-WebRequest http://localhost:32769
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Why can't I access the published port from my host machine?

Matthew
  • 28,056
  • 26
  • 104
  • 170

2 Answers2

3

I found the answer in this question. I needed to change my npm run start command from:

"start": "webpack-dev-server"

to

"start": "webpack-dev-server --host 0.0.0.0"

This is because in webpack-dev-server 1.8.0 and up, by default webpack-dev-server only listens on localhost, not on the machine's (container in this case) ip.

Community
  • 1
  • 1
Matthew
  • 28,056
  • 26
  • 104
  • 170
0

Looks like you're running on Docker for Windows. If your using the docker-machine install, then do a docker-machine ip default (you might need to use docker-machine ls and use a specific node name instead of default). Then connect to that IP address to reach the Linux VM being run by Docker under the covers instead of localhost. So if you were to get:

docker-machine ip default
192.168.100.1

Invoke-WebRequest http://192.168.100.1:32769
....
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • I don't think I'm using `docker-machine`. `docker-machine ls` displays an empty list. I installed Docker via "Docker for Windows" (https://docs.docker.com/engine/installation/windows/). – Matthew Sep 25 '16 at 13:17
  • As a sanity check, if I run `docker run -d -p 80:80 --name webserver nginx` and point my web browser to `localhost:80`, I see the `nginx` welcome page as expected. – Matthew Sep 25 '16 at 13:30
  • From your sanity test, that would probably be the new 1.12 install with HyperV, avoiding the past ip challenges, so my answer above won't apply. – BMitch Sep 25 '16 at 13:47