How to start multiple containers that use tcp with Docker

1.1k views Asked by At

I'm trying to run a second Meteor app in a Docker's container. The first (who is running as I want) is accessible with localhost:3000and I want to run a second one in a other port like localhost:3003. When I try to access the second I got this

This website is not accessible. Localhost don't allow the connexion. Do a research about localhost 3003 on Google.

And If I look into Docker I can see that my containers are running: enter image description here but I noticed a difference between the port, the one who is accessible is 0.0.0.0:3000->3000/tcpand the one who isn't accessible is 3000/tcp, 0.0.0.0:3003->3003/tcpso I think there is something wrong here.

And in my docker-compose.yml I did :

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

Thank you for the help

[EDIT] there are the logs :

=> 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] With the new config the errors are : enter image description here

But I have the network testNtw:

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

And I did the same docker-compose.yml as in the answer

1

There are 1 answers

10
Farhad Farahi On BEST ANSWER

Try the following:

sudo docker network create <your network name>

Docker Compose file:

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

Then check this url in your browser:

http://localhost:3003

Clarification:

With default networking you need to use links that is a legacy feature. I recommend using User-Defined networks. User-Defined networks provide built in DNS so you can connect to your containers from other containers on the same User-Defined network by name. In your case a container named mongo needs to be accessed from a container named app.