Docker-compose: Connect to database using localhost & mysql as hostname
Solution 1
As said by @Sai Kumar you can connect the docker containers to the host
network and then can use localhost to access services. But the problem is the port will be reserved by that container and will not be available till it is deleted.
But from your question, the following sentence caught my attention
Without Docker, I'm able to run PHP scripts which leverage magento core modules by loading it. But with Docker and the blog post configuration, I'm not able to do it anymore as Magento is weirdly expecting for a DB hostname called "mysql"
So If I understand properly Magento is expecting to connect to MySQL with mysql
as hostname instead of localhost
. If so this can be easily solved.
How?
So in docker, there is a concept called service discovery. I've explained the same in many of my answers. Basically what it does is it resolves IP of containers by container hostname/aliases.So, instead of connecting between containers using IP address you can connect between them by using their hostname such that even if container restarts(which results in change of IP), Docker will take care of resolving it to respective container.
This works only with user-defined networks. So what you can do is create a bridge network and connect both Magento and Mysql to it. and give the container_name
as mysql
for mysql or you can also use alias as mentioned here. So putting it all together a sample docker compose will be
version: '3'
services:
mariadb:
image: mariadb:5.5
container_name: mysql #This can also be used but I always prefer using aliases
networks:
test:
aliases:
- mysql #Any container connected to test network can access this simply by using mysql as hostname
php-fpm:
build: docker/php-fpm
networks:
test:
aliases:
- php-fpm
networks:
test:
external: true
More references
1. Networking in Compose.
2. Docker Networking.
Solution 2
Yes you can connect throught the your db with localhost or 127.0.0.1 But this is only possible when you create docker-compose network in host mode. But when you set your docker network in host mode then containerising concept will failed. So you have to choose host or bridge network mode
You can find networking in docker-compose
network_mode: "host"
Related videos on Youtube
Comments
-
vidu.sh almost 2 years
I'm currently trying to dockerize my app for local development. For a bit of context, it is using Magento.
I have a configuration file where I'm used to set
127.0.0.1
as MySQL hostname as the web app is running on the same host as MariaDB.Initially, I've tried to link my container within my docker-compose file using 'links' (below an extract of my docker-compose setting at this point)
mariadb: image: mariadb:5.5 php-fpm: build: docker/php-fpm links: - "mariadb:mysql"
At this point, MariaDB was reachable by setting
mysql
as hostname in my configuration file instead of127.0.0.1
. However, I wanted to keep127.0.0.1
.After a bit of digging, I've found this blog post where it is explained how to set up containers so that it can be reached through
localhost
or127.0.0.1
This is working as I'm expecting but it has a flaw.
Without Docker, I'm able to run PHP scripts which leverage magento core modules by loading it. But with Docker and the blog post configuration, I'm not able to do it anymore as Magento is weirdly expecting for a DB hostname called "mysql".
Is there anyway through docker-compose to have a container be reachable with
localhost
and an hostname?Without Docker, if I install MariaDB on my host machine, I am able to connect to its instance through
127.0.0.1:3306
ormysql://
. I want to get a similar behaviour.-
Honk der Hase about 4 yearsObviously this is NOT a problem of docker, but of Magento...
-
vidu.sh about 4 yearsI'm not so sure about this as it works without docker, if I install MariaDB on my machine directly. The scripts were working with the 'links' though, it stopped working with the namespace networks..
-
-
Sai Kumar about 4 yearsAnd don't add any links in your docker-compose when you add links with network mode it will failed