|Blogs|How to Connect Dockerized Applications to MySQL on the Host Machine?
ringring
DevOps
How to Connect Dockerized Applications to MySQL on the Host Machine?
Published by:Anuj Poudel
Published on:03 Jan, 2025
blogimage
Share

Connecting Dockerized Applications to MySQL running in the Host Machine

Running the database server in Docker is generally not preferred in production environments. Due to the requirements for scalability and backups, there is typically a preference for a standalone database server or at least a server running on the host machine. On the other hand, for running applications, Docker is generally preferred. Therefore, production deployments often involve the application server running in Docker containers, while the database server runs on the host machine. When running applications in Docker containers, a common challenge arises in connecting them to a MySQL database running on the host machine.

Docker containers operate in an isolated environment with their own network namespace. By default:

  • Containers cannot directly access the host machine's services using localhost or 127.0.0.1 because localhost refers to the container itself, not the host.
  • Special configurations are needed to expose the host machine's services to the container.
 

Problem Overview

Suppose you have a Docker container running a Node.js application with Express that needs to connect to a MySQL instance hosted on your local machine and If localhost is used as the host in the env file or in the application, the application might throw errors like:

  • ER_HOST_NOT_PRIVILEGED when the host machine's database is not accessible.

  • Host '<container_ip>' is not allowed to connect to this MySQL server when the MySQL server denies access.

  • Syntax errors when granting privileges or setting up user accounts in MySQL.

 

Not lets go into the step wise process of proper configuration

Step 1: Identify the Host IP Address

Docker containers are isolated from the host machine and communicate over a virtual network. To allow your Docker container to access the host’s MySQL instance, you need to determine the host's IP address that the container can reach.

On Linux:

The default bridge network Docker creates maps the host IP to 172.17.0.1. You can confirm this with:

ip addr show docker0

The output will include an IP address like 172.17.0.1. Use this as the host address.

On Other Platforms (macOS/Windows):

Docker provides a special DNS name host.docker.internal that resolves to the host machine's IP address. You can directly use host.docker.internal in your application configurations.

 

Step 2: Configure MySQL to Listen on the Correct Address

By default, MySQL may only listen on 127.0.0.1 (localhost), which makes it inaccessible to Docker containers. To allow connections from the Docker network:

  1. Open the MySQL configuration file, usually located at /etc/mysql/my.cnf or /etc/my.cnf.

  2. Look for the following lines and modify them:

    bind-address = 0.0.0.0
    mysqlx-bind-address = 0.0.0.0

    Setting bind-address to 0.0.0.0 ensures MySQL listens on all available network interfaces.

  3. Restart the MySQL server to apply changes:

    sudo systemctl restart mysql

Step 3: Grant Access to the Docker Container

MySQL uses user authentication tied to specific host IPs. For the Docker container to connect, you need to grant privileges to the container's IP.

Find the Container’s IP

To find the container’s IP address, run:

docker inspect <container_id> | grep IPAddress

Alternatively, the container IP can be dynamically assigned, so it's better to allow access to the entire Docker bridge network (172.17.0.0/16).

Grant Privileges in MySQL

Connect to the MySQL instance and execute the following commands:

CREATE USER 'root'@'172.17.0.%' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database.* TO 'root'@'172.17.0.%';
FLUSH PRIVILEGES;

Replace your_password and your_database with the appropriate values.


Step 4: Update Node.js Application Configuration

In your Node.js application, update the database connection settings to use the host’s IP address or host.docker.internal (on macOS/Windows). If you’re using the popular mysql2 or mysql libraries, the configuration might look like this:

Using the mysql2 Library:

Install the library if you haven’t already:

npm install mysql2

Set up the database connection in your application:

const mysql = require('mysql2');

const connection = mysql.createConnection({
  host: '172.17.0.1', // Or 'host.docker.internal' for macOS/Windows
  user: 'root',
  password: 'your_password',
  database: 'your_database',
});

connection.connect((err) => {
  if (err) {
    console.error('Error connecting to the database:', err);
    return;
  }
  console.log('Connected to the MySQL database.');
});

Step 5: Test the Connection

To verify the connection:

  1. Start your MySQL server on the host machine.

  2. Run the Docker container with your Node.js application.

  3. Check the application logs for successful database connection messages or use tools like telnet to test connectivity:

    telnet 172.17.0.1 3306

If everything is configured correctly, your Node.js application should now connect to the MySQL database.


Common Errors and Fixes

Error: Host '<container_ip>' is not allowed to connect to this MySQL server

  • Ensure the MySQL user is granted privileges for the correct IP address.

  • Use wildcards like 172.17.0.% to allow access from all containers in the Docker network.

Error: ER_ACCESS_DENIED_ERROR

  • Double-check the username and password in your Node.js application.

  • Ensure the MySQL user account is configured to allow connections from the container’s IP or network.

Error: Cannot connect to MySQL server on '172.17.0.1'

  • Verify that MySQL is configured to listen on 0.0.0.0 and that the service is running.

  • Check firewall settings to ensure traffic is allowed on port 3306.

Conclusion

By this process your nodejs application or any application running in the docker container can easily use the database server running in the host machine. For further assistance or clarity contact anuj@dallotech.com

 

Happy coding!

Other related blogs
Technology
Integrating Online Payment in NestJS using Factory Pattern: Project Setup, Service Layer and APIs (Part 1)

If you are integrating a payment features(eSewa, Khalti, ConnectIPS, Stripe, PayPal etc.) in NestJS or are interested in knowing how online payment features can be implemented, this blog is perfect for you.

Technology
Restore MSSQL Database file .bak on a Linux Server

If you need to restore a MSSQL database file (.bak) on a Linux server, this is the perfect blog for you. Additionally, anyone interested in web development, database management, or technology in general may also find this useful and interesting.

Technology
Hexagonal Architecture in NestJS with Sample Code

NestJS is javascript server side progressive framework combining Object Oriented Programming(OOP) and Functional Programming(FP). Since I got introduced to this framework, I have been loving it. Personal Opinion: At first, I became familiar with NestJS framework. After that, I wrote my first API , my first NestJS package and so on. But I was still exploring new things. This time, I wanted to learn about Hexagonal Architecture. Learning never stops !!! Rather than talking about theories, let’s directly dive into codes. I will be referencing code from the below mentioned repository. Github Link: Nestjs-Hexagonal-Architecture-Demo-Project

DevOps
How to Set Up CI/CD pipeline on Next.js project using GitLab on AWS EC2

Continuous Integration and Continuous Deployment (CI/CD) are essential for modern web development, ensuring fast, reliable, and automated software delivery. In this guide, we’ll walk you through setting up a CI/CD pipeline for a Next.js application using GitLab CI/CD. By automating the build and deployment process, you can eliminate manual tasks, reduce errors, and deploy updates seamlessly. Whether you're deploying to a VPS, cloud server, or a containerized environment, this tutorial will help you streamline the deployment of your Next.js app using Docker, GitLab CI/CD, and SSH. Let’s dive into automating your Next.js project for efficient, hassle-free deployment!

Discover Dallo Tech, a leading software development company offering expertise in Mobile and Web App Development, Data Analytics, Visualization, AI, and Blockchain solutions. Elevate your digital journey with our innovative technology services.

Open Hours

Sun - Fri:
10:00 am - 5:00 pm

Saturday:
CLOSED

Dallotech as tech partner
Business Team
Devops and Service
Design Team
Quality Assurance
Software Team
Mobile Team
Hire Expert Team
© 2018 - 2025 . DalloTech Pvt. Ltd. All Rights Reserved