loading...

How to connect Docker MySQL image to existing Rails app?

chethanagopinath profile image Chethana Gopinath Updated on ・2 min read

Greetings fellow devs,

Relatively new to Rails, but so far I have never had weird issues like this. I was having a lot of configuration issues while trying to do a bundle install on existing Rails apps. And brew install mysql did not help so I decided to create a container for mysql alone and use it for an existing Rails app.

These are what I have done so far
1) Created a docker container with MySQL image.
2) Went into my container to see if it connects to mysql commandline within it.
3) Tried running bundle install again but this shows up.

Screen Shot 2020-10-03 at 3.57.55 AM

4) Tried to change my database.yml as such

default: &default
  ...Other settings...
  adapter: mysql2
  host: mysql
  port: 3306

where mysql is the container name. Did not work.
5) I also read about Dockerfile and Docker compose - got extremely confused with the whole thing. I have just a single MySQL container that I would like to connect to my Rails app.

So, I'm here right now, frustrated for two days since the beginning of Hacktoberfest unable to run my Rails apps locally to start making contributions.
I don't know where to go with this and how to fix this issue.
Please help.

EDIT - Oct 7 '20
I had also asked about this in here and I got some pretty cool answers!

Solution
Always make sure to have mysql installed on your local, cause when installing the mysql2 gem using bundle install, the bundler would look for mysql developmental headers that are in the same space as the Rails app workspace. Unless your mysql docker image and the rails app run on the same space, you will always get an error. And it won't help even if you bind the port no of localhost to your docker image port on your database.yml cause you are just running bundle install, you are not running any code within the app. This is my explanation from what I read and understood. Hope it helps!

Discussion

pic
Editor guide