...
/Database and Core App Connection through Docker
Database and Core App Connection through Docker
Learn how to connect the Flask application to MySQL database through Docker.
We'll cover the following...
Adding the database to the docker-compose.yml
file
To connect our Core app to the database, we must add the MySQL database as a service in the docker-compose.yml
file, as shown below:
Press + to interact
version: '2.2'services:backend:image: educative1/python_django_flask:latestcommand: 'python3 core.py'ports:- 5000:5000volumes:- .:/backendservicedepends_on:db:condition: service_healthydb:image: mysql:latestrestart: alwaysenvironment:MYSQL_DATABASE: coreMYSQL_USER: microserviceMYSQL_PASSWORD: microserviceMYSQL_ROOT_PASSWORD: microservicevolumes:- dbdata:/var/lib/mysqlports:- 3306:3306healthcheck:test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORDvolumes:dbdata:
Lines 10–11: We use the
depends_on
keyword to set the startup or shutdown order of services. Here, we set it todb
. This means the database service will be the first to start running, before any other service.Line 12: We use the
condition
keyword to state the condition under which dependency is satisfied. Here, it is ...