Docker将NodeJS容器与前端JS中的Apache容器连接 [英] Docker connect NodeJS container with Apache container in front end JS

查看:58
本文介绍了Docker将NodeJS容器与前端JS中的Apache容器连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个在Docker中实现的聊天应用程序.我有一个带有socket.io的NodeJS容器和一个带有apache服务器和网站的容器.

I am building a chat application that i am implementing in Docker. I have a NodeJS container with socket.io and a container with apache server and website on it.

问题是我需要将网站(使用javascript)连接到NodeJS服务器.我已经看过Docker组成的码头,并阅读了有关网络的知识.文档说地址应该是容器的名称.但是当我尝试这样做时,我在浏览器控制台中收到以下错误:

The thing is i need to connect to the website(with javascript) to the NodeJS server. I have looked at the Docker-compose docks and read about networking. The docs said that the address should be the name of the container. But when i try that i get the following error in my browser console:

获取http://nodejs:3000/socket.io/socket.io.js net :: ERR_NAME_NOT_RESOLVED

整个项目在容器之外工作.我唯一不知道的是NodeJs容器和Apache容器之间的连接.

The whole project works outside containers.The only thing that i cannot figure out is the connection between the NodeJs container and the Apache container.

引发错误的代码:

<script type="text/javascript" src="//nodejs:3000/socket.io/socket.io.js"></script>

我的docker撰写文件:

My docker compose file:

version: '3.5'

services:

  apache:
    build:
      context: ./
      dockerfile: ./Dockerfile
    networks:
      default:
    ports:
      - 8080:80
    volumes:
      - ./:/var/www/html
    container_name: apache

  nodejs:
    image: node:latest
    working_dir: /home/node/app
    networks:
      default:
    ports:
      - '3001:3000'
    volumes:
      - './node_server/:/home/node/app'
    command: [npm, start]
    depends_on:
      - mongodb
    container_name: nodejs

networks:
  default:
    driver: bridge

谁能解释我如何将apache容器成功连接到NodeJS容器,以便它可以为socket.io.js文件提供服务?
如果需要,我可以提供更多的源代码.

Can anyone explain me how to succesfully connect the apache container to the NodeJS container so it can serve the socket.io.js file?
I can give more of the source code if needed.

推荐答案

nodejs 服务公开端口3001而不是3000. 3001:3000 是转发的端口映射:3001到:3000容器端口.因此,您需要将其指向 nodejs:3001 .

The nodejs service is exposing port 3001 not 3000. 3001:3000 is a port mapping which forwards :3001 to the :3000 container port. So you would need to point it to nodejs:3001.

但是,由于 nodejs 主机名不可被浏览器访问,因此我认为这不会起作用.您需要将其指向运行docker的主机,因为您要在其中公开这些端口.如果您在本地运行此程序,则可能看起来像这样:

However, I don't think that'll work since the nodejs hostname is not accessible by the browser. You need to point it to the host in which docker is running since you are exposing those ports there. If you are running this locally it might look like:

<script type="text/javascript" src="//localhost:3001/socket.io/socket.io.js"></script>

换句话说,您不是从apache服务连接到nodejs服务器,而是通过浏览器从外部访问它.

In other words, you are not connecting to the nodejs server from the apache service, you are accessing it externally through the browser.

这篇关于Docker将NodeJS容器与前端JS中的Apache容器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