docker container cannot connect to localhost mongodb [duplicate] - node.js

This question already has answers here:
Connect to host mongodb from docker container
(4 answers)
Closed 4 years ago.
A. I have a container that includes the following
1. NodeJS version 8.11.4
2. Rocketchat meteor app
B. This is my Dockerfile
FROM node:8.11.4
ADD . /app
RUN npm install -g node-gyp
RUN set -x \
&& cd /app/programs/server/ \
&& npm install \
&& npm cache clear --force
WORKDIR /app/
ENV PORT=3000 \
ROOT_URL=http://localhost:3000
EXPOSE 3000
CMD ["node", "main.js"]
C. This command is executed well
docker build -t memo:1.0 .
When I try to run the container, it encounters the following error in containers log
{"log":"MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]\n","stream":"stderr","time":"2019-01-24T21:56:42.222722362Z"}
So container can not be executed.
The mongodb is running and I've added 0.0.0.0 to bindIp in the mongod.conf file.
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
My mongodb is installed in host(outside the container)
The problem was not resolved and my container status is Exited
I put the IP instead of the localhost,but it encounters the following error
{"log":"MongoNetworkError: failed to connect to server [192.168.0.198:27017] on first connect [MongoNetworkError: connect EHOSTUNREACH

The problem here is that you're starting a docker container (a self contained environment) and then trying to reach localhost:27017. However, localhost inside your container is not the same localhost as outside your container (on your host). There are two approaches you could take from this point:
Instead of attempting to connect to localhost:27017, connect to your host's ip (something like 192.x.x.x or 10.x.x.x)
(Better option imo) dockerize your mongodb, then your services will be able to communicate with each other using docker dns. To do this, you would create a docker-compose.yml with one service being your app and the other being mongodb.

When you try with MONGO_URL=mongodb://192.168.0.198:27017/rocketchat add that IP to bindIp as well.
With localhost MONGO_URL=mongodb://127.0.0.1:27017/rocketchat
I also recommend enabling security authorization in mongo config. Then set a user and password for your database.
Keep in mind that any change to config file requires a mongo restart

Related

Error: connect ECONNREFUSED 127.0.0.1:5432 docker-compose up

Not sure why am I getting the SequelizeConnectionRefusedError. I verified that I am able to run all my docker images locally but when I try to run 'docker-compose up' command, I am running into Error: connect ECONNREFUSED 127.0.0.1:5432.
Based on my understanding of your question, here are my assumptions:
You are using MacOS
Your Postgres server is running in the host OS instead of in another docker container.
With that being said, this is a common problem with MacOS users who want to connect their docker containers to the Postgres server running in the host machine. As they are not in the same network, there is no way for your container to reach the Postgres server and hence, connecting to it via 127.0.0.1:5432 will definitely not reachable.
It will be trivial to solve in a Linux machine by adding network_mode: host so that the containers will be running in the same network as host machine hence is able to reach the Postgres server. However, due to the implementation of Docker on Mac where Docker host is actually being run in a hidden VM on top of your MacOS, this solution will not work here.
Some suggestions:
Migrate your Postgres server to run in a docker container (in the same docker-compose file if you will). You can always do a port mapping in order to access it from your Postbird.
Or if you still insist on running it locally in your MacOS, here is a workaround that involves creating another docker container in the same docker network and perform a revert SSH tunneling.
Here are the steps to migrate the Postgres server to using docker container
Update your docker-compose with a new db service:
db:
image: postgres:10.5-alpine
environment:
POSTGRES_USER: $UDAGRAM_USERNAME
POSTGRES_PASSWORD: $UDAGRAM_PASSWORD
POSTGRES_DB: $UDAGRAM_DATABASE
ports:
- 35432:5432
volumes:
- <path where you want to persist your database data>:/var/lib/postgresql/data
You can now connect to your new postgres using Postbird at localhost:35432
EDIT 1
If you run your Postgres instance in AWS RDS, you will not need to make the changes above but follow other steps:
Make sure that your network can reach the RDS endpoint at port 5432. A best practice here is to update the security group inbound rules to allow only port 5432 from only your IP address (how to do that is out of the scope of this answer but can easily be found from AWS documentation)
Update the value of UDAGRAM_HOST to be the RDS endpoint which can be found from the AWS RDS console.

Connecting Redis localhost from Docker

I have a Node.js application where I use Redis, I am trying to connect the Docker container and the locally running Redis.
Tried solutions:
vim /usr/local/etc/redis.conf
Updated
bind 127.0.0.1
To
bind 0.0.0.0
Stopped the redis and start it again and tried running the docker
With the above thing tried running docker run -p 4000:8080 -p 6379:6379 -t node-app
Both above didn't worked getting the below error
Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
Update: I am checking it on Mac.
In Dockerfile add this
Docker v19.03
ENV REDIS_HOST "redis://host.docker.internal"
when i using it on node.js
const REDIS_HOST = process.env.REDIS_HOST ? process.env.REDIS_HOST : ""
const client = redis.createClient(REDIS_HOST)
"docker.for.mac.localhost" instead of localhost or '127.0.0.1' will work :), it worked for me on mac machine.
If you use default networking (--network="bridge"), you could simply use the IP address of the gateway between the Docker host and the bridge network, i.e. 172.17.0.1. Here is the documentation. This would work on all platforms, not only on a Mac.
You just need to set the docker internal host in your node app's config.json file:
"redisHost": "host.docker.internal"
You don't need to change any Redis configuration on your local.

