Cannot connect to mongo docker from another docker - mongodb

After looking this and this and this post, I couldn't connect to mongo from another docker.
This is a part of my DockerFile:
FROM ubuntu:16.04
...
WORKDIR /code
# Install project requirements
RUN pip3 install -r requirements.pip
ADD . /code
ENTRYPOINT ["/usr/bin/supervisord", "-c", "/code/supervisord.conf"]
In my telegram service I am connecting to mongo like this:
register_connection(
alias='main',
name='new_tetabot',
host='mongo',
port=27017
)
And this is my docker-compose.yml file:
version: '3'
services:
telegram:
hostname: nh-11
build: .
ports:
- "9001:9001"
- "5555:5555"
extra_hosts:
postgresql: 127.0.0.1
mongo: 127.0.0.1
redis: 127.0.0.1
es: 127.0.0.1
broker: 127.0.0.1
volumes:
- /root/telegram_logs:/root/telegram_logs
- /home/crawler/telegram/sessions:/root/crawler/telegram/sessions
- /root/downloads:/root/downloads
- /root/images:/root/images
restart:
always
depends_on:
- mongo
- redis
links:
- mongo
- redis
redis:
image: "redis:latest"
ports:
- "6379:6379"
expose:
- "6379"
mongo:
image: "mongo:4"
ports:
- "27017:27017"
expose:
- "27017"
When I use docker-compose up I get Failed to connect to mongo port 27017: Connection refused. But I have telnet from localhost (outside of docker) to 127.0.0.1 on port 27017.
My docker-compose version is docker-compose version 1.23.2, build 1110ad01 and when I run docker-compose up the result of docker ps is
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dfa23e7d07dd telegram_telegram "/bin/sh -c 'curl \"m…" 2 minutes ago Restarting (7) 11 seconds ago telegram_telegram_1
74d875f3828c mongo:4 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:27017->27017/tcp telegram_mongo_1
6ee448c1cc30 redis:latest "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6379->6379/tcp telegram_redis_1

You need to remove extra_hosts from your docker-compose.yml when using links. The telegram container is unable to resolve the correct address for mongo because it was overrided to 127.0.0.1.

Related

Could not connect to local postgres DB from docker using pgadmin4

I tried connecting to my local postgres DB from docker using pgadmin4 but it failed with unable to connect to server: timeout expired. I have my server running, and I used the same properties that are mentioned in the docker-compose.yml file while connecting to server in pgadmin4.
This is my docker-compose.yml
version: "3"
services:
web:
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=app
- DB_USER=postgres
- DB_PASS=secretpassword
depends_on:
- db
db:
image: postgres:10-alpine
environment:
- POSTGRES_DB=app
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=secretpassword
This is the screenshot of the error I get in pgadmin4
I tried changing the host address to something like localhost, host.docker.internal, 127.0.0.1 and the IPAddress by inspecting docker container. But I get the same result every time. I also tried adding pgadmin4 as a service in my docker-compose.yml file and tried, but got the same result there too.
I am confused what I am missing here.
Thanks in advance.
First, you didn't export any port from Postgres Container which might be 5432 as default, then you can't connect 8000 because that is binding for your application from your host.
Here is some description from docker-compose ports
When mapping ports in the HOST:CONTAINER format, you may experience erroneous results when using a container port lower than 60, because YAML parses numbers in the format xx:yy as a base-60 value. For this reason, we recommend always explicitly specifying your port mappings as strings.
so you can try to export port "5432:5432" from Postgres DB from container which your Pgadmin might need to use 5432 port.
version: "3"
services:
web:
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=app
- DB_USER=postgres
- DB_PASS=secretpassword
depends_on:
- db
db:
image: postgres:10-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_DB=app
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=secretpassword

Prisma, MongoDB, Docker "request to http://localhost:4466/ failed, reason: connect ECONNREFUSED 127.0.0.1:4466"

