samwalton
samwalton

Reputation: 551

How best to work with Django database from another Docker container?

So I'm building a tool using Docker Compose and Django. I have an app container running Django and a database container running a MySQL database. I want to add a third container, which will run a data collection script, with the intention of inserting that data into the database.

It's straightforward enough to insert the data directly using queries, but I'd rather be using the Django ORM for ease-of-use, consistency, and so that Django signals etc. are being fired correctly.

What's the most sensible way for this data collection script to run while making use of the Django ORM to save the data? Should I just be running the data collection process in the Django container?

Upvotes: 2

Views: 551

Answers (1)

zeynel
zeynel

Reputation: 953

Your third container will be same as your first django app container, since you want to use your models. The only difference is, it will not be acting as a http server but rather as a task executer to insert records to database.

You can write a custom django-admin command for that purpose and run this command on container. Or for complex cases, you can use celery.

Upvotes: 3

Related Questions