Docker cannot access mariadb server

I am newbie on docker.
I want to migrate my nodejs app to docker, and existing database already installed on server (172.17.2.1). I set mariadb host 172.17.2.1 on my nodejs config.
After that, I created an images and run with :
docker run -p 3009:3009 -d my-node
actually its already running, but when I tested to open by browser, I got an error that my app cannot connect to 172.17.2.1 (connecting to database).
I try to create bridge IP (172.17.2.135) and make a same subnet, but still got a same error.
My images on docker inside doesn't know 172.17.2.1 on my LAN.
Please help me,
I use windows 10 environment
You have two options to allow your container to reach an external server:
Run your docker container on your host network:
docker run -p 3009:3009 --network host -d my-node
This way your container will be able to reach anything reachable from your machine
create a network bridge: in this case docker will route the traffic from the container to the external server. the bridge IP can't be your docker machine IP as you tried to do.

Failed to connect to duckling http server. Make sure the duckling server is running and the proper host and port are set in the configuration

I have made workplace on slack and app is registered there from where i get the necessary things like slack token and channel to put it into the credentials.yml file of the rasa. After getting all the credentials i tried to connect between the rasa bot and slack using the command as:
rasa run
and my credentials.yml contains:
slack:
slack_token: "xoxb-****************************************"
slack_channel: "#ghale"
Here i have used the ngrok to expose a web server running on your local machine to the internet
but getting the error :
rasa.nlu.extractors.duckling_http_extractor - Failed to connect to duckling http server. Make sure the duckling server is running and the proper host and port are set in the configuration. More information on how to run the server can be found on github: https://github.com/facebook/duckling#quickstart Error: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /parse (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))
Are you using Duckling? Duckling is a rule-based component to extract entities(docs).
If you are not using it, you can remove it from your NLU pipeline.
If you want to use it, the easiest way to do so is using docker:
docker run -p 8000:8000 rasa/duckling
The command above will run duckling and expose it on port 8000 of your host.
Just to add to #Tobias's answer;
If you are running some other service on port 8000, then you can bind any other port with th container's port and specify that in your pipeline config.
Example:
docker run -p <Desired Port, ex- 1000>:8000 rasa/duckling
Change config file to reflect that. Your pipeline should include
- name: DucklingHTTPExtractor
# https://rasa.com/docs/rasa/nlu/components/#ducklinghttpextractor
url: http://localhost:<Desired Port, 1000>
Retrain your model with changed config.
After training, simply run: rasa run
If you are doing numerical extractions (e.g. six to 6 etc), I presume this will be helpful for form filling on Rasa - you need to install docker and then expose duckling on port 8000
First, install docker (assumes Fedora but you can lookup other distros)
sudo dnf install docker
Second, activate docker
sudo systemctl start docker
Last, activate Rasa's docker
docker run -p 8000:8000 rasa/duckling
Your response should be - Status: "Downloaded newer image for
docker.io/rasa/duckling:latest Listening on http://0.0.0.0:8000"

Docker container returns empty response even if host is already 0.0.0.0

I dockerized my application and built an image successfully. However when I tried to make a container out of it, I still can't connect successfully.
Dockerfile:
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4888
CMD ["npm", "start"]
Docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
748f60fd36c4 ahchi-front:v2.0.4 "npm start" 34 minutes ago Up 34 minutes 0.0.0.0:4888->4888/tcp ahchi-front-v2.0.4.1
I already set the hostname to 0.0.0.0, but no avail.
Docker Logs:
[SOCKET] started
[SERVER] Listening on 0.0.0.0:4888
Yet my local machine still can't connect to it.
Curl:
curl localhost:4888
curl: (52) Empty reply from server
Same thing happens when I curled inside the container.
Tried the docker machine, and it also didn't work: (This one is refusing to connect, rather than returning an empty response.
curl 192.168.99.100:4888
curl: (7) Failed to connect to 192.168.99.100 port 4888: Connection refused
What should I do to solve this problem?

Resources