Flask-Migrate unable to connect to MySQL in a Docker container but the Flask app works fine #514
-
So, I'm kind of hitting my head against the wall on this, would appreciate if anyone could help me understand what I'm doing wrong. My code exists on my machine, in a folder. It is running Flask, uses Flask-SQLAlchemy, and Flask-Migrate. The MySQL Server is running in a Docker container. The container itself is stock from the Docker repo. The port 3306 is forwarded and visible from the host (where the code runs). I can connect using DBeaver. The connection string is:
Now, the funny part is that when I run the server using the standards My understanding is that Flask-Migrate uses Alembic below the covers and Alembic should be using SQLAlchemy, right, so this should "Just Work". Reading the docs and the issues here, I wasn't able to find this particular setup, so posting in case someone has this setup and knows what I'm doing wrong. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Are you using 127.0.0.1 inside the docker container? That IP address does not map to your host computer inside the container, it maps to the container itself. In general you can use |
Beta Was this translation helpful? Give feedback.
-
Just to make sure people can find this, I've solved this by just taking Another way would be what Miguel suggests which is to move the migrations out somewhere, regenerate the migrations scaffold and then move the migration files back in. This will also generate an up-to-date |
Beta Was this translation helpful? Give feedback.
Well, I feel stupid now...The solution was right in front of me, but I didn't actually read the source all the way. This commit or, rather, what it fixed, was bugging me. I generated my migrations folder way back, some years back, and the
env.py
file was old, which means it didn't have this new code that properly get the DB URI.And, no, I didn't compare the code. I figured this out because I sprinkled a lot of debug statements all over the place and finally found this SO comment that made me change
env.py
and it worked. Then I went to write this response and saw the commit. 😢Is there a way to regenerate the
env.py
file? I guess I could just move the migrations out, nuke the folder, re-run