简体   繁体   中英

connect two application running separatly on docker-compose

I have a django application with mysql (chatbot) and rails application with mongodb each run over docker-compose They both running separately I want to connect them together, how can i accomplish that ???

My docker-compose file with rails

#with rails on the dockerfile
version: '3.6'
services:
  db:
    image: mongo:latest
    volumes:
      - ./tmp/db:/var/lib/mongo/data
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/IMS
    ports:
      - "3000:3000"
    depends_on:
      - db
  bundle:
    image: ims_web
    volumes:
      - /bundle

The django prroject :

#with django on the dockerfile
version: '3.6'
services:
  db:
    image: mysql:latest
    volumes:
      - ./tmp/db:/var/lib/mysql/data
  web:
    build: .
    command: bundle exec rails s -p 4000 -b '0.0.0.0'
    volumes:
      - .:/SDV
    ports:
      - "4000:4000"
    depends_on:
      - db
  bundle:
    image: sdv_web
    volumes:
      - /bundle

You just need to make sure that the containers you want to talk to each other are on the same network. You need to create a custom network in your docker compose files.

For more reference, you can refer to https://docs.docker.com/compose/networking/

Communication between multiple docker-compose projects

If these applications are meant to be run together, I would suggest combining your docker-compose files here and identifying each service with a unique name (ie, don't duplicate service names, call them mongo-db and mysql-db or something -- same with the web services).

At that point, so long as all of your services share a common network, you will be able to intercommunicate by using the service name as hostname. For example, you can hit your new rails-web service from django-web via http://rails-web/ .

See Hemant's answer for Docker networking documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM