DEV Community

Chethana Gopinath
Chethana Gopinath

Posted on • Edited on

2 1

How to connect Docker MySQL image to existing Rails app?

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
Enter fullscreen mode Exit fullscreen mode

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!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more