R
R
Rustam Sadykov2020-09-13 17:57:32
PHP
Rustam Sadykov, 2020-09-13 17:57:32

Single database for api and web in docker?

Good afternoon.

There are 2 working applications:
- api written in lumen for mob. applications
- web application on laravel
They are in different repositories, and CI / CD is configured for them. They use a single base.
Set up docker-compose in the web application (4 php, nginx, mysql, redis services). Launched, everything works as it should.

The question is how to specify in api in docker-compose what would use the existing base container?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimitry T, 2020-09-14
@TrueKanonir

To solve this problem, you need to create an additional network, with the bridge driver , in the stack with the services you want to provide access to (connect to).
After creating the network, you need to specify which containers should connect to the network during startup in order to access them from another stack.
Example:
docker-compose.laravel.yml

services:
    mysql: 
      image: ...
      networks:
         - default
         - my_private_network

    redis: 
      image: ...
      networks:
         - default
         - my_private_network

networks:
  my_private_network:
    name: my_private_network
    driver: bridge

docker-compose.lumen.yml
php-fpm:
    image: ...
    networks:
      - default
      - my_private_network

networks:
  my_private_network:
    external: true
    name: my_private_network

- In the first stack, we create a new network and connect the containers we need;
- In the second stack, we declare the existence of the network that was created earlier, and also connect our php-fpm container to this network in order to communicate with other containers over the network;
After that, in the stack for Lumen , you can safely connect to mysql, redis containers by their aliases, as before:
Lumen .env
DB_HOST=mysql
REDIS_HOST=redis

Good luck!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question