Reputation: 286
I can't seem to access any of my running docker containers. The most recent hello-world3 is a node app that uses port 8080. I have the node app listening on that port via process.env.PORT. I set the PORT=8080
with the npm start script and I EXPOSE 8080 with the docker file. After I build the container, I specify a port. in this case 8082 by docker run -p 8082:8080 hello-world3
looking at this img from my console I should be able to see my apps response by going to localhost:8082 yeah?
my docker file
FROM jkilbride/node-npm-alpine:8
WORKDIR /src
COPY package.json .
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm","start"]
package.json:
{
"name": "service",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start":"set PORT=8080 && node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
index.js:
const http = require('http');
const server = http.createServer((req,res) => {
const data = {
'data': 'Hello World',
'hostname': require('os').hostname()
};
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(data));
});
server.listen(process.env.PORT, (err) => {
if (err)
return console.log(err);
console.log('API is running on ' + process.env.PORT);
})
Upvotes: 1
Views: 3956
Reputation: 7722
In my case the issue was with the source-code of the container's content: In a server-class I configured a Rest API server to run on port 8080
on 127.0.0.1
. Like explained in @anujprashar's answer 127.0.0.1
is the VM's localhost
. This might not automatically bind to the (windows) machine's localhost
.
After I changed the sourcecode to run the server from 0.0.0.0
instead of 127.0.0.1
I was able to use http://localhost:8080/...
from my windows machine.
(#DockerDesktop, #Windows11)
Upvotes: 1
Reputation: 6335
On windows docker run in vm. So when you do localhost:8082
you are calling localhost of host machine i.e. your windows and not docker vm that is running on different ip. One way you can make localhost calls forward to vm running docker is using port forwarding
.
I hope you have installed virtualbox
. Follow below steps:
Open virtual box -> on left sidebar select vm running docker (which is named default) -> click on settings button on upper bar -> select network -> select advanced -> select port forwarding.
And here you for your case you can enter rule to forward host port 8082 request to 8080 guest port. And now when you do localhost:8082 it will forward request to 8080 of docker vm, which will forward to container 8080.
Upvotes: 2
Reputation: 994
This seems to be caused by a file sharing issue. When Docker is installed the file sharing service seems to not work. Docker then does not have access to the local volumes and file sharing.
Here is an article which details how to get it working. I would setup a very basic node app and then use these steps to reproduce.
Dockerizing a node application
I was able to get my basic node app working using the below link.
How to solve windows blocking sharing between host and docker containers
Upvotes: 0
Reputation: 2346
To access the site on the Windows Docker host, you need to make the request using the container's IP address - which is the virtual IP address only visible to the host machine (and the port the container exposes if it's different to the published port, which is where the host is listening for external requests):
Reference - https://blog.sixeyed.com/published-ports-on-windows-containers-dont-do-loopback/
Upvotes: 0