8

I got two ddev projects that needs to interact with each other. When a ran into some issues, I check the resolved IP for the connection. I did it by ssh into project1 and ping project2 (ping project2.ddev.local) The domain resolves to 127.0.0.1 So every request I send to this domain will stay in the current container and is not routet to the other project.

Steps to reproduce: Start two separate ddev containers and ssh into one of them. Try to ping the the other project by using the ddev domain.

Is there a solution that two (or more) projects can interact with each other?

Tim Schreiner
  • 513
  • 4
  • 14
  • Do you want to communicate via http or some other mechanism? Currently ddev isn't architected with the idea of two projects talking to each other, either via the router or not. Maybe you could say a little more about what you're trying to accomplish. – rfay Aug 06 '18 at 22:29
  • Hi, I got two Laravell projects that build a REST API for a smartphone application. The first application is currently the main entrypoint for the API. The second application if for handling emails and push notifications. So my first application needs to call the second application by a http request (currently curl ist used) to add some email or push notification to the spool. In the future, I want to split up the main application in several smaller applications to archive a better maintainability. I'm working with mock data, but sometimes I want to try a real request to the mail application. – Tim Schreiner Aug 08 '18 at 05:10
  • There are two basic ways to interact with an http project - you can go through the router which proxies you through to the web container (and where the "official" *.ddev.local URl goes). Or you can interact directly with a container by name. So for example, the web container connects to the db container by the name "db", and if you set up solr, the typical setup has that service/container named "solr". So... you could run both on one container, or you could add an additional service, seems like the way to go... https://ddev.readthedocs.io/en/latest/users/extend/additional-services/ – rfay Aug 09 '18 at 03:45

4 Answers4

20

Edit 2019-01-08: It's actually easy to do this with just the docker name of the container, no extra docker-compose config is required. For a db container that's ddev-<projectname>-db. So you can access the db container of a project named "d8composer" by using the hostname ddev-d8composer-db; for example mysql -udb -pdb -h ddev-d8composer-db db


Here's another technique that actually does have two projects communicating with each other.

Let's say that you have two projects named project1 and project2, and you want project2 to have access to the db container from project1.

Add a .ddev/docker-compose.extradb.yaml to project2's .ddev folder with this content:

version: '3.6'
services:
  web:
    external_links:
      - ddev-project1-db:proj1-db

And now project1's database container is accessible from the web container on project2. For example, you can mysql -h proj1-db from within the project2 web container.

Note that this is often a bad idea, it's best not to have two dev projects depend on each other, it's better to figure out development environments that are as simple as possible. If you just need an extra database, you might want to try How can I create and load a second database in ddev? . If you just need an additional web container as an API server or whatever, the other answer is better.

rfay
  • 9,963
  • 1
  • 47
  • 89
3

As per the documentation https://ddev.readthedocs.io/en/latest/users/usage/faq/#can-different-projects-communicate-with-each-other, the web container of any project can be accessed by another project by following the pattern https://ddev-<PROJECT-NAME>-web. In Drupal, if you are using the entity_share module and want to add one website as a client then you can add https://ddev-<CLIENT-PROJECT-NAME>-web and it will work.

MutantMahesh
  • 1,480
  • 15
  • 20
1

I tried this and it worked quite nicely; the basic idea is to run a separate ddev-webserver as a service. We usually think of a ddev "service" as something like redis or memcache or solr, but it can really be an API server of any type, and can use the ddev-webserver image (or any other webserver image you want to use).

For example, add this docker-compose.api.yaml to your project's .ddev folder (updated for ddev v1.1.1):

version: '3.6'

services:
  myapi:
    image: drud/ddev-webserver:v1.1.0
    restart: "no"
    labels:
      com.ddev.site-name: ${DDEV_SITENAME}
      com.ddev.approot: $DDEV_APPROOT
      com.ddev.app-url: $DDEV_URL
    volumes:
      - "../myapi_docroot/:/var/www/html:cached"
      - ".:/mnt/ddev_config:ro"

  web:
    links:
      - myapi:$DDEV_HOSTNAME

and put a dummy index.html in your project's ./myapi_docroot.

After ddev start you can ddev ssh -s myapi and do whatever you want there (and myapi_docroot is mounted at /var/www/html). If you ddev ssh into the web container you can curl http://myapi and you'll see the contents of your myapi_docroot/index.html. Your myapi container can access the 'db' container, or you can run another db container, or ...

Note that this mounts a subdirectory of the main project as /var/www/html, but it can actually mount anything you want. For example,

    volumes:
      - "../../fancyapiproject/:/var/www/html:cached"
rfay
  • 9,963
  • 1
  • 47
  • 89
1

A simple example of extra_hosts. I needed to use an HTTPS URL in a Drupal module's UI, entity_share, to cURL another ddev site.

On foo I add a .ddev/docker-compose.hosts.yaml

version: '3.6'
services:
  web:
    extra_hosts:
      - bar.ddev.site:172.18.0.6
DarkoDev
  • 29
  • 2