如何从本地主机向我的Docker容器发送HTTP请求? [英] How to send HTTP requests to my docker container from localhost?

查看:166
本文介绍了如何从本地主机向我的Docker容器发送HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法对在VM(Docker工具箱)中运行的Docker容器(与Redis数据库进行通信的Node.js API)发出HTTP请求.

I'm having trouble making HTTP requests to my docker container (it's a Node.js API that communicates with a Redis database), which runs inside a VM (Docker Toolbox).

我已经用所需的端口设置了Dockerfile和docker-compose.yml.构建它们并成功运行(运行").

I've set up my Dockerfile and docker-compose.yml with the desired ports. Built them and ran ("up") them successfully.

FROM node:8.15

WORKDIR /redis_server

COPY package.json package-lock.json ./

RUN npm install

COPY . ./

EXPOSE 8080

CMD ["npm", "start"]

version: '3'
services:
  web:
    build: .
    depends_on:
      - db

  db:
    image: redis
    ports:
      - "6379:6379"

redis.js

const PORT = 6379
const HOST = 'db'

server.js(express.js)

server.js (express.js)

const PORT = '0.0.0.0:8080'

我成功地构建了容器,然后使用HTTP请求服务来测试GET.由于我运行Docker Toolbox,并且该虚拟机位于主机192.168.99.100上,因此我将请求发送到 http://192.168.99.100:8080 .

I build the container succesfully, then use a HTTP request service to test a GET. Since I run Docker Toolbox and that the VM is on host 192.168.99.100, I send my requests to http://192.168.99.100:8080.

这不起作用,在Visual Studio代码中显示的错误消息是连接被拒绝.服务未在服务器上运行,或者vscode中的代理设置不正确,或者防火墙阻止了请求.详细信息:错误:连接ECONNREFUSED 192.168.99.100:8080."

This does not work, the error message that appears in my Visual Studio Code is "Connection is being rejected. The service isn't running on the server, or incorrecte proxy settings in vscode, or a firewall is blocking requests. Details: Error: connect ECONNREFUSED 192.168.99.100:8080."

不确定从这里要去哪里.我认为自己不熟悉物联网.

Not sure where to go from here. I don't consider myself knowledgeable on things network.

推荐答案

这是因为您尚未打开主机上的端口.您可以尝试:

It’s because you have not opened port on host. You may try:

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - db

  db:
    image: redis
    ports:
      - "6379:6379"

这篇关于如何从本地主机向我的Docker容器发送HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