Answer the question
In order to leave comments, you need to log in
How to set up MYSQL DB connection: container - container on the same server?
Good afternoon.
I want to set up a docker application (php + lighttpd) with a connection to an external MySQL database.
The database also works in a container on the same host, port 0.0.0.0:3306 is exposed outside.
I could not set up a connection from php + lighttpd to the database at the address of the host server. If the database is also running in a container, but on a different server, then the connection works. Within the limits of one server - is not present.
For one server, it only worked after adding an external subnet to the php + lighttpd settings, but I suspect that there is a better solution.
docker-compose.yml:
version: '3.7'
services:
php5:
build: ./images/php5
restart: "always"
networks:
- backend
volumes:
- ./www:/var/www/
ports:
- "9000:9000"
lighttpd:
build: ./images/lighttpd
restart: "always"
networks:
- frontend
- backend
- mysql_default
links:
- "php5:php-fpm"
depends_on:
- php5
volumes:
- ./www:/var/www/
- ./images/lighttpd/etc/lighttpd:/etc/lighttpd
- ./logs/lighttpd:/var/log/lighttpd/
ports:
- "8081:80"
networks:
frontend:
backend:
mysql_default:
external: true
Answer the question
In order to leave comments, you need to log in
As far as I understand, the application containers and the mysql container are deployed in separate files and belong to separate docker bridges. If so, then everything is correct - they are isolated from each other, but accessible to the host machine, which knows about the existence and is connected to all available grids.
Look here: https://docs.docker.com/network/bridge/
That is, you need to either connect additional bridges to the application (as you did), or bring them into one grid (bridge) along with the base, or use the default bridge (which the documentation does not recommend to us in production, since the isolation is less).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question