Answer the question
In order to leave comments, you need to log in
How to set up a connection to the database on the host from the container?
Configured nginx and php via docker-compose.yml
How to make php able to connect to local database?
version: '2'
services:
web:
build:
context: ./
dockerfile: web.docker
volumes:
- ./:/var/www
ports:
- "8080:80"
links:
- app
app:
build:
context: ./
dockerfile: app.docker
volumes:
- ./:/var/www
Answer the question
In order to leave comments, you need to log in
Pass the host, login and password to the app container through environment variables to access the database. In the app code, take the database connection parameters from the forwarded environment variables.
Or, mount a folder with a unix database socket in volumes and connect to this socket in the app code. But this will only work when the db and app are strictly on the same machine.
The simplest solution is `docker run --network host...` or add to `docker-compose.yml`:
version: '3'
services:
myservice:
// ...
network_mode: host
$ hostname -I | cut -d ' ' -f1
192.168.0.82
$ psql
psql (10.5 (Ubuntu 10.5-0ubuntu0.18.04))
Type "help" for help.
sergey=# SHOW config_file;
config_file
-----------------------------------------
/etc/postgresql/10/main/postgresql.conf
(1 row)
sergey=# \q
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = 'localhost,192.168.0.82' # можно просто звездочку (*) напечатать
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question