1

Overall: I don't want my django app to rely on a docker container as my db. In other words, when I run an image

docker run -p 8000:8000 -d imagename

I want this to know to connect to my local db. I have my settings.py configured to connect to a pg db, so everything is fine when I do something like

python manage.py runserver

Feel free to call out any incorrect use of certain terms or just my overall understanding of docker. So from all the tutorials I've seen they usually make a docker compose file that is reliant on a db-image that will be spun up in a container separate from the web app. Examples of things I've gone through: https://docs.docker.com/compose/django/#connect-the-database, http://ruddra.com/2016/08/14/docker-django-nginx-postgres/, etc. At this point I'm extremely lost, since I don't know if I do this in my Dockerfile, settings.py in my project, or docker-compose.yml (I'm guessing I shouldn't even have this since this is for a multi-container app which I'm trying to avoid). [Aside: Lastly, can one run a django app reliant on celery & rabbitmq in just one container? Like my pg example I only see instances of having them all in separate containers.] As for my Dockerfile it's pretty much this.

FROM python:3
ENV APP 'http://githubproj`
RUN git clone $APP \
&& cd $APP/projectitself \
&& pip install -r requirements.txt
CMD cd $APP_DIR/mydjangoproject && gunicorn mydjangoproject.wsgi:application --bind 0.0.0.0:8000
chris123
  • 177
  • 2
  • 13
  • 1
    The DB is on your docker host or some remote machine? – johnharris85 Jun 17 '17 at 20:17
  • @johnharris85. So for now I'm trying to do it on my own local machine -- but the eventual goal is to access a remote machine that will be hosting the db. From my understanding, I will not have a container that's running a postgres image. – chris123 Jun 17 '17 at 20:59

2 Answers2

1

In order to allow your containerized Django application to connect to a local database running in the host machine, you need to enable incoming connections from your docker interface. You do that by including the following rule in your iptables in your local machine:

$ sudo iptables -A INPUT -i docker0 -j ACCEPT

Next, you have to configure your postgres server to listen on multiple addresses. Open /etc/postgresql/<version>/main/postgresql.conf and search for a line containing listen_addresses='localhost, and change that for:

listen_addresses='*'

After these changes, you should be able to connect to your local postgres database from inside the container.

This answer might give you further clarifications on how to connect to your local machine from your container.

1

To connect from the container to the host, you can you use the IP address of the docker0 bridge. Run ifconfig on the host to find the docker0 IP address (default is 172.17.0.1 I believe). Then connect to that from inside your container.

This is obviously not super host-portable as that IP might change between machines, so a wrapper script might be useful to find and inject that IP into the container.

Better still, postgres in a container! :p

Also, if connecting to a remote postgres then just provide the IP of the remote instance (no different to regular inter-connectivity here).

johnharris85
  • 13,054
  • 4
  • 43
  • 49
  • hello, I saw some articles that say putting Postgres in the container is not recommended. When your growing, is it good to put your database in services like `AWS RDS,` but when you start, I think it is good practice to put DB on the local machine? – Jasurbek Nabijonov Mar 04 '21 at 09:52