I need help to run a database with docker and nodejs. I do not understand where I'm going wrong, but I can not make connection between my container with database and my container with node. This is the db link in docker: "https://hub.docker.com/_/rethinkdb/".Then follows:
my Dockerfile
FROM node:latest
ENV HOME=/src/jv-agricultor
RUN mkdir -p $HOME/
WORKDIR $HOME/
ADD package* $HOME/
RUN npm install
EXPOSE 80
ADD . $HOME/
CMD ["node", "node_modules/.bin/nodemon", "-L", "bin/www"]
My docker-compose.yml
version: "3"
volumes:
rethindb-data:
external: true
services:
db:
image: rethinkdb:latest
ports:
- "8080:8080"
- "29015:29015"
- "28015:28015"
api:
image: hello-nodemon
environment:
- NODE_ENV=development
- PORT=80
- DB_HOST=localhost
- DB_PORT=28015
deploy:
# replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "3000:80"
volumes:
- .:/src/jv-agricultor
- /src/jv-agricultor/node_modules
depends_on:
- db
networks:
- webnet
networks:
webnet:
i run: docker stack deploy -c docker-compose.yml webservice
My docker service
ID NAME MODE REPLICAS IMAGE PORTS
yez42a7w8khs webservice_api replicated 1/1 hello-nodemon:latest *:3000->80/tcp
n8idu78cp18m webservice_db replicated 1/1 rethinkdb:latest *:8080->8080/tcp,*:28015->28015/tcp,*:29015->29015/tcp
My docker service api (here is node/express)
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
p20qdagcspjc webservice_api.1 hello-nodemon:latest abner Running Running 28 minutes ago
My Docker service db
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
3046xuo4l8ix webservice_db.1 rethinkdb:latest abner Running Running 30 minutes ago
My logs internal db
webservice_db.1.3046xuo4l8ix#abner | Recursively removing directory /data/rethinkdb_data/tmp
webservice_db.1.3046xuo4l8ix#abner | Initializing directory /data/rethinkdb_data
webservice_db.1.3046xuo4l8ix#abner | Running rethinkdb 2.3.6~0jessie (GCC 4.9.2)...
webservice_db.1.3046xuo4l8ix#abner | Running on Linux 4.15.0-24-generic x86_64
webservice_db.1.3046xuo4l8ix#abner | Loading data from directory /data/rethinkdb_data
webservice_db.1.3046xuo4l8ix#abner | Listening for intracluster connections on port 29015
webservice_db.1.3046xuo4l8ix#abner | Listening for client driver connections on port 28015
webservice_db.1.3046xuo4l8ix#abner | Listening for administrative HTTP connections on port 8080
webservice_db.1.3046xuo4l8ix#abner | Listening on cluster addresses: 127.0.0.1, 172.18.0.3, 10.0.5.182, 10.0.5.183, 10.255.11.212, 10.255.11.213
webservice_db.1.3046xuo4l8ix#abner | Listening on driver addresses: 127.0.0.1, 172.18.0.3, 10.0.5.182, 10.0.5.183, 10.255.11.212, 10.255.11.213
webservice_db.1.3046xuo4l8ix#abner | Listening on http addresses: 127.0.0.1, 172.18.0.3, 10.0.5.182, 10.0.5.183, 10.255.11.212, 10.255.11.213
webservice_db.1.3046xuo4l8ix#abner | Server ready, "069fd360acfb_jot" c1cf5173-cf0d-457f-9c8f-4ba1756c28d8
my app.js
...
var connect = require('./lib/connect');
console.log('DB_HOST: ' + process.env.DB_HOST);
console.log('DB_PORT: ' + process.env.DB_PORT);
console.log('PORT: ' + process.env.PORT);
console.log('NODE_ENV: ' + process.env.NODE_ENV);
...
My connect middleware
'use strict'
// import r from 'rethinkdb';
var r = require('rethinkdb');
module.exports._connect = (function _connect(req, res, next) {
r.connect( {host: process.env.DB_HOST, port: process.env.DB_PORT}, (err, conn) => {
console.log(err);
})
})();
My service docker api logs respose
webservice_api.1.p20qdagcspjc#abner | [nodemon] restarting due to changes...
webservice_api.1.p20qdagcspjc#abner | [nodemon] starting `node bin/www`
webservice_api.1.p20qdagcspjc#abner | DB_HOST: localhost
webservice_api.1.p20qdagcspjc#abner | DB_PORT: 28015
webservice_api.1.p20qdagcspjc#abner | PORT: 80
webservice_api.1.p20qdagcspjc#abner | NODE_ENV: development
webservice_api.1.p20qdagcspjc#abner | { ReqlDriverError: Could not connect to localhost:28015.
webservice_api.1.p20qdagcspjc#abner | connect ECONNREFUSED 127.0.0.1:28015
webservice_api.1.p20qdagcspjc#abner | at ReqlDriverError.ReqlError [as constructor] (/src/jv-agricultor/node_modules/rethinkdb/errors.js:23:13)
webservice_api.1.p20qdagcspjc#abner | at new ReqlDriverError (/src/jv-agricultor/node_modules/rethinkdb/errors.js:68:50)
webservice_api.1.p20qdagcspjc#abner | at TcpConnection.<anonymous> (/src/jv-agricultor/node_modules/rethinkdb/net.js:94:27)
webservice_api.1.p20qdagcspjc#abner | at Object.onceWrapper (events.js:273:13)
webservice_api.1.p20qdagcspjc#abner | at TcpConnection.emit (events.js:182:13)
webservice_api.1.p20qdagcspjc#abner | at Socket.<anonymous> (/src/jv-agricultor/node_modules/rethinkdb/net.js:705:22)
webservice_api.1.p20qdagcspjc#abner | at Socket.emit (events.js:187:15)
webservice_api.1.p20qdagcspjc#abner | at emitErrorNT (internal/streams/destroy.js:82:8)
webservice_api.1.p20qdagcspjc#abner | at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
webservice_api.1.p20qdagcspjc#abner | at process._tickCallback (internal/process/next_tick.js:63:19)
webservice_api.1.p20qdagcspjc#abner | From previous event:
webservice_api.1.p20qdagcspjc#abner | at Function.<anonymous> (/src/jv-agricultor/node_modules/rethinkdb/net.js:945:10)
webservice_api.1.p20qdagcspjc#abner | at Function.connect (/src/jv-agricultor/node_modules/rethinkdb/util.js:43:16)
webservice_api.1.p20qdagcspjc#abner | at _connect (/src/jv-agricultor/lib/connect.js:9:7)
webservice_api.1.p20qdagcspjc#abner | at Object.<anonymous> (/src/jv-agricultor/lib/connect.js:19:3)
webservice_api.1.p20qdagcspjc#abner | at Module._compile (internal/modules/cjs/loader.js:689:30)
webservice_api.1.p20qdagcspjc#abner | at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
webservice_api.1.p20qdagcspjc#abner | at Module.load (internal/modules/cjs/loader.js:599:32)
webservice_api.1.p20qdagcspjc#abner | at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
webservice_api.1.p20qdagcspjc#abner | at Function.Module._load (internal/modules/cjs/loader.js:530:3)
webservice_api.1.p20qdagcspjc#abner | at Module.require (internal/modules/cjs/loader.js:637:17)
webservice_api.1.p20qdagcspjc#abner | at require (internal/modules/cjs/helpers.js:20:18)
webservice_api.1.p20qdagcspjc#abner | at Object.<anonymous> (/src/jv-agricultor/app.js:14:15)
webservice_api.1.p20qdagcspjc#abner | at Module._compile (internal/modules/cjs/loader.js:689:30)
webservice_api.1.p20qdagcspjc#abner | at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
webservice_api.1.p20qdagcspjc#abner | at Module.load (internal/modules/cjs/loader.js:599:32)
webservice_api.1.p20qdagcspjc#abner | at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
webservice_api.1.p20qdagcspjc#abner | at Function.Module._load (internal/modules/cjs/loader.js:530:3)
webservice_api.1.p20qdagcspjc#abner | at Module.require (internal/modules/cjs/loader.js:637:17)
webservice_api.1.p20qdagcspjc#abner | at require (internal/modules/cjs/helpers.js:20:18)
webservice_api.1.p20qdagcspjc#abner | name: 'ReqlDriverError',
webservice_api.1.p20qdagcspjc#abner | msg:
webservice_api.1.p20qdagcspjc#abner | 'Could not connect to localhost:28015.\nconnect ECONNREFUSED 127.0.0.1:28015',
webservice_api.1.p20qdagcspjc#abner | frames: undefined,
webservice_api.1.p20qdagcspjc#abner | message:
webservice_api.1.p20qdagcspjc#abner | 'Could not connect to localhost:28015.\nconnect ECONNREFUSED 127.0.0.1:28015' }
docker-compose does inter-service communication by service name, so the value of DB_HOST should be db.
On a side note, unless you need to expose the database outside of the stack, you do not need the port mapping.
#Alex Karshin It is unnecessary to fully specify the container name. The first example in the docker-compose networking docs shows how simple it really is.
spawnia has a point, but might not have the right answer. If you take a look at docker ps -a the name of your database container is webservice_db. Therefore, you will not be successful if you try to connect to rethinkdb on localhost (cuz obviously it's not on localhost).
You must either hardcode the container name (webservice_db) to your config file, or do set it in docker-compose.yml. But if you do, I suggest you set the container names explicitly:
version: "3"
...
services:
db:
container_name: webservice_db
...
api:
container_name: webservice_api
environment:
- NODE_ENV=development
- PORT=80
- DB_HOST= webservice_db
- DB_PORT=28015
...
There, now it should work normally.
Related
i'm getting this error while connecting node to mongodb:
techworld-js-docker-demo-app-mongo-express-1 | Could not connect to database using connectionString: mongodb://admin:password#mongodb:27017/"
techworld-js-docker-demo-app-mongo-express-1 | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [Error: connect ECONNREFUSED 172.19.0.3:27017
techworld-js-docker-demo-app-mongo-express-1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
techworld-js-docker-demo-app-mongo-express-1 | name: 'MongoNetworkError'
techworld-js-docker-demo-app-mongo-express-1 | }]
techworld-js-docker-demo-app-mongo-express-1 | at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
techworld-js-docker-demo-app-mongo-express-1 | at Pool.emit (events.js:314:20)
techworld-js-docker-demo-app-mongo-express-1 | at /node_modules/mongodb/lib/core/connection/pool.js:564:14
techworld-js-docker-demo-app-mongo-express-1 | at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
techworld-js-docker-demo-app-mongo-express-1 | at /node_modules/mongodb/lib/core/connection/connect.js:32:7
techworld-js-docker-demo-app-mongo-express-1 | at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
techworld-js-docker-demo-app-mongo-express-1 | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
techworld-js-docker-demo-app-mongo-express-1 | at Object.onceWrapper (events.js:421:26)
techworld-js-docker-demo-app-mongo-express-1 | at Socket.emit (events.js:314:20)
techworld-js-docker-demo-app-mongo-express-1 | at emitErrorNT (internal/streams/destroy.js:92:8)
techworld-js-docker-demo-app-mongo-express-1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
techworld-js-docker-demo-app-mongo-express-1 | at processTicksAndRejections (internal/process/task_queues.js:84:21)
techworld-js-docker-demo-app-mongo-express-1 | (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
Docker-compose.yaml file :
version: '3'
services:
my-app:
image: nihalchandra/myapp:4.0
ports:
- 3000:3000
mongodb:
image: mongo
restart: always
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- mongo-data:/data/db
mongo-express:
image: mongo-express
restart: always # fixes MongoNetworkError when mongodb is not ready when mongo-express starts
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
mongo-data:
driver: local
done this in server.js file:
let mongoUrlLocal = "mongodb://admin:password#localhost:27017";
let mongoUrlDocker = "mongodb://admin:password#mongodb";
i tried changing the server but it still didnt work
You should add into your my-app definition a link to database:
my-app:
image: nihalchandra/myapp:4.0
ports:
- 3000:3000
links:
- mongodb:mongodb
Then your application will be able to connect to MongoDB using mongodb as a hostname.
More details about this you can find in documentation: https://docs.docker.com/compose/networking/
There are multiple services that I had been trying to run (redis, front-end, back-end and elastic-search) and I was not able to connect to the elastic search service. I even tried giving a static ip for the service. (The networking part is currently commented out in the docker file attached). I tried changing the images and it still was not working.
When I tested ES locally using curl localhost:9200/_cat/health as I have mapped the container port locally it gives me that the cluster is green. I could connect to the other services like redis without issues. As with redis, I am using the service name, elasticsearch to connect it to the back-end service. Following is my docker-compose.yml file.
version: '3'
services:
arc-external:
image: arc-external
build:
context: ./arc-development-branch/arc-external
ports:
- '4201:4201'
# networks:
# - vpcbr
redis:
image: redis:3.2.11-alpine
ports:
- '6379:6379'
# networks:
# - vpcbr
elasticsearch:
image: elasticsearch:2
ports:
- '9200:9200'
- '9300:9300'
environment:
- node.name=elasticsearch
- cluster.name=datasearch
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- cluster.initial_master_nodes=elasticsearch
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./data/elastic:/usr/share/elasticsearch/data
# networks:
# vpcbr:
# ipv4_address: 10.5.0.4
api-external:
image: api-external
build: .
ports:
- '3001:3001'
depends_on:
- redis
- elasticsearch
# networks:
# - vpcbr
# networks:
# vpcbr:
# driver: bridge
# ipam:
# config:
# - subnet: 10.5.0.0/16
# gateway: 10.5.0.1
This is the exact error that I am getting when running docker compose-up
api-external_1 | 2021-03-09 20:41:46.3253 - info: Finished setting up log directories
api-external_1 | 2021-03-09 20:41:46.3514 - info: Connection successful to mongodb # mongodb://10.0.0.44:27017/arc
api-external_1 | 2021-03-09 20:41:46.3764 - info: Connection successful to redis at: host: redis port: 6379
api-external_1 | Elasticsearch ERROR: 2021-03-09T20:41:46Z
api-external_1 | Error: Request error, retrying
api-external_1 | HEAD http://elasticsearch:9200/ => connect ECONNREFUSED 172.24.0.4:9200
api-external_1 | at Log.error (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/log.js:226:56)
api-external_1 | at checkRespForFailure (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/transport.js:259:18)
api-external_1 | at HttpConnector.<anonymous> (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/connectors/http.js:164:7)
api-external_1 | at ClientRequest.wrapper (/usr/src/app/api-external/node_modules/lodash/lodash.js:4935:19)
api-external_1 | at ClientRequest.emit (events.js:198:13)
api-external_1 | at ClientRequest.EventEmitter.emit (domain.js:448:20)
api-external_1 | at Socket.socketErrorListener (_http_client.js:401:9)
api-external_1 | at Socket.emit (events.js:198:13)
api-external_1 | at Socket.EventEmitter.emit (domain.js:448:20)
api-external_1 | at emitErrorNT (internal/streams/destroy.js:91:8)
api-external_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
api-external_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
api-external_1 |
api-external_1 | Elasticsearch WARNING: 2021-03-09T20:41:46Z
api-external_1 | Unable to revive connection: http://elasticsearch:9200/
api-external_1 |
api-external_1 | Elasticsearch WARNING: 2021-03-09T20:41:46Z
api-external_1 | No living connections
api-external_1 |
api-external_1 | 2021-03-09 20:41:46.3844 - error: Error: Failed to connect to elasticsearch # elasticsearch:9200
api-external_1 | at exports.esClient.ping (/usr/src/app/api-external/dist/setup/elastic-search.js:33:46)
api-external_1 | at respond (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/transport.js:327:9)
api-external_1 | at sendReqWithConnection (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/transport.js:226:7)
api-external_1 | at next (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/connection_pool.js:214:7)
api-external_1 | at process._tickCallback (internal/process/next_tick.js:61:11)
api-external_1 | 2021-03-09 20:41:46.3854 - error: Error: No Living connections
api-external_1 | at sendReqWithConnection (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/transport.js:226:15)
api-external_1 | at next (/usr/src/app/api-external/node_modules/elasticsearch/src/lib/connection_pool.js:214:7)
api-external_1 | at process._tickCallback (internal/process/next_tick.js:61:11)
api-external_1 | npm ERR! code ELIFEC
Frankly, I searched a lot and were not able to debug it. Any help would be appreciated.
I was able to figure out the answer. The thing stating as depends_on does not wait the services to completely up. Here, api-external does get start up as soon as the redis and elasticsearch starts. However, elasticsearch need a bit time to configure everything so restarting the service will do the trick.
More permanent solution is to write a script that would wait until the elasticsearch is up completely before starting the api-external service
I have been trying to build the classical voting app. But my redis and mongodb are giving same error. I have to connect my redis to my node app and i have tried the following:
//redis
const redisoption = {
host: "redis",
port: 6379
};
.
//redis
const redisoption = {
host: "0.0.0.0",
port: 6379
};
docker-compose.yml
version: "3"
services:
vote:
image: adityagaddhyan/voting-app
ports:
- "3000:3000"
depends_on:
- redis
networks:
- front-end
- back-end
volumes:
- ./vote:/app
result:
image: adityagaddhyan/voting-result
ports:
- "3011:3011"
depends_on:
- mongodb
networks:
- back-end
- front-end
volumes:
- ./result:/app
worker:
image: adityagaddhyan/service-worker
depends_on:
- redis
- mongodb
networks:
- back-end
volumes:
- ./worker:/app
redis:
image : redis
command: ["redis-server", "--bind", "0.0.0.0", "--port", "6379"
ports:
- "6379:6379"
networks:
- back-end
volumes:
- ./redis:/app
mongodb:
image: mongo
ports:
- "27017:27017"
volumes:
- ./db:/app
volumes:
db-data:
networks:
front-end:
back-end:
I have tried all sort of combination.
I am majorily getting two kind of error.
if i use host: "0.0.0.0", i get
Error: Redis connection to 0.0.0.0:6379 failed - connect ECONNREFUSED 0.0.0.0:6379
vote_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
vote_1 | Emitted 'error' event on RedisClient instance at:
vote_1 | at RedisClient.on_error (/usr/src/app/node_modules/redis/index.js:341:14)
vote_1 | at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:222:14)
vote_1 | at Socket.emit (events.js:315:20)
vote_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
vote_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
vote_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21) {
vote_1 | errno: 'ECONNREFUSED',
vote_1 | code: 'ECONNREFUSED',
vote_1 | syscall: 'connect',
vote_1 | address: '0.0.0.0',
vote_1 | port: 6379
vote_1 | }
and if i use host:redis, i get:
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
worker_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
worker_1 | Emitted 'error' event on RedisClient instance at:
worker_1 | at RedisClient.on_error (/usr/src/app2/node_modules/redis/index.js:341:14)
worker_1 | at Socket.<anonymous> (/usr/src/app2/node_modules/redis/index.js:222:14)
worker_1 | at Socket.emit (events.js:315:20)
worker_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
worker_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
worker_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21) {
worker_1 | errno: 'ECONNREFUSED',
worker_1 | code: 'ECONNREFUSED',
worker_1 | syscall: 'connect',
worker_1 | address: '127.0.0.1',
worker_1 | port: 6379
worker_1 | }
vote_1 | /usr/src/app/app.js:28
vote_1 | host: back-end,
vote_1 | ^
I am new to docker and unable to solve this problem. please help.
Here is my Dockerfile for React.js with the error I got in terminal:
FROM node:8
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./package.json /usr/src/app
RUN npm install
RUN npm build
EXPOSE 3000
CMD ["npm", "run", "start"]
Error:-
react_1 |
react_1 | > ecom-panther#0.1.0 start /usr/src/app
react_1 | > react-scripts start
react_1 |
react_1 | ℹ 「wds」: Project is running at http://172.18.0.2/
react_1 | ℹ 「wds」: webpack output is served from
react_1 | ℹ 「wds」: Content not from webpack is served from /usr/src/app/public
react_1 | ℹ 「wds」: 404s will fallback to /
react_1 | Starting the development server...
react_1 |
ecom-panther_react_1 exited with code 0
For Node and Express, I got this:
express_1 | (node:30) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
express_1 | server is running on port: 5000
express_1 | (node:30) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
express_1 | at Pool.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
express_1 | at emitOne (events.js:116:13)
express_1 | at Pool.emit (events.js:211:7)
express_1 | at createConnection (/usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:561:14)
express_1 | at connect (/usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:994:11)
express_1 | at makeConnection (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:31:7)
express_1 | at callback (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:264:5)
express_1 | at Socket.err (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:294:7)
express_1 | at Object.onceWrapper (events.js:315:30)
express_1 | at emitOne (events.js:116:13)
express_1 | at Socket.emit (events.js:211:7)
express_1 | at emitErrorNT (internal/streams/destroy.js:73:8)
express_1 | at _combinedTickCallback (internal/process/next_tick.js:139:11)
express_1 | at process._tickCallback (internal/process/next_tick.js:181:9)
express_1 | (node:30) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
express_1 | (node:30) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Docker file for backend:-
FROM node:8
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
COPY . /usr/src/app
EXPOSE 5000
CMD ["npm","start"]
Here is my docker-compose.yml file
version: '3' # specify docker-compose version
# Define the service/container to be run
services:
react: #name of first service
build: client #specify the directory of docker file
ports:
- "3000:3000" #specify port mapping
express: #name of second service
build: server #specify the directory of docker file
ports:
- "5000:5000" #specify port mapping
links:
- database #link this service to the database service
database: #name of third service
image: mongo #specify image to build contasiner flow
ports:
- "27017:27017" #specify port mapping
How I can run frontend at browser and is there any easy approach to do this in a better way ?
Error 1:
Add stdin_open: true to your react service, like:
...
services:
react: #name of first service
build: client #specify the directory of docker file
stdin_open: true
ports:
- "3000:3000" #specify port mapping
...
You might need to rebuild or clean cached so "docker-compose up --build" or "docker-compose build --no-cache" then "docker-compose up"
Error 2:
In your database connections line in your index.js file or whatever you named should have :
mongodb://database:27017/
where "database" is your named MongoDB service. You can use your container IP address too with docker inspect <container> and use the IP the see there too. Ideally you want to have a ENV in your Dockerfile or docker-compose.yml:
ENV MONGO_URL mongodb://database:27017/
The Dockerfile for my application is as follows
# Tells the Docker which base image to start.
FROM node
# Adds files from the host file system into the Docker container.
ADD . /app
# Sets the current working directory for subsequent instructions
WORKDIR /app
RUN npm install
RUN npm install -g bower
RUN bower install --allow-root
RUN npm install -g nodemon
#expose a port to allow external access
EXPOSE 9000 9030 35729
# Start mean application
CMD ["nodemon", "server.js"]
The docker-compose.yml file is as follows
web:
build: .
links:
- db
ports:
- "9000:9000"
- "9030:9030"
- "35729:35729"
db:
image: mongo:latest
ports:
- "27017:27017"
And the error generated while running is as follows:-
web_1 | [nodemon] 1.11.0
web_1 | [nodemon] to restart at any time, enter `rs`
web_1 | [nodemon] watching: *.*
web_1 | [nodemon] starting `node server.js`
web_1 | Server running at http://127.0.0.1:9000
web_1 | Server running at https://127.0.0.1:9030
web_1 |
web_1 | /app/node_modules/mongodb/lib/server.js:261
web_1 | process.nextTick(function() { throw err; })
web_1 | ^
web_1 | MongoError: failed to connect to server [localhost:27017] on first connect
web_1 | at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:313:35)
web_1 | at emitOne (events.js:96:13)
web_1 | at Pool.emit (events.js:188:7)
web_1 | at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:271:12)
web_1 | at Connection.g (events.js:291:16)
web_1 | at emitTwo (events.js:106:13)
web_1 | at Connection.emit (events.js:191:7)
web_1 | at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:165:49)
web_1 | at Socket.g (events.js:291:16)
web_1 | at emitOne (events.js:96:13)
web_1 | at Socket.emit (events.js:188:7)
web_1 | at emitErrorNT (net.js:1281:8)
web_1 | at _combinedTickCallback (internal/process/next_tick.js:74:11)
web_1 | at process._tickCallback (internal/process/next_tick.js:98:9)
web_1 | [nodemon] app crashed - waiting for file changes before starting...
I have uploaded the image for my application at DockerHub as crissi/airlineInsurance.
In docker you can't connect to an other container via localhost because each container is independend and has its own IP. You should use container_name:port. In your example it should be db:27017 to connect from your NodeJS application in 'web' to the MongoDB in 'db'.
So it's not the problem of your Dockerfile. It's the connection URL from your NodeJS application that points to localhost instead of db.