简体   繁体   English

两个Docker容器如何相互通信?

[英]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). 容器“ A”由discourse.org应用程序和端口5432上的PostgreSQL数据库组成,容器“ B”具有在端口1000上运行的ROR应用程序。我想将与ROR应用程序的数据库连接到PostgreSQL(位于另一个容器中)。 How to connect ROR app with PostgreSQL database? 如何将ROR应用程序与PostgreSQL数据库连接?

RoR Application, docker-compose.yml RoR应用程序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 RoR应用程序,database.yml

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

docker ps 码头工人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 将数据库放入docker-compose.yml并在您的应用程序中添加指向它的链接

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: 并在database.yml中将您的容器名称作为主机:

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

You should use the links in docker-compose. 您应该使用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. 首先,您可以尝试在多容器模式下设置语篇: https : //github.com/discourse/discourse_docker#single-container-vs-multiple-container之后,您可能只有一个docker-compse.yml文件, struct:app,db(语篇数据库)和discourse_app。

Secondly, you can try to run PostgreSQL to outside using EXPOSE operator. 其次,您可以尝试使用EXPOSE运算符在外部运行PostgreSQL。 Add to your app.yml: 添加到您的app.yml:

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

You must have password for postgres user: Enter the container: 您必须具有postgres用户的密码:输入容器:

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

Also you can try to use Docker Networking, like this: 您也可以尝试使用Docker Networking,如下所示:

$ docker network ls

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

You will see network for Discourse container. 您将看到Discourse容器的网络。 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. 之后,您的RoR应用程序将在同一网络中运行。

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


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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