"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Connect a Local MySQL Database to Your Containerized Application in a Production Environment?

How to Connect a Local MySQL Database to Your Containerized Application in a Production Environment?

Published on 2024-11-17
Browse:356

 How to Connect a Local MySQL Database to Your Containerized Application in a Production Environment?

Docker Database Connectivity Options for Local Production Deployment

In a production environment, you may prefer to utilize your locally hosted MySQL database instead of a containerized database. If your Docker-compose.yml setup mirrors that provided above, you can seamlessly connect to your local database.

Connecting to Local MySQL Database Using Docker

To connect your local MySQL database with the containerized application, modify the docker-compose.yml file as follows:

version: '3'
services:
  web-app:
    build:
      context: .
      dockerfile: web-app/Dockerfile
    ports:
      - 8080:8080
    links:
      - mysql

  mysql:
    image: mysql:5.7
    ports:
      - 3306:3306
    environment:
    - MYSQL_ROOT_PASSWORD=password
    - MYSQL_DATABASE=Optimize

Internal Connection Parameter

To establish the connection between the container and the local database, append the parameter --add-host host.docker.internal:host-gateway when running the Docker container. This parameter ensures proper resolution of your local database using host.docker.internal.

By utilizing this technique, you can connect your local MySQL database to the containerized application, ensuring a seamless transition to production with the benefit of accessing your local database.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3