To run the MySQL 5.7 image using Docker and access it from your Rails project

Vaibhav Jain
2 min readJul 26, 2023

--

To run the MySQL 5.7 image using Docker and access it from your Rails project, you can follow these steps:

  1. Install Docker: If you haven’t already, install Docker on your Ubuntu 20.04 system. You can find the installation instructions on the Docker website.

2. Pull the MySQL 5.7 image: Open a terminal and run the following command to pull the MySQL 5.7 image from Docker Hub:

docker pull mysql:5.7

3. Run the MySQL container: Once the image is downloaded, you can create and run a container from it using the following command:

docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=your_password -p 3306:3306 -d mysql:5.7

This command creates a container named “mysql-container” from the MySQL 5.7 image, sets the root password (replace “your_password” with your desired password), and maps port 3306 from the container to the host.

4. Verify the container is running: You can check if the container is running by executing the following command:

docker ps

You should see the “mysql-container” listed in the output, indicating that the container is running.

5. Connect to the MySQL container: To access the MySQL container, you can use a MySQL client or connect from your Rails project. Install the MySQL client on your Ubuntu system if you don’t have it already:

sudo apt update
sudo apt install mysql-client

Then, connect to the MySQL container using the following command:

mysql -h 127.0.0.1 -P 3306 -u root -p

Enter the root password you specified earlier when prompted.

6. Configure your Rails project: In your Rails project’s database configuration file (config/database.yml), update the database host to 127.0.0.1 and the port to 3306. For example:

default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: your_password
host: 127.0.0.1
port: 3306

Replace “your_password” with the password you set for the root user.

7. Run your Rails project: Start or restart your Rails project, and it should now be able to connect to the MySQL container using the specified host and port.

By following these steps, you should be able to run the MySQL 5.7 image using Docker and access it from your Rails project running on Ubuntu 20.04.

LinkedIn :- https://www.linkedin.com/in/vaibhav-jain-6454971a4

--

--