Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

node.js - Am I mapping ports wrong on this API call from a Docker container to a locally running API?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

My assumption: there are two docker containers.

  1. .NET Core service
  2. NodeJS app

When you have more than one container things get a lot easy when you create a docker-compose.yml file

The advantage is that it'll create a common network between those two docker containers.

Another point: there are two worlds when you are running containers.

  1. Inside docker (and docker network)
  2. The host machine

the -p <host>:<container> when you are accessing from the host machine. use the host port when you are accessing from the container use the container port.

Summary answer:

  1. create docker compose
  2. change localhost:8000 in nodejs to webapi:8000 (webapi is the name in docker-compose)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...