This is my docker-compose.yml configuration:
version: '3'
services:
web:
build:
context: .
dockerfile: './apache/Dockerfile'
restart: always
ports:
- '8100:80'
env_file:
- ./env/db.env
- ./env/modx.env
volumes:
- './public_html:/var/www/html'
- './elements:/var/www/html/elements'
db:
image: 'mariadb:10.3.38'
networks:
- my-mariadb-net
volumes:
- ./db-data:/var/lib/mysql
env_file:
- ./env/db.env
ports:
- '3307:3306'
command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8110:80
On the production my connection via php to mysql server is as following:
$database_dsn = 'mysql:host=localhost;dbname=dbname;charset=utf8mb4';
Locally i have installed Docker on my linux and want to replicate this so i dont need to change it manually. The only way that i can connect to database server from my host machine is to put name of the service in there - in that case db. However, i would like to have it as on my producation which is localhost.
My question is: Is any good way to get it? Im not worried about security risks as it will be always locally.
Thank you.
Is any good way to get it?
No.
Docker virtualizes network. There are 4 localhost here. Every containers has its own localhost interface. And your host has a localhost. They all are completely separate lo
interfaces.
You can use network: host
to disable network virtualization. Port redirection will also not affect anything, as there will be no network virtualization. This is not a good way, as it reduces the good things about docker and containerization, like separation.
A great solution, is to mount /var/run/mysql
into the container and specify to straight use mysql.sock
.
An acceptable solution is to make mysql listen on docker interface and use host.docker.internal to connect to host.
Could you please share with me some more detailed information about mounting /var/run/mysql into the container and specify the straight use mysql.sock`?
You mount the mysql sock and connect to it
version: "3"
services:
phpmyadmin:
volumes:
- type: bind
source: /var/run/mysql/
target: /var/run/mysql/
read_only: true
in phpmyadmin configuration, from How to connect with PhpMyAdmin to the localhost via a Unix socket? :
$cfg['Servers'][$i]['socket'] = '/var/run/mysql/mysql.sock';