Want to connect mongodb docker service with my django application - django

I want to create two docker services one is mongodb service another one is web service build using django. And i need that web-service (django app) which need to be connected to that mongodb docker service.
but i dont know how to connect with mongodb docker service in my django application which is also a service running in a same docker swarm .`This is my docker-compose.yml:
version: '3'
services:
mongo:
image: mongo:latest
command: mongod --storageEngine wiredTiger
ports:
- "27017:27017"
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- mongo
depends_on:
- mongo
Here i tried with mongoengine in settings.py of django application but failed
MONGO_DATABASE_NAME = "reg_task21"
MONGO_HOST = "mongo"
mongoengine.connect(db=MONGO_DATABASE_NAME, host=MONGO_HOST,port=27017)

You should add the username and password to connect statement:
mongoengine.connect(db=MONGO_DATABASE_NAME, username='root', password='example', host=MONGO_HOST,port=27017)

Related

How to connect to a postgres database in a docker container?

I setup my django and postgres container on my local machine and all working fine. Local server is running, database running but I am not being able to connect to the created postgres db.
docker-compose.yml
version: '3'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres:13.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=my_user
- POSTGRES_PASSWORD=my_password
- POSTGRES_DB=my_db
volumes:
postgres_data:
I tried this command:
docker exec -it container_id psql -U postgres
error:
psql: error: could not connect to server: FATAL: role "postgres" does not exist
I am very new to Docker.
You're not using the username and the password you provided in your docker-compose file. Try this and then enter my_password:
docker exec -it container_id psql -U my_user -d my_db --password
Check the official documentation to find out about the PostgreSQL terminal.
I would also like to add, in your compose file you're not exposing any ports for the db container. So it will be unreachable via external sources (you, your app or anything that isn't ran within that container).
I think you need to add environment to project container.
environment:
- DB_HOST=db
- DB_NAME=my_db
- DB_USER=youruser
- DB_PASS=yourpass
depends_on:
- db
add this before depends_on
And now see if it solves
You should add ports to the docker-compose for the postgres image,as this would allow postgres to be accessible outside the container
- ports:
"5432:5432"
You can checkout more here docker-compose for postgres

Docker-Compose Django can't connect to DB or Celery

I have everything on one network (it talks to another app in another docker-compose file).
But I keep getting this error:
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "db" (192.168.208.3) and accepting
TCP/IP connections on port 5000?
When I change my SQL_PORT=5432 (default port that's running in the postgres container) the error above disappears and my app is up, but then it has problems when trying to connect to celery or in shell it says db is not connected.
I have to use 5000 cause there is another postgres db on the other app in the second docker-compose setup.
I think I'm lost on the internals of networks. I was pretty sure I am suppose to use the exposed port of 5000 for my database.
version: "3.9"
services:
app:
build: .
command: python manage.py runserver 0.0.0.0:8000
container_name: app
environment:
- DEBUG=True
- PYTHONUNBUFFERED=1
- CELERY_BROKER=redis://broker:6379/0
- CELERY_BACKEND=redis://broker:6379/
- APP_BASIC_AUTH_PASSWORD=adPswd12*
- APP_BASIC_AUTH_USER=admin
- APP_TOKEN_AUTH=NHEC_UTILITY
- VTN_API_URL=vtn_app:8000
- VTN_API_TOKEN=NHECAPP_UTILITY
- SQL_PORT=5000
volumes:
- .:/code
ports:
- "9000:8000"
networks:
- app-web-net
depends_on:
- db
- celery-worker
- broker
app_test:
build: .
command: python manage.py test
container_name: app_test
environment:
- DEBUG=True
- PYTHONUNBUFFERED=1
volumes:
- .:/code
depends_on:
- db
db:
image: postgres:10
container_name: app_postgres
ports:
- 5000:5432
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_DB: nhec
POSTGRES_USER: nhec
POSTGRES_PASSWORD: nhec
networks:
- app-web-net
celery-worker:
build: .
command: celery -A app worker -l DEBUG
depends_on:
- db
- broker
environment:
CELERY_BROKER_URL: redis://broker:6379/0
networks:
- app-web-net
broker:
image: redis:6-alpine
ports:
- 6379:6379
networks:
- app-web-net
volumes:
db_data: {}
networks:
app-web-net:
driver: bridge
The ports will expose the postgres server on the your localhost port 5000, not to internal services. Your services will be able to reach the database container and its ports within the same network.
If you still want to change the default postgres port, here's a related answer that you might find helpful. Changing a postgres containers server port in Docker Compose
Your container name is actually app_postgres, not db as specified by this in your docker compose:
container_name: app_postgres
The other thing you can do is change the name of the container from app_postgres to something not the same as the postgres in your other app's docker compose file. Both postgres instances can have port 5432 exposed for your apps to use.

Docker-compose + Django on AWS C9

I'm trying to use a docker container to run a django application on the cloud base IDE from AWS (AWS C9).
The application is started correctly and the development server is started on http://127.0.0.1:8080/
However when browsing to the public URL of the cloud 9 application I'm getting the error 'no application seems to be running'.
When creating a django application without docker, the preview on AWS coud 9 works fine.
Are there any addtional settings required to get the cloud 9 preview to work?
This is my docker-compose file.
services:
web:
build: .
command: python manage.py runserver $IP:$PORT
volumes:
- .:/code
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres

How to connect a django app to a dockerized postgres db, both from a dockerized django and non-dockerized django using same DATABASE_HOST

I have a postgres container, docker-compose.yml:
services:
db:
container_name: db
expose:
- "5432"
ports:
- 5432:5432
volumes:
- postgres_data:/var/lib/postgresql/data/
And a django project with settings.py:
DATABASES = {
'default': {
'HOST': os.environ.get('POSTGRES_HOST', '127.0.0.1')
# etc
}
}
.env
POSTGRES_HOST_DJANGO=db
When I run my django app locally with manage.py runserver 0.0.0.0:8000 it connects fine, using the default POSTGRES_HOST=127.0.0.1, because .env isn't loaded.
I also run my django app in a container sometimes:
docker-compose.yml:
web:
#restart: unless-stopped
build: .
env_file: .env
command: bash -c "cd /app/src/ && python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- 8000:8000
links:
- db:db
However it uses the .env file and connects with POSTGRES_HOST=db
If I try to connect the locally run django app with POSTGRES_HOST=db it fails:
django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known
And if I try to run the django app in a container with POSTGRES_HOST=127.0.0.1, it fails in the same way.
How can I get them to use the same POSTGRES_HOST?
The problem seems to be in the network config. I don't see one.
The default behavior of docker-compose is that it creates a network for every compose file - by default the name is the folder name with '_default'.
Django App is in a different network and Postgres is in a different network
If your Django app and your Postgres containers are in different docker-compose files, using container names to resolve hosts will not work(by default, can be done with a custom network config) as they are in two different networks.
As you have done a port binding, you can directly access Postgres by giving host machine's private ip and port 5432 in the container, this way communication is happening through the host network.
If you find a need to make the containers talk to each other directly, make sure they are on the same docker network
I figured out how to do it. It wasn't getting them to use the same variable, it was to get them to read different variables based on how it was run. So:
from docker-compose.yml
web:
build: .
command: bash -c "cd /app/src/ && python manage.py runserver 0.0.0.0:8000
env_file: .env
environment:
POSTGRES_HOST: db # takes precendent over .env file
And in .env:
POSTGRES_HOST=127.0.0.1
Now, when I run locally, with ./manage.py runserver, it uses the .env file and connects to the db container properly at 127.0.0.1:5342
But if I run docker-compose up web, even though it also read the .env file, the environment variable provided in the compose file takes precedent and it uses POSTGRES_HOST: db and connects to the db container as well!

127.0.0.1 refused to connect in docker django

I'm trying to connect to an instance of django running in docker. As far as i can tell I've opened the correct port, and see in docker ps that there is tcp on port 8000, but it there is no forwarding to the port.
After reading the docker compose docs on ports, i would expect this to work (I can view pgadmin on 127.0.0.1:9000 too).
My docker compose:
version: '3'
services:
postgresql:
restart: always
image: postgres:latest
environment:
POSTGRES_USER: pguser
POSTGRES_PASSWORD: pgpassword
POSTGRES_DB: pgdb
pgadmin:
restart: always
image: dpage/pgadmin4:latest
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: admin
GUNICORN_THREADS: 4
PGADMIN_LISTEN_PORT: 9000
volumes:
- ./utility/pgadmin4-servers.json:/pgadmin4/servers.json
depends_on:
- postgresql
ports:
- "9000:9000"
app:
build: .
environment:
POSTGRES_DB: pgdb
POSTGRES_USER: pguser
POSTGRES_PASSWORD: pgpassword
POSTGRES_HOST: postgresql
volumes:
- .:/code
ports:
- "127.0.0.1:8000:8000"
- "5555:5555"
depends_on:
- postgresql
- pgadmin
I have tried with the following combinations for (app) ports, as are suggested here:
app:
...
ports:
- "8000"
- "8000:8000"
- "127.0.0.1:8000:8000"
but i still see This site can’t be reached 127.0.0.1 refused to connect. on trying to access the site.
I'm sure that this is a port forwarding problem, and that my server is turning correctly in django because i can run a docker attach to the container and curl a url with the expected response.
What am i doing wrong?
I was running my application using the command:
docker-compose run app python3 manage.py runserver 0.0.0.0:8000
With docker-compose you need to use the argument --service-ports to:
Run command with the service's ports enabled and mapped to the host.
Thus my final command looked like this:
docker-compose run --service-ports app python3 manage.py runserver 0.0.0.0:8000
Documentation on run can be found here
If you are running docker inside a virtual machine then you need to access your application through the virtual machine IP address and not using localhost or 127.0.0.1. Try to get the virtual machine IP. Also please specify in which platform/environment you installed and running the docker.