简体   繁体   English

Docker compose无法访问其他容器的端口

[英]Docker compose can't access ports of other containers

I have 2 containers running with docker compose.我有 2 个使用 docker 组合运行的容器。 One of the containers is executing a shell script which should check if the other container has already started and is running on port 9990.其中一个容器正在执行 shell 脚本,该脚本应检查另一个容器是否已经启动并在端口 9990 上运行。

Even though the container is starting, the shell script echos nothing.即使容器正在启动,shell 脚本也没有回应。

   keycloak:
      image: jboss/keycloak:latest
      volumes: 
        - ./imports/cache_reload/disable-theme-cache.cli:/opt/jboss/startup-scripts/disable-theme-cache.cli
        - ./imports/themes/custom/:/opt/jboss/keycloak/themes/custom-theme/
        - ./imports/realm/realm-export.json:/opt/jboss/realms/custom-import.json
      environment:
        DB_VENDOR: MYSQL
        DB_ADDR: mysql
        DB_DATABASE: keycloak
        DB_USER: keycloak
        DB_PASSWORD: password
        KEYCLOAK_USER: admin
        KEYCLOAK_PASSWORD: Pa55w0rd
      ports:
        - 8080:8080
      depends_on:
        - mysql
  keycloak_installer:
    image: solr:6.6-alpine
    volumes:
      - ./imports/scripts/import-realm.sh:/docker-entrypoint-initdb.d/init.sh
    depends_on: 
      - keycloak

The shell script is the following: shell 脚本如下:

echo "MOIN LEUDE TRYMACS HIER!"

while ! nc -z localhost 9990; do
    sleep 1
    echo "Waiting for keycloak server startup 9990..."

    echo "$(nc -z localhost 9990)"
done

The first echo is printed, but then nothing else is printed.打印了第一个回声,但随后没有打印任何其他内容。

The container keycloak is running on Port 9990.容器 keycloak 在端口 9990 上运行。

Please help, thanks请帮忙,谢谢

You have to understand more detail about network in docker compose.您必须在 docker compose 中了解有关网络的更多详细信息

To solve your issue, you need:要解决您的问题,您需要:

  1. Add network in your docker compose file for each container (there is a default network but to understand the mechanism, you can define it explicitly).在每个容器的 docker 组合文件中添加网络(有一个默认网络,但要了解该机制,您可以明确定义它)。 This must looks like this (under ports for example) for the first container (named keycloak ):对于第一个容器(名为keycloak ),这必须看起来像这样(例如在端口下):
     ports:
       - 8080:8080  
     networks:
       - keycloak_network

On the second container (named keycloak_installer ) (you must expose the port that you want to request in the first container):在第二个容器(名为keycloak_installer )上(您必须在第一个容器中公开要请求的端口):

    depends_on: 
      - keycloak
    networks:
      - keycloak_network
  1. On your script call explictly the second container which will be now available by the network.在您的脚本调用中,网络现在可以使用第二个容器。 You must change your code by this:您必须通过以下方式更改您的代码:
    nc -z keycloak_installer 9990

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

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