After I launch my docker-compose up command, everything starts up and I run prisma deploy which also works fine, yet my application still returns the above error. I have been trying to find a solution to this for days, and there is nothing helpful online, and the few similar questions have been closed or ignored. I would appreciate getting help with this issue.
Here is my docker-compose.yml file:
version: '3'
services:
prisma:
env_file:
- .env
image: prismagraphql/prisma:1.34
restart: always
ports:
- "4466:4466"
environment:
PRISMA_CONFIG: |
port: 4466
databases:
default:
connector: mongo
uri: ${MONGODB_URI}
host: host.docker.internal
JWT_SECRET: ${JWT_SECRET}
mongo:
env_file:
- .env
image: mongo:3.6
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD}
ports:
- "27017:27017"
volumes:
- mongo:/var/lib/mongo
web:
env_file:
- .env
build: .
volumes:
- .:/usr/app/
- /usr/app/node_modules
ports:
- "4000:4000"
environment:
DATABASE_URL: ${MONGODB_URI}
volumes:
mongo:
My Dockerfile:
FROM node:8.16.0-alpine
WORKDIR /usr/app
COPY package.json .
RUN npm install --quiet
COPY . .
ENV DOCKERIZE_VERSION v0.6.0
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz
CMD dockerize -wait tcp://mongo:27017 -wait tcp://prisma:4466 -timeout 60m npm start
My prisma.yml:
endpoint: http://localhost:4466
datamodel:
- db/types.prisma
- db/enums.prisma
databaseType: document
generate:
- generator: javascript-client
output: ./generated/prisma-client/
My prisma deploy command works, and it generates the mongo database, but when I try to query my application at localhost:4000, it looks like this and returns this error:
request to http://localhost:4466/ failed, reason: connect ECONNREFUSED 127.0.0.1:4466
But when I navigate to localhost:4466/_admin, the database is all set up fine and shows the three tables that should be there.
I checked if anything is running localhost:4466 by issuing this command: lsof -i :4466, and I can see that docker started up correctly.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 5923 sguduguntla 24u IPv4 0x89ea943c9b98ff09 0t0 TCP *:4466 (LISTEN)
com.docke 5923 sguduguntla 25u IPv6 0x89ea943c87111549 0t0 TCP localhost:4466 (LISTEN)
When I run docker ps, you can also see the following output with the three images:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa70fae37f10 prismagraphql/prisma:1.34 "/bin/sh -c /app/sta…" 35 minutes ago Up 31 minutes 0.0.0.0:4466->4466/tcp decal-board-graphql-server_prisma_1
d64b9f6dcd29 decal-board-graphql-server_web "docker-entrypoint.s…" 35 minutes ago Up 31 minutes 0.0.0.0:4000->4000/tcp decal-board-graphql-server_web_1
6f7dda5e58a0 mongo:3.6 "docker-entrypoint.s…" 35 minutes ago Up 31 minutes 0.0.0.0:27017->27017/tcp decal-board-graphql-server_mongo_1

AdminMongo with docker-compose doesn't work

I'm trying to access to my mongo database on docker with adminmongo.
Here's my docker-compose.yml
version: '3'
services:
mongo:
image: mongo
volumes:
- ~/data:/data/db
restart: always
expose:
- 6016
adminmongo:
image: mrvautin/adminmongo
expose:
- 1234
links:
- mongo:mongo
When i do a docker-compose up everything works fine, adminmongo also return me this : adminmongo_1_544d9a6f954c | adminMongo listening on host: http://localhost:1234
But when i go to localhost:1234 my navigator is telling me this page doesn't exist.
Here's what a docker ps return me :
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c27d4a89254 mrvautin/adminmongo "/bin/sh -c 'node ap…" 38 seconds ago Up 33 seconds 1234/tcp iris_adminmongo_1_544d9a6f954c
2a7496a8c56a mongo "docker-entrypoint.s…" 40 minutes ago Up 38 seconds 6016/tcp, 27017/tcp iris_mongo_1_7f00356a3adc
I've found 2 issues here:
1st: Exposing a port is not enough. expose is just documentation, you need to publish (bind) a port to the host to be reachable. This is how it's done:
ports:
- 1234:1234
2nd: you have to configure adminmongo to listen to 0.0.0.0 because by default it starts listening on 127.0.0.1 and this makes it accessible only inside the container itself. From the documentation page you've included in your question, the Configuration section states that this can be done by passing an environment variable:
All above parameters are usable through the environment which makes it very handy to when using adminMongo as a docker container! just run docker run -e HOST=yourchoice -e PORT=1234 ...
Since you are using docker-compose, this is done by the following:
environment:
- HOST=0.0.0.0
Working example:
version: '3'
services:
mongo:
image: mongo
volumes:
- ~/data:/data/db
restart: always
expose:
- 6016
adminmongo:
image: mrvautin/adminmongo
ports:
- 1234:1234
environment:
- HOST=0.0.0.0
Example of docker-compose works :
version: '3'
services:
server:
container_name: docker_api_web_container
image: docker_api_web
build: .
volumes:
- ./src:/usr/src/node-app/src
- ./package.json:/usr/src/node-app/package.json
environment:
- ENV=DEVELOPMENT
- PORT=4010
ports:
- '9000:4010'
depends_on:
- 'mongo'
mongo:
container_name: docker_mongo_container
image: 'mongo'
ports:
- '27017:27017'
adminmongo:
container_name: docker_adminmongo_container
image: mrvautin/adminmongo
links: ['mongo:mongo']
environment:
- HOST=0.0.0.0
ports:
- '1234:1234'
You have to expose your service to the outside world like this:
version: '3'
services:
mongo:
image: mongo
volumes:
- ~/data:/data/db
restart: always
adminmongo:
image: mrvautin/adminmongo
ports:
- 1234:1234
Now you can access your adminmongo by http://localhost:1234.
And you don't have to use links here.Since compose creates a network and joins all services in the compose files. You can access other containers with their service names.

