如何使用Docker

时间:2017-01-05 07:34:11

标签: docker tcp port docker-container

我正在尝试在Docker的容器中运行第二个Meteor应用程序。可以使用localhost:3000访问第一个(我正在运行的人),我希望在localhost:3003等其他端口中运行第二个。当我尝试访问第二个时,我得到了这个

  

无法访问此网站。 Localhost不允许连接。在Google上对localhost 3003进行一项研究。

如果我查看Docker,我可以看到我的容器正在运行: enter image description here 但我发现端口之间存在差异,可访问的端口为0.0.0.0:3000->3000/tcp,无法访问的端口为3000/tcp, 0.0.0.0:3003->3003/tcp,所以我认为此处存在问题。

在我的docker-compose.yml中,我做了:

app:
  image: jeromevi/controlcontainersapp
  ports:
    - "3003:3003"
  environment:
    - ROOT_URL=http://localhost:3003
    - MONGO_URL=mongodb://mongo:27017/meteor
mongo:
  image: mongo:latest

感谢您的帮助

[编辑]有日志:

=> Starting app on port 3000...

/opt/meteor/dist/bundle/programs/server/node_modules/fibers/future.js:313
                        throw(ex);
                        ^
MongoError: failed to connect to server [mongo:27017] on first connect
    at Object.Future.wait (/opt/meteor/dist/bundle/programs/server/node_modules/fibers/future.js:449:15)
    at new MongoConnection (packages/mongo/mongo_driver.js:219:27)
    at new MongoInternals.RemoteCollectionDriver (packages/mongo/remote_collection_driver.js:4:16)
    at Object.<anonymous> (packages/mongo/remote_collection_driver.js:38:10)
    at Object.defaultRemoteCollectionDriver (packages/underscore.js:784:19)
    at new Mongo.Collection (packages/mongo/collection.js:103:40)
    at meteorInstall.both.collections.infosContainers.js (both/collections/infosContainers.js:2:14)
    at fileEvaluate (packages/modules-runtime.js:181:9)
    at require (packages/modules-runtime.js:106:16)
    at /opt/meteor/dist/bundle/programs/server/app/app.js:217:1
    - - - - -
    at [object Object].<anonymous> (/opt/meteor/dist/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/server.js:313:35)
    at emitOne (events.js:77:13)
    at [object Object].emit (events.js:169:7)
    at [object Object].<anonymous> (/opt/meteor/dist/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/pool.js:271:12)
    at [object Object].g (events.js:260:16)
    at emitTwo (events.js:87:13)
    at [object Object].emit (events.js:172:7)
    at Socket.<anonymous> (/opt/meteor/dist/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/connection.js:165:49)
    at Socket.g (events.js:260:16)
    at emitOne (events.js:77:13)

[EDIT2]使用新配置时,错误是: enter image description here

但我有网络测试Ntw:

NETWORK ID          NAME                DRIVER              SCOPE
6767c4bf208f        bridge              bridge              local
c69fbc3a59cf        host                host                local
f6f5083df32e        none                null                local
3d0aacdbb757        testNtw             bridge              local

我做了与答案

相同的docker-compose.yml

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

sudo docker network create <your network name>

Docker撰写文件:

version: '2'
services:
  app:
    image: jeromevi/controlcontainersapp
    ports:
      - "3003:3000"
    environment:
      - ROOT_URL=http://localhost:3000
      - MONGO_URL=mongodb://mongo:27017/meteor
    networks:
      - <your network name>
  mongo:
    image: mongo:latest
    networks:
      - <your network name>
networks:
  <your network name>:
    external: true

然后在浏览器中查看此网址:

http://localhost:3003

<强>澄清

使用默认网络,您需要使用links这是一项传统功能。我建议使用User-Defined个网络。 User-Defined内置了DNS个网络,因此您可以通过User-Defined从同一name网络上的其他容器连接到您的容器。在您的情况下,需要从名为mongo的容器中访问名为app的容器。