Some more context: I have a simple dotnetcore API endpoint (simple GET endpoint that, for now, just responds with a HTTP 200 statuscode). I want to call that endpoint from a NodeJs script running inside a Docker container. Then endpoint when running from Visual Studio is:
https://localhost:44350/api/commands
The NodeJs script:
const axios = require('axios');
axios.get('https://localhost:8080/api/commands')
.then(response => {
console.log(response.status)
console.log(response.data);
})
.catch(error => {
console.log(error);
});
The Dockerfile:
FROM node:14
WORKDIR /usr/src/app
RUN npm install
RUN npm install axios
COPY . .
EXPOSE 8080
CMD [ "node", "script.js" ]
I build the image as such:
docker build -t <mydockerid>/jsscriptexecution . --no-cache
And run as such:
docker run -p 44350:8080 -it --rm <mydockerid>/jsscriptexecution
When I execute that line, I get:
docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:44350: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
time="2021-01-27T13:30:13+01:00" level=error msg="error waiting for container: context canceled"
I assume I'm doing something wrong mapping the ports, or maybe the concept of "localhost" is not recognized in Docker. I replaced localhost by my machine's IP, but that also doesn't work. I am very new to Docker, but I can't imagine being the first dealing with this kind of issue..?
As a sidenote, if I replace the url in the axios.get call by a publicly available API like 'https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', I get a result back without any issues. So it's not so much that I can not call any exterior API, but more (I think) that the "localhost" part of it is not recognized..?
question from:
https://stackoverflow.com/questions/65919296/am-i-mapping-ports-wrong-on-this-api-call-from-a-docker-container-to-a-locally-r 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…