How to use docker-compose to setup a mongodb with pymonogo app?

I am trying to spin up a python application with mongodb server using docker-composer, but I get the following error:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
I really don't understand what am I missing in my setup:
version: '3.6'
services:
web:
build: .
ports:
- "5000:5000"
- "9000:9000"
links:
- db
db:
image: mongo:latest
container_name: "mongodb"
ports:
- 27017:27017
command: mongod --smallfiles # --quiet
redis:
image: "redis:alpine"
I am not sure why I can't connect to the server which seems to be up (docker ps):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fb5a00bb8867 composedir_web "python app.py" 20 seconds ago Up 19 seconds 0.0.0.0:5000->5000/tcp, 0.0.0.0:9000->9000/tcp composedir_web_1
56aeae245ad5 mongo:latest "docker-entrypoint.s…" 14 minutes ago Up 20 seconds 0.0.0.0:27017->27017/tcp mongodb
51c64650bab8 redis:alpine "docker-entrypoint.s…" 14 minutes ago Up 21 seconds 6379/tcp composedir_red
You need to configure the python application to connect mongo on db:27017 instead of localhost:27017

Docker-compose mongoose

I'm new to Docker, and I'm trying the simplest of setups with docker-compose, but don't succeed to connect to Mongodb.
My docker-compose.local.yaml file:
version: "2"
services:
posts-api:
build:
dockerfile: Dockerfile.local
context: ./
volumes:
- ".:/app"
ports:
- "6820:6820"
depends_on:
- mongodb
mongodb:
image: mongo:3.5
ports:
- "27018:27018"
command: mongod --port 27018
My Docker file:
FROM node:7.8.0
MAINTAINER Livefeed 'project.livefeed#gmail.com'
RUN mkdir /app
VOLUME /app
WORKDIR /app
ADD package.json yarn.lock ./
RUN eval rm -rf node_modules && \
yarn
ADD server.js .
RUN mkdir config src
ADD config config/
ADD src src/
EXPOSE 6820
EXPOSE 27018
CMD yarn run local
In server.js I try to connect with:
mongoose.connect('mongodb://localhost:27018');
I also tried:
mongoose.connect('mongodb://mongodb:27018');
To run docker-compose:
docker-compose -f docker-compose.local.yaml up --build
And I receive the error:
connection error: { MongoError: failed to connect to server [localhost:27018] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27018]
What am I missing?
In server.js use mongodb instead of localhost:
mongoose.connect('mongodb://mongodb:27018');
Because containers in the same network can communicate using their service name.
Bear in mind that each container and your host have their own localhost. Each localhost is a different host: container A, container B, your host (each one has its own network interface).
Edit:
Be sure to get your mongo up:
docker-compose logs mongodb
docker-compose ps
Sometimes it doesn't get up because of disk space.
Edit 2:
With newer versions of mongo, you need to specify to listen to all interfaces too:
command: mongod --port 27018 --bind_ip_all
I think, that you should add links option in your config. Like this:
ports:
- "6820:6820"
depends_on:
- mongodb
links:
- mongodb
update
As I promised
version: '2.1'
services:
pm2:
image: keymetrics/pm2-docker-alpine:6
restart: always
container_name: pm2
volumes:
- ./pm2:/app
links:
- redis_db
- db
environment:
REDIS_CONNECTION_STRING: redis://redis_db:6379
nginx:
image: firesh/nginx-lua
restart: always
volumes:
- ./nginx:/etc/nginx
- /var/run/docker.sock:/tmp/docker.sock:ro
ports:
- 80:80
links:
- pm2
s3: # mock for development
image: lphoward/fake-s3:latest
redis_db:
container_name: redis_db
image: redis
ports:
- 6379:6379
db: # for scorebig-syncer
image: mysql:5.7
ports:
- 3306:3306