我正在使用Docker Compose运行2个映像,但无法从Mac命中localhost.我正在暴露端口3000.我是否缺少某些东西? [英] I am running 2 images with Docker Compose and I am having trouble hitting the localhost from my Mac. I am exposing ports 3000. Am I missing something?
问题描述
我正在使用Docker构建Node/Mongo应用程序,当我运行 docker-compose up
时,无法从运行MacOs的主机访问本地主机.使用邮递员或 curl -i localhost:3000
不会返回任何内容.我还尝试检查容器并与该IP连接.我究竟做错了什么?谢谢!
I am building a Node/Mongo app using Docker and I am having trouble hitting my localhost from my host computer running MacOs when I run docker-compose up
. Using postman or curl -i localhost:3000
returns nothing. I have also tried inspecting the container and connecting with that ip. What am I doing wrong? Thanks!
docker-compose.yml:
docker-compose.yml:
version: "2"
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
env_file:
- todoListDocker.env
links:
- mongo
mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=tWwp3Fm4hZUsaLw4
volumes:
- mongo:/data/db
ports:
- "27017:27017"
env_file:
- todoListDocker.env
volumes:
mongo:
Dockerfile:
Dockerfile:
FROM node:boron
MAINTAINER Clinton Medbery <clintomed@gmail.com>
RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "vim"]
RUN mkdir - p /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 3000
CMD ["npm", "start"]
Index.js:
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
var app = express();
var router = require('./services/router');
//Use ENV Variables
console.log("Connecting to Mongo");
mongoose.connect('mongodb://root:tWwp3Fm4hZUsaLw4@mongo:27017');
// mongoose.connect('mongodb://localhost:todoList/todoList');
console.log("Connected to Mongo");
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use('/v1', router);
var PORT = process.env.PORT || 3000;
var HOST = process.env.HOST || '127.0.0.1';
app.get('/hello', function (req, res) {
console.log("Hello World");
res.send({hello:'Hello World!'});
});
console.log('Listening on port ', HOST, PORT);
app.listen(PORT, HOST);
推荐答案
您的快递服务器正在监听 localhost 端口3000.
Your express server is listening on localhost port 3000.
var PORT = process.env.PORT || 3000;
var HOST = process.env.HOST || '127.0.0.1';
这将绑定到容器的本地主机.它独立于Mac的localhost和任何其他容器的localhost.您无法从容器外部到达它.
This will bind to the container's localhost. That is independent from the Mac's localhost, and from any other container's localhost. You cannot reach it from outside the container.
您需要绑定到容器的外部接口,这将使Mac或其他容器连接到端口.您可以为此使用特殊地址 0.0.0.0
.
You need to bind to the external interface of the container, which will let the Mac, or other containers, connect to the port. You can use the special address 0.0.0.0
for this.
var PORT = process.env.PORT || 3000;
var HOST = process.env.HOST || '0.0.0.0';
现在从Mac可以访问Express服务器,端口绑定 3000:3000
将起作用.默认情况下,它将绑定在Mac的所有网络接口上,但是如果您愿意,可以将其限制为Mac的本地主机.
Now that the express server is reachable from the Mac, the port binding 3000:3000
will work. By default, that will be bound on all of the Mac's network interfaces, but you can limit it to the Mac's localhost if you prefer.
ports:
- "127.0.0.1:3000:3000"
这篇关于我正在使用Docker Compose运行2个映像,但无法从Mac命中localhost.我正在暴露端口3000.我是否缺少某些东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!