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
Related
In order to connect spring boot to mongoDB, i am using docker. And i created docker images for mongo and springboot application.
In the beginning running container is:
C:\Users\ASUS>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6dc2aa34ff8f mongo:latest "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:27017->27017/tcp mongodbproject
C:\Users\ASUS>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot-mongodb 1.0 5a1cf26b0e0b 5 minutes ago 550MB
mongo latest d98599fdfd65 2 days ago 696MB
However for making connection for them i used below command.
C:\Users\ASUS>docker run -p 8080:8080 --name springboot-mongodb --link mongodbproject:mongo -d springboot-mongodb:1.0
36120b50f09ae07c7c88ca10b1f478d726a1a5c318ee0c9d0f0fc3fb9eff5750
After that i can not see springboot-mongodb in running containers:
C:\Users\ASUS>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6dc2aa34ff8f mongo:latest "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:27017->27017/tcp mongodbproject
After that when i check Docker application it says status for springboot-mongodb as exited(1). when i was trying to run it again, it stops again.
Your Spring application is dying. Don't use -d, then look at the logs, and fix the error that is shown. Or use docker ps -a, then get the ID of the killed container, and run docker logs <id>
Ideally, you'd use Docker Compose, anyway. The --link option is deprecated.
x-mongo: &mongo-opts
MONGODB_USERNAME: mongoUser
MONGODB_PASSWORD: mongoPass
MONGODB_DATABASE: example
version: '2'
services:
web:
image: springboot-mongodb:1.0
ports:
- "8080:8080"
environment:
<<: *mongo-opts
MONGODB_PROTOCOL: mongodb # Example variables. Added in case you wanted to change to mongo+srv://
MONGODB_HOST: mongodb:27017
depends_on:
- mongodb
mongodb:
image: mongo:latest
ports:
- "27017:27017"
environment:
<<: *mongo-opts
MONGODB_ROOT_PASSWORD: l0c#l
I am trying to making it up mongodb container but i am getting error
Starting v2_mongodb ... done
Starting rockmongo_v2 ... done
Attaching to v2_mongodb, rockmongo_v2
v2_mongodb | Starting mongod...
v2_mongodb exited with code 100
Here is the content of docker-compose.yml file and output of docker ps
version: '2'
services:
v2_db:
image: sameersbn/mongodb:latest
container_name: "v2_mongodb"
ports:
- "27017:27017"
volumes:
- ./data/db:/data/db:rw
- ./data/db:/var/lib/mongodb:rw
environment:
- MONGO_DATA_DIR=/data/db
command: mongod --verbose --smallfiles --dbpath=/data/db # --quiet
rockmongo_v2:
image: javierjeronimo/rockmongo:latest
container_name: "rockmongo_v2"
ports:
- "27118:80"
links:
- v2_db:mongo
depends_on:
- v2_db
docker ps output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94794d6f3ff1 javierjeronimo/rockmongo:latest "/bin/sh -c 'service…" 38 minutes ago Up About a minute 0.0.0.0:27118->80/tcp rockmongo_v2
bd4ed92796db sameersbn/mongodb:latest "/sbin/entrypoint.sh…" 38 minutes ago Exited (100) About a minute ago v2_mongodb
I am not able to get the clue what can be the possible cause of above error?
List your volume links:
docker volumes ls
Delete the volumes: (In my case this freed up about 29.35 Gig)
docker volumes prune
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
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.
I'm trying to dockerize laravel project with mongodb but i'm getting
No suitable servers found (serverselectiontryonce set): [Failed connecting to '127.0.0.1:27107': Connection refused calling inmaster on 27017]
here's my docker-compose file
version: '3.6'
services:
mongodb:
image: mongo:latest
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
web:
build: .
volumes:
- ./tmp/db:/var/lib/mongodb/data
ports:
- 8000:8000
links:
- mongodb
depends_on:
- mongodb
Change the mongodb connection string from '127.0.0.1:27107' to 'mongodb:27107' because mongo is running in separate container. Mongodb instance can be reached with linked service name.