简体   繁体   中英

How two docker containers communicate each other?

I've two containers and want to communicate with each other. Container 'A' consist discourse.org application with PostgreSQL database on port 5432, container 'B' has ROR application which running on port 1000. I want to make database connection with ROR app to PostgreSQL(which is in another container). How to connect ROR app with PostgreSQL database?

RoR Application, docker-compose.yml

version: '2'
services:
  app:
    build: .
    command: bundle exec rails s -p 9000 -b '0.0.0.0'
    volumes:
      - ".:/slackcron"
    ports:
      - "9000:9000"

RoR Application, database.yml

development:
  <<: *default
  database: discourse
  username: muzammil
  password: '123'
  host: 0.0.0.0
  port: 5432

docker ps

CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                                                NAMES
92a9cb961e56        slackcroncom_app      "bundle exec rails..."   47 seconds ago      Up 46 seconds       0.0.0.0:9000->9000/tcp                                               slackcroncom_app_1
b727c2d0d5ba        local_discourse/app   "/sbin/boot"             10 minutes ago      Up 10 minutes       0.0.0.0:443->443/tcp, 0.0.0.0:5432->5432/tcp, 0.0.0.0:8080->80/tcp   app

Put database to docker-compose.yml and add link to it in your app

version: '2'
services:
  db:
    container_name: db
    image: postgres:9.4.1
    ports:
      - "5555:5555"
  app:
    container_name: app
    build: .
    command: rails server --port 3000 --binding 0.0.0.0
    ports:
      - "3000:3000"
    links:
      - hobover_db
    volumes:
      - .:/app

and in database.yml put your container name as host:

development: &default
  adapter: postgresql
  encoding: unicode
  database: myapp_development
  pool: 5
  username: postgres
  password:
  host: db

You should use the links in docker-compose. You will just have to link to two containers, and inside your configuration, you just reference the service name.

"Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified."

Firstly, you can try to set up discourse in multi-container mode: https://github.com/discourse/discourse_docker#single-container-vs-multiple-container After than, you may have single docker-compse.yml file with struct: app, db (discourse database) and discourse_app.

Secondly, you can try to run PostgreSQL to outside using EXPOSE operator. Add to your app.yml:

expose:
 ...
  - "5432:5432" # PostgreSQL ports

You must have password for postgres user: Enter the container:

su - postgres
psql -d postgres -c "ALTER USER postgres WITH PASSWORD '<new password>';"

Also you can try to use Docker Networking, like this:

$ docker network ls

NETWORK ID          NAME                DRIVER
7fca4eb8c647        bridge              bridge
9f904ee27bf5        none                null
cf03ee007fb4        <discourse_name>    host

You will see network for Discourse container. You can try use it with your application.

version: '2'
services:
  app:
    build: .
    command: bundle exec rails s -p 9000 -b '0.0.0.0'
    volumes:
       - ".:/slackcron"
    ports:
      - "9000:9000"
networks:
  default:
    external:
      name: <discourse_name>

After than your RoR application will be run in same network.

我通过使用容器“ A” IP到容器“ B” database.yml文件并将其与PostgreSQL连接来做到这一点


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