Connecting my flask with the mongodb Docker - mongodb

I am trying to connect my docker container running the gunicorn with another container of mongodb.
This is my Dockerfile for building the container
FROM python:3.8.10-buster
COPY requirements.txt /
RUN apt-get update
RUN apt-get -y install build-essential libpoppler-cpp-dev pkg-config
RUN apt install -y libsm6 libxext6
RUN apt-get install -y libxrender-dev
RUN pip3 install -r /requirements.txt
COPY . /app
WORKDIR /app
RUN ["chmod", "+x", "./gunicorn.sh"]
EXPOSE 4444
ENTRYPOINT ["./gunicorn.sh"]
I created the following docker-compose.yml for running the built container
version: '3.7'
services:
web:
build: .
image: 'flask/flask_docker'
container_name: 'xyz'
ports:
- 4444:4444
Following is the docker-compose for the Mongodb
version: '3.7'
services:
database:
image: 'mongo:3.6.8'
container_name: 'transactionsDB'
environment:
- MONGO_INITDB_DATABASE=abc
- MONGO_INITDB_ROOT_USERNAME=abc
- MONGO_INITDB_ROOT_PASSWORD=abc#v_1
ports:
- '5555:27017'
volumes:
- /home/ubuntu/abc/:/data/db
I used the following as the connection string from flask to connect with the mongodb
mongodb://abc:abc#v_1#transactionsDB:5555/abc
But everytime I get the following error pymongo.errors.ServerSelectionTimeoutError

Related

error 111 connection refused, topology type :unknown

I am trying to containerise an python-flask application which uses MongoDB as database.
the error that I am getting
The error is same when whether I run the Dockerfile of project or the Docker-compose file.
It works fine when I run it on my machine locally.
My DOCKERFILE
FROM python:3
COPY requirements.txt ./
WORKDIR /
RUN apt update -y
RUN apt install build-essential libdbus-glib-1-dev libgirepository1.0-dev -y
RUN apt-get install python-dev -y
RUN apt-get install libcups2-dev -y
RUN apt install libgirepository1.0-dev -y
RUN pip install pycups
RUN pip install cmake
RUN pip install dbus-python
RUN pip install reportlab
RUN pip install PyGObject
RUN pip install -r requirements.txt
COPY . .
CMD ["python3","main.py"]
MY DOCKER-COMPOSE.YML
version: '2.0'
networks:
app-tier:
driver: bridge
services:
myapp:
image: 'chatapp'
networks:
- app-tier
links:
- mongodb
ports:
- 8000:8000
depends_on:
- mongodb
mongodb:
image: 'mongo'
networks:
- app-tier
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
- 27018:27017
I tried linking the two containers via --links but i am unable to figure out what actual problem is.
Your app is trying to mongodb with localhost:27017.
For your app, localhost, is the container where the app is running.
To access the mingoDb container you must use the service name in your docker-compose.yaml.
In your case: mongodb.
So the connection to the db should be: mongodb:27017.
To access mongodb directly from your host macchine you use localhost:27018. In this case localhost refers to your hostsystem (your pc).
Your docker-compose is a bit obsolete. You can update it like so:
version: '3.9'
networks:
app-tier:
driver: bridge
services:
myapp: // this is a service name
image: 'chatapp'
networks:
- app-tier
ports:
- 8000:8000
depends_on:
- mongodb
mongodb: // this is the servicename to connect from the app container
image: 'mongo'
networks:
- app-tier
ports:
- 27018:27017
You can remove also allowempty password.

Run MongoDB and RabbitMQ in Dockerfile

I'm trying to run MongoDB and RabbitMQ in docker using Dockerfile to test my python app. what's the best way to do that?
I did
FROM python:latest
RUN apt-get update
RUN apt-get install -y rabbitmq-server wget
RUN wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
RUN touch /etc/apt/sources.list.d/mongodb-org-5.0.list
RUN apt-get install -y mongodb-org
RUN sudo apt-get update
RUN sudo apt-get install -y mongodb-org
but it doesn't seem to work.
Using Dockerfile you can only run one service at a time if you want to run 2 services at the same time, you have to use docker-compose
Here is a docker-compose.yaml, you can use to run 2 MongoDB and rabbit-mq at the same time.
version: '3.7'
services:
mongodb_container:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: rootpassword
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db
rabbitmq3:
container_name: "rabbitmq"
image: rabbitmq:3.8-management-alpine
environment:
- RABBITMQ_DEFAULT_USER=myuser
- RABBITMQ_DEFAULT_PASS=mypassword
ports:
# AMQP protocol port
- '5672:5672'
# HTTP management UI
- '15672:15672'
volumes:
mongodb_data_container:

