title: Docker Cannot Connect Localhost But Can Connect via Vscode
body: <p>I'm new with Docker and learning Docker images while creating a custom node.js image for my dev env. (Windows 10 WSL2 Docker latest)</p>
  <p><code>TL;DR</code>
  I created a custom image as below and created a container from this custom image. When I try a <code>gatsby start</code> command (or an express server start), it's supposed to connect to <code>http://localhost:8000/</code> (tried <code>127.0.0.1</code>, ip address, <code>host.docker.internal</code>) however it doesn't. When I attach the container to VSCode, somehow VSCode creates a new auto-forwarded port which I can connect with. I attached the details below.</p>
  <p>So, does anyone know why I can't connect to port <code>8000</code> on my localhost? What am I missing?</p>
  <p>The custom image</p>
  <pre><code>FROM debian:latest
  ENV HOME /root
  
  RUN rm /bin/sh &amp;&amp; ln -s /bin/bash /bin/sh
  
  # Upgrade
  RUN apt update &amp;&amp; apt -y upgrade
  
  RUN apt install -y apt-utils software-properties-common python3 git curl
  
  RUN mkdir -p /root/.ssh \
  &amp;&amp; echo &quot;Host *&quot; &gt;&gt; /root/.ssh/config \
  &amp;&amp; echo &quot;    StrictHostKeyChecking no&quot; &gt;&gt; /root/.ssh/config \
  &amp;&amp; echo &quot;    GlobalKnownHostsFile  /dev/null&quot; &gt;&gt; /root/.ssh/config \
  &amp;&amp; echo &quot;    UserKnownHostsFile    /dev/null&quot; &gt;&gt; /root/.ssh/config
  
  # Upgrade
  RUN apt update &amp;&amp; apt -y upgrade
  
  RUN apt install -y python3-pip
  
  # Set py alias
  RUN echo 'alias python=python3 &amp;&amp; alias py=python3' &gt;&gt; ~/.bashrc
  
  # Install nvm to install npm and node.js
  ENV NVM_DIR /home/.nvm
  ENV NODE_VERSION 16.3.0
  RUN mkdir /home/.nvm &amp;&amp; \
      curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash &amp;&amp; \
      chmod +x /home/.nvm/nvm.sh &amp;&amp; \
      . /home/.nvm/nvm.sh &amp;&amp; \
      nvm install --latest-npm &quot;$NODE_VERSION&quot; &amp;&amp; \
      nvm alias default &quot;$NODE_VERSION&quot; &amp;&amp; \
      nvm use default &amp;&amp; \
      DEFAULT_NODE_VERSION=$(nvm version default) &amp;&amp; \
      ln -sf /home/.nvm/versions/node/$DEFAULT_NODE_VERSION/bin/node /usr/bin/nodejs &amp;&amp; \
      ln -sf /home/.nvm/versions/node/$DEFAULT_NODE_VERSION/bin/node /usr/bin/node &amp;&amp; \
      ln -sf /home/.nvm/versions/node/$DEFAULT_NODE_VERSION/bin/npm /usr/bin/npm
  
  RUN npm install -g gatsby-cli
  
  WORKDIR /app
  
  VOLUME [&quot;/app&quot;]
  
  EXPOSE 3000
  EXPOSE 3001
  EXPOSE 8000
  EXPOSE 8001
  EXPOSE 8002
  EXPOSE 8003
  EXPOSE 8004
  EXPOSE 8005
  EXPOSE 8006
  EXPOSE 8007
  EXPOSE 8008
  EXPOSE 8009
  EXPOSE 8010
  </code></pre>
  <p>Then I started the container with command</p>
  <pre><code>docker run --name mycontainer -v ${pwd}:/app --publish 8000:8000 -it nodejs-dev-img /bin/bash
  </code></pre>
  <p>The image successfully runs and I can connect.</p>
  <p><code>http://localhost:8000/</code> doesn't connect.</p>
  <p>When I attach VSCode to the container and run, VSCode creates a auto forwarded port, which I can use to connect as below:</p>
  <p><a href="https://i.stack.imgur.com/WFCxH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WFCxH.png" alt="a auto forwarded port" /></a></p>
  <p>I'm wondering what I'm doing wrong.</p>
  <p>Thank you,</p>