<p>May be you are using bridge network mode. In that case, you need to bind your host port to container port to use same port. 
So when you execute docker run, you may need to add 
-p 5000:5000 as a flag to docker run command. Also I don't see any expose in Dockerfile. So you may want to add EXPOSE 5000 in Dockerfile just above cmd. </p>

CC BY-SA 4.0
2020-01-17T14:13:06.077Z
https://docs.docker.com/engine/reference/builder/#expose provides some further information on -p/-P flags and EXPOSE
Lykanion
CC BY-SA 4.0
2019-02-06T04:55:10.057Z
2019-02-06T04:55:10.057Z
deosha
<p>I spent hours before found that:</p>
<p>Wrong:</p>
<pre><code>docker run alex/dockeronepagesite:4 --name dockeronepagesite -it --publish 8080:8080 --rm
</code></pre>
<p>Correct:</p>
<pre><code>docker run --name dockeronepagesite -it --publish 8080:8080 --rm alex/dockeronepagesite:4
</code></pre>
<p>Name of image should be last, after all options.</p>

CC BY-SA 4.0
2024-05-12T15:41:23.947Z
2024-05-12T19:22:58.200Z
2024-05-12T19:22:58.200Z
Alexander Kutin
<h3>Containers have to expose ports</h3>
<p>A container will not listen to outside traffic unless the Dockerfile has an <code>EXPOSE 5000</code> command. This tells the container to listen for external traffic on that port.</p>
<p>That being said, the Docker engine will only listen to external ports that you tell it to. By default that is none. Setting <code>EXPOSE 5000</code> means that the container will listen to external traffic that the docker engine routes to it. By default this means only internal docker engine traffic will be able to access that port.</p>
<h3>Expose the port to the host</h3>
<p>Depending on how you are starting the container (either cli or docker-compose), you will need to expose the port to the host. From CLI, this will be done with the <code>-p</code> flag in the format <code>-p hostPort:containerPort</code>. You can then map the port as you wish, like <code>-p 50000:5000</code> with 5000 being the same port in your <code>EXPOSE</code> command in the Docker file and 50000 being the <code>localhost</code> port you want to use.</p>
<h3>Putting it together</h3>
<p>Update your docker file to something like this:</p>
<pre><code># Specify a base image
FROM node:alpine
...

EXPOSE 5000

# Default command
CMD [&quot;npm&quot;, &quot;start&quot;]
</code></pre>
<p>Then run it with:</p>
<pre class="lang-sh prettyprint-override"><code>docker run --rm imageName -p 5000:5000
</code></pre>
<p>From there you should be able to <code>curl http://localhost:5000</code> and get the expected response.</p>

CC BY-SA 4.0
2021-01-12T16:36:41.950Z
2021-01-12T16:36:41.950Z
Rhuarc13
<p>I'm new to Docker and following a tutorial on Udemy.com. The turtorial has me setting and running a very simple container.</p>

<p><strong>Docker File</strong></p>

<pre><code># Specify a base image
FROM node:alpine

# Putting files into a specific directory
WORKDIR /usr/app

# Install some dependencies
COPY ./package.json ./
RUN npm install
COPY ./ ./

# Default command
CMD ["npm", "start"]
</code></pre>

<p><strong>Index.js</strong></p>

<pre><code>const express = require('express');

const app = express();

app.get('/', (req, res) =&gt; {
    res.send('Hi there');
});

app.listen(5000, () =&gt; {
    console.log('Listening on port 5000');
});
</code></pre>

<p><strong>package.json</strong></p>

<pre><code>{
    "dependencies": {
        "express": "*"
    },
    "scripts": {
        "start": "node index.js"
    }
}
</code></pre>

<p>I'm working on a laptop running Windows 10 Home. Using the Docker Quickstart Terminal and PowerShell.</p>

<p>I've edited my HOSTS file as to point localhost to 192.168.99.100, which is the ip the container gets.</p>

<p>I've tried hitting the server by both 192.168.99.100:5000 and localhost:5000. No matter what, when I try to access via browser(I.E. and Chrome) I get the following message:</p>

<p>This site can’t be reached</p>

<p>192.168.99.100 refused to connect.</p>

<hr>

<blockquote>
  <p>curl <a href="http://192.168.99.100:5000" rel="nofollow noreferrer">http://192.168.99.100:5000</a></p>
</blockquote>

<p>returns</p>

<blockquote>
  <p>curl: (7) Failed to connect to 192.168.99.100 port 5000: Connection refused</p>
</blockquote>

<hr>

<p>However, If I run <code>'docker-machine ls'</code>, I get:</p>

<blockquote>
  <p>tcp://192.168.99.100:2376</p>
</blockquote>

<p>Why port 2376? I cannot figure out where/how this port is being assigned.</p>

<p>Trying the browser on that port I get a different message:</p>

<p>This page isn’t working</p>

<p>192.168.99.100 sent an invalid response.</p>

<p>ERR_INVALID_HTTP_RESPONSE</p>

<hr>

<p>This would suggest(at least to me) that the server is there on that port, but not doing something right.</p>

