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.
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