使用 Web 应用程序访问托管在另一个容器中的 docker 容器中的 nodejs

时间:2021-02-12 10:33:32

标签: node.js docker

我有 3 个容器在运行:

  1. nodejs
  2. tomcat
  3. mysql

tomcat 容器能够连接到 mysql 。 节点能够连接到mysql。

但是,当 tomcat 容器尝试使用 node-container-id:3004/3333 作为 hostname:portnode- 在 http 请求上连接到节点时container-internal-ip:3004/3333 作为 hostip:port,它不断给出 404 错误。

我的docker-compose.yml如下:

version: '2.1'

services:

  openmrs-referenceapplication-mysql:
    restart: "always"
    image: mysql:5.7
    ports:
      - 3333:3306
    command: "mysqld --character-set-server=utf8 --collation-server=utf8_general_ci"
    environment:
      MYSQL_DATABASE: ${MYSQL_DB:-openmrs}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root-pass}
      MYSQL_USER: ${MYSQL_USER:-openmrs_user}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:-my-pass}
    healthcheck:
      test: "exit 0"
    volumes:
      - openmrs-referenceapplication-mysql-data:/var/lib/mysql

  openmrs-referenceapplication-node:
    restart: "always"
    image: node:12.20.1
    depends_on :
      - openmrs-referenceapplication-mysql
    expose:
      - "3004"
    ports:
      - "3009:3004"
    links:
      - openmrs-referenceapplication-mysql
      - openmrs-referenceapplication
    environment:
      DB_DATABASE: ${MYSQL_DB:-my-db}
      DB_HOST: openmrs-referenceapplication-mysql
      DB_USERNAME: ${MYSQL_USER:-root}
      DB_PASSWORD: ${MYSQL_PASSWORD:-my-pass}
    volumes:
     - openmrs-referenceapplication-node-data:/home/azureuser/my-dir
     - ./Mydir:/Mydir
    command: node ./Mydir/bin/www


  openmrs-referenceapplication:
    restart: "always"
    image: openmrs/openmrs-reference-application-distro:2.8.0
    depends_on:
      - openmrs-referenceapplication-mysql
    ports:
      - "8001:8080"
    links:
      - openmrs-referenceapplication-mysql
    environment:
      DB_DATABASE: ${MYSQL_DB:-openmrs}
      DB_HOST: openmrs-referenceapplication-mysql
      DB_USERNAME: ${MYSQL_USER:-openmrs_user}
      DB_PASSWORD: ${MYSQL_PASSWORD:-my-pass}
      DB_CREATE_TABLES: 'true'
      DB_AUTO_UPDATE: 'true'
      MODULE_WEB_ADMIN: 'true'
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/openmrs/"]
      timeout: 20s
    volumes:
      - openmrs-referenceapplication-data:/usr/local/tomcat/.OpenMRS/
      - /usr/local/tomcat/.OpenMRS/modules/ # do not store modules in data
      - /usr/local/tomcat/.OpenMRS/owa/ # do not store owa in data
      - ./webservices.rest-2.29.0.omod://usr/local/tomcat/.OpenMRS/modules/webservices.rest-2.29.0.omod
      - /var/lib/tomcat8/webapps/ROOT.war:/usr/local/tomcat/webapps/ROOT
     
volumes:
   openmrs-referenceapplication-mysql-data:
   openmrs-referenceapplication-data:
   openmrs-referenceapplication-node-data:

知道我错过了什么吗?

谢谢

1 个答案:

答案 0 :(得分:0)

喂!

可以使用服务名称(并且仅此)访问您的容器。所以如果你想从另一个节点访问你的节点容器,你必须输入这个地址:http://openmrs-referenceapplication-node/<sub_path>。要在容器外(在本地计算机上)访问您的服务,您只需转到 :localhost:3009,就您而言。

解决连接问题的最佳方法是进入容器并尝试 ping 目标容器。此外,您应该使用更新版本的 compose 和 networks 而不是 links