<pre><code>curl http://192.168.99.100:2376 --output -
</code></pre>

<p>Gives me some garbled output in the terminal, and writing it to a file gives garbled output as well. Not much, just a few garbage characters.</p>

<p>I suspect this is the "Hi There" message that is not being formatted correctly.</p>

<p>So my questions would be, am I doing something wrong in my dockerfile? Why/how is the container getting this port 2376 when i'm specifically mapping to 5000?</p>

<p>Any insight would be appreciated.</p>

CC BY-SA 4.0
2019-03-26T13:38:27.767Z
You need to expose the ports either by adding them in your dockerfile or add arguments while starting container. Look at deosha's answer.
KatariaA
<docker><dockerfile>
Troubles with mapping ports on a very simple Docker container
CB Midkiff
# Troubles with mapping ports on a very simple Docker container

(Asked by: username_0 on 2019-02-05)

I'm new to Docker and following a tutorial on Udemy.com. The turtorial has me setting and running a very simple container.
Docker File
# Specify a base image
FROM node:alpine

# Putting files into a specific directory
WORKDIR /usr/app

# Install some dependencies
COPY ./package.json ./
RUN npm install
COPY ./ ./

# Default command
CMD ["npm", "start"]

Index.js
const express = require('express');

const app = express();

app.get('/', (req, res) => {
    res.send('Hi there');
});

app.listen(5000, () => {
    console.log('Listening on port 5000');
});

package.json
{
    "dependencies": {
        "express": "*"
    },
    "scripts": {
        "start": "node index.js"
    }
}

I'm working on a laptop running Windows 10 Home. Using the Docker Quickstart Terminal and PowerShell.
I've edited my HOSTS file as to point localhost to 192.168.99.100, which is the ip the container gets.
I've tried hitting the server by both 192.168.99.100:5000 and localhost:5000. No matter what, when I try to access via browser(I.E. and Chrome) I get the following message:
This site can’t be reached
192.168.99.100 refused to connect.


curl http://192.168.99.100:5000

returns

curl: (7) Failed to connect to 192.168.99.100 port 5000: Connection refused


However, If I run 'docker-machine ls', I get:

tcp://192.168.99.100:2376

Why port 2376? I cannot figure out where/how this port is being assigned.
Trying the browser on that port I get a different message:
This page isn’t working
192.168.99.100 sent an invalid response.
ERR_INVALID_HTTP_RESPONSE

This would suggest(at least to me) that the server is there on that port, but not doing something right.
curl http://192.168.99.100:2376 --output -

Gives me some garbled output in the terminal, and writing it to a file gives garbled output as well. Not much, just a few garbage characters.
I suspect this is the "Hi There" message that is not being formatted correctly.
So my questions would be, am I doing something wrong in my dockerfile? Why/how is the container getting this port 2376 when i'm specifically mapping to 5000?
Any insight would be appreciated.

**Tags:** docker, dockerfile

> **Comments:**
> **username_1**: You need to expose the ports either by adding them in your dockerfile or add arguments while starting container. Look at username_2's answer.  

---

## 3 answers

### Answer by username_2 on 2019-02-06. Score: 1

May be you are using bridge network mode. In that case, you need to bind your host port to container port to use same port.
So when you execute docker run, you may need to add
-p 5000:5000 as a flag to docker run command. Also I don't see any expose in Dockerfile. So you may want to add EXPOSE 5000 in Dockerfile just above cmd.

> **Comments:**
> **username_3**: https://docs.docker.com/engine/reference/builder/#expose provides some further information on -p/-P flags and EXPOSE  

---

### Answer by username_4 on 2024-05-12. Score: 0

I spent hours before found that:
Wrong:
docker run alex/dockeronepagesite:4 --name dockeronepagesite -it --publish 8080:8080 --rm

Correct:
docker run --name dockeronepagesite -it --publish 8080:8080 --rm alex/dockeronepagesite:4

Name of image should be last, after all options.


---

### Answer by username_5 on 2021-01-12. Score: 0

Containers have to expose ports
A container will not listen to outside traffic unless the Dockerfile has an EXPOSE 5000 command. This tells the container to listen for external traffic on that port.
That being said, the Docker engine will only listen to external ports that you tell it to. By default that is none. Setting EXPOSE 5000 means that the container will listen to external traffic that the docker engine routes to it. By default this means only internal docker engine traffic will be able to access that port.
Expose the port to the host
Depending on how you are starting the container (either cli or docker-compose), you will need to expose the port to the host. From CLI, this will be done with the -p flag in the format -p hostPort:containerPort. You can then map the port as you wish, like -p 50000:5000 with 5000 being the same port in your EXPOSE command in the Docker file and 50000 being the localhost port you want to use.
Putting it together
Update your docker file to something like this:
# Specify a base image
FROM node:alpine
...

EXPOSE 5000

# Default command
CMD ["npm", "start"]

Then run it with:
docker run --rm imageName -p 5000:5000

From there you should be able to curl http://localhost:5000 and get the expected response.


---

