如何在docker compose文件(docker-compose.yml)中链接容器?

hmmo2u0o  于 2023-04-11  发布在  Docker
关注(0)|答案(3)|浏览(161)

I am not able to link MySQL and play containers using docker compose. See the yml file below:

zookeeper:
    image: zookeeper:3.4
    ports:
       - 2181:2181
kafka:
    image: ches/kafka:latest
    ports:
       - 9092:9092
    links: 
       - zookeeper
myDpm:
    image: dpm-image:latest
    ports:
       - 9000:9000
    links:
       - kafka
       - zookeeper
mySql:
    image: mysql:latest
    environment:
       MYSQL_ROOT_PASSWORD: root
myMc3:
    image: mc3-v3:3.0
    ports:
        - 9001:9000
    links:
        - mySql:3306  
    environment:
       runMode: dev
myElastic:
    image: elasticsearch:2.4.0
    ports:
        - 9200:9200

I want to link mysql with my play scala project. I am getting an error:
[error] - [play.core.server.netty.PlayDefaultUpstreamHandler] - Cannot invoke the action java.sql.SQLTimeoutException: Timeout after 10001ms of waiting for a connection. at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:227) ~[com.zaxxer.HikariCP-java6-2.3.7.jar:na] at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:182) ~[com.zaxxer.HikariCP-java6-2.3.7.jar:na] at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:93) ~[com.zaxxer.HikariCP-java6-2.3.7.jar:na] at slick.jdbc.hikaricp.HikariCPJdbcDataSource.createConnection(HikariCPJdbcDataSource.scala:12) ~[com.typesafe.slick.slick-hikaricp_2.11-3.1.1.jar:na] at slick.jdbc.JdbcBackend$BaseSession.conn$lzycompute(JdbcBackend.scala:415) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.JdbcBackend$BaseSession.conn(JdbcBackend.scala:414) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.JdbcBackend$SessionDef$class.prepareStatement(JdbcBackend.scala:297) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.JdbcBackend$BaseSession.prepareStatement(JdbcBackend.scala:407) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.StatementInvoker.results(StatementInvoker.scala:33) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.StatementInvoker.iteratorTo(StatementInvoker.scala:22) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.Invoker$class.foreach(Invoker.scala:48) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.StatementInvoker.foreach(StatementInvoker.scala:16) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.Invoker$class.firstOption(Invoker.scala:24) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.StatementInvoker.firstOption(StatementInvoker.scala:16) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.StreamingInvokerAction$HeadOptionAction.run(StreamingInvokerAction.scala:57) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.jdbc.StreamingInvokerAction$HeadOptionAction.run(StreamingInvokerAction.scala:56) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.backend.DatabaseComponent$DatabaseDef$$anon$2.liftedTree1$1(DatabaseComponent.scala:237) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at slick.backend.DatabaseComponent$DatabaseDef$$anon$2.run(DatabaseComponent.scala:237) ~[com.typesafe.slick.slick_2.11-3.1.1.jar:na] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_111] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_111] at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_111] Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

dnph8jn4

dnph8jn41#

如上所述,here Docker链接在某种程度上被网络取代。
在Docker网络功能之前,您可以使用Docker链接功能来允许容器彼此发现。随着Docker网络的引入,容器可以通过其名称自动发现。
默认情况下,组合文件中的所有容器都加入默认网络(如果没有定义其他网络),并且您应该能够通过使用servicename和正确的端口从另一个服务访问一个服务,例如:3306 - 3306 - 3306 - 3306
更多信息可以在network docs from docker中找到。

j8ag8udp

j8ag8udp2#

正确链接到MySQL:

myMc3:
  ...
  links:
    - mySql

(不含3306)

pw9qyyiw

pw9qyyiw3#

如果mc 3-v3:3.0需要mysql容器,那么你将使用depends来解决依赖关系。它将首先运行mysql,然后运行mc 3。
并确保在mc 3-v3中,您在代码中使用mysql:3306连接mysql或使用mysql容器ip进行连接。

myMc3:
image: mc3-v3:3.0
ports:
    - 9001:9000
depends:
    - mySql
environment:
   runMode: dev

相关问题