localhost not accesible to load flask app via docker container

I have a flask-mongodb project where I use docker-compose to create a container of my flask app with mongodb data as backup . Now I can load my app perfectly in my linux vm browser with typing localhost:5000 but in windows 10 I get that my connection is refused when typing the same thing in chrome .
My docker-compose.yml file :
version: '2'
services:
mongodb:
image: mongo
restart: always
container_name: mongodb
ports:
- 27017:27017
volumes:
- ./mongodb/data:/data/db
flask-service:
build:
context: ./flask
restart: always
container_name: flask
depends_on:
- mongodb
ports:
- 5000:5000
environment:
- "MONGO_HOSTNAME=mongodb"
My DockerFile :
FROM ubuntu:16.04
MAINTAINER user <user#gmailcom>
RUN apt-get update
RUN apt-get install -y python3 python3-pip
RUN apt-get install -y bcrypt
RUN pip3 install Flask-PyMongo py-bcrypt
RUN mkdir /app
RUN mkdir -p /app/templates
COPY webservice.py /app/webservice.py
ADD templates /app/templates
EXPOSE 5000
WORKDIR /app
ENTRYPOINT ["python3" , "-u" , "webservice.py" ]
How I connect to mongodb with my flask app :
from pymongo import MongoClient
mongodb_hostname = os.environ.get("MONGO_HOSTNAME","localhost")
client = MongoClient('mongodb://'+mongodb_hostname+':27017/')
db = client['MovieFlixDB']
#more code ...
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
I would appreciate your help with this . Thank you in advance

How to dockerize my dotnet core + postgresql app?

I have a dotnet core application created with Angular template that communicates with a postgresql database.
On my local machine, I run the following command on my terminal to run the database container:
docker run -p 5432:5432 --name accman-postgresql -e POSTGRES_PASSWORD=mypass -d -v 'accman-postgresql-volume:/var/lib/postgresql/data' postgres:10.4
And then by pressing F5 in VsCode, I see that my application works great.
To dockerise my application, I added this file to the root of my application.
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
# install nodejs for angular, webpack middleware
RUN apt-get update
RUN apt-get -f install
RUN apt-get install -y wget
RUN wget -qO- https://deb.nodesource.com/setup_11.x | bash -
RUN apt-get install -y build-essential nodejs
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Web.dll"]
Now I think I have to create a docker-compose file. Would you please help me on creating my docker-compose.yml file?
Thanks,
I figured it out, here is my final version of docker-compose.yml file:
version: '3'
services:
web:
container_name: 'accman-web-app'
image: 'accman-web'
build:
context: .
dockerfile: Dockerfile
ports:
- '8090:80'
depends_on:
- 'postgres'
networks:
- accman-network
postgres:
ports:
- '5432:5432'
container_name: accman-postgresql
environment:
- POSTGRES_PASSWORD=mypass
volumes:
- 'accman-postgresql-volume:/var/lib/postgresql/data'
image: 'postgres:10.4'
networks:
- accman-network
volumes:
accman-postgresql-volume:
networks:
accman-network:
driver: bridge
You can use composerize to find out how you can add services to your docker-compose file.
Now you can run these following commands consecutively:
docker-compose build
docker-compose up
And voila!

Docker/Mongodb data not persistent

Iam running a rails api server with mongodb all worked perfectly find and I started to move my server into docker.
Unfortunately whenever I stop my server (docker-compose down) and restart it all data are lost and the db is completely empty.
This is my docker-compose file:
version: '2'
services:
mongodb:
image: mongo:3.4
command: mongod
ports:
- "27017:27017"
environment:
- MONGOID_ENV=test
volumes:
- /data/db
api:
build: .
depends_on:
- 'mongodb'
ports:
- "3001:3001"
command: bundle exec rails server -p 3001 -b '0.0.0.0'
environment:
- RAILS_ENV=test
links:
- mongodb
And this is my dockerfile:
FROM ruby:2.5.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
ENV APP_HOME /app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
COPY Gemfile* $APP_HOME/
RUN bundle install
COPY . $APP_HOME
RUN chown -R nobody:nogroup $APP_HOME
USER nobody
ENV RACK_ENV test
ENV MONGOID_ENV test
EXPOSE 3001
Any idea whats missing here?
Thanks,
Michael
In docker-compose, I think your "volumes" field in the mongodb service isn't quite right. I think
volumes:
- /data/db
Should be:
volumes:
- ./localFolder:/data/db