Answer the question
In order to leave comments, you need to log in
How to connect to local project on host machine from docker container?
So I work locally. One project is lifted via docker-compose, the other is configured the old fashioned way on the host machine. The first project (which works through docker) does not have its own database and takes data via API from the second one (which works on localhost), which has its own database.
The first project has a docker-compose.yml config for a long time, because on prod work goes through the docker. Until recently, I raised the first project also on localhost, but the management set me the task of dealing with the docker and expanding the config. While everything went through the localhost, there were no problems, because. setting up virtual hosts on nginx is not tricky. But as soon as I started working with the docker, a problem arose, which I voiced in the question. I can’t establish an API connection, despite the fact that in the first project all containers have risen normally and are working.
My current docker-compose.yml config of the first project:
version: '3'
services:
fpm:
restart: always
command: php-fpm
image: pk1z/php74-fpm-memcached-phalcon4-redis-sockets-blackfire-xdebug-gd:1
volumes:
- ./:/var/www/html
- ./phpSettings.conf:/usr/local/etc/php-fpm.d/zzz-phpSettings.conf
env_file:
- .env
environment:
- PHP_IDE_CONFIG=serverName=${BASE_HOST}
- XDEBUG_CONFIG=remote_enable=true remote_host=dockerhost remote_port=9000 idekey=SK_IDE_KEY remote_autostart=true
networks:
- default
- dockernet
dockerhost:
image: qoomon/docker-host
cap_add: [ 'NET_ADMIN', 'NET_RAW' ]
restart: on-failure
nginx:
restart: always
image: nginx:latest
depends_on:
- fpm
links:
- fpm:fpm
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- .:/var/www/html
ports:
- 8098:80
db:
image: mysql:5.7
ports:
- ${MYSQL_PORT}:3306
env_file:
- .env
environment:
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
MYSQL_DATABASE: "${DB_NAME}"
MYSQL_USER: "${DB_USER}"
MYSQL_PASSWORD: "${DB_PASSWORD}"
volumes:
- mirrors_db_data:/var/lib/mysql
volumes:
mirrors_db_data:
external: true
networks:
dockernet:
external: true
Answer the question
In order to leave comments, you need to log in
This is called extra-hosts, it works like a container hosts file. Below is an example of usage:
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- ./:/var/www
extra_hosts:
- "site.local:192.168.88.1"
volumes:
wordpress:
db:
[email protected]:~/tmp|⇒ docker-compose exec wordpress /bin/bash
[email protected]:/var/www/html# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.88.1 site.local
172.18.0.2 707dcf9d2f3f
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question