Communication between two ddev projects

回眸只為那壹抹淺笑 提交于 2019-12-24 11:07:41

问题


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?


回答1:


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.




回答2:


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"


来源:https://stackoverflow.com/questions/51710272/communication-between-two-ddev-projects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!