Home  >  Article  >  Database  >  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?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-26 21:48:03350browse

 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:

<code class="yaml">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</code>

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.

The above is the detailed content of How to Connect a Local MySQL Database to Your Containerized Application in a Production Environment?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn