C
C
Crash2021-02-07 14:44:10
Docker
Crash, 2021-02-07 14:44:10

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


When I go to localhost:8098 , I see this error:

601fd1a050554257360514.png

As you might guess, the second project is hanging on the skapi.test virtual host configured on the local machine. The connection to the API goes through the Symfony HTTP Client. I tried to make exactly the same request through Postman - the data is returned without problems.

As I understand it, you need to expand the fpm service config so that it connects to the virtual host of the host machine. Please tell me how to do it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2021-02-07
@Bandicoot

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:

in wordpress container added to hosts : site.local - 192.168.88.1
[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

add your external service to the container hosts and it will access them - just specify the correct IP address, of course.

P
part_os, 2021-02-07
@part_os

most likely your docker does not understand where skapi.test is located, because you probably registered it in hosts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question