如何访问在nginx-proxy后面的theia docker容器上运行的应用程序?

时间:2019-04-09 19:13:43

标签: docker nginx jwilder-nginx-proxy theia

我正在nginx代理服务器容器(jwilder/nginx-proxy)后面运行theia docker容器。

在theia内部,我正在端口号上运行一个简单的ExpressJS应用程序。 8001。

我可以使用子域公开访问容器。

如何公开访问容器中运行的应用程序?

用于在docker上运行nginx-proxy的代码

docker run -d -p 80:80 --name nginx-proxy --net nginx-proxy -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy

用于在docker上运行theia的代码

docker run -d --name theia --expose 3000 --net nginx-proxy -e VIRTUAL_HOST=theia.example.com theia:theia1

可以使用http://theia.example.com公开访问theia容器。

这显然行不通: http://theia.example.com:8001

我尝试使用图像mashupmill/nginx-proxyncadou/nginx-proxy来实现https://github.com/jwilder/nginx-proxy/pull/259

将运行jwilder/nginx-proxy的容器替换为mashupmill/nginx-proxy后,我运行:

docker run -d --name theia --expose 3000 --net nginx-proxy -e VIRTUAL_HOST="theia.example.com=>http:80,app.example.com=>http:8001" -e VIRTUAL_PROTO=http theia:theia1

我不确定是否误解了mashupmill/nginx-proxy的功能或做错了什么。 理想情况下,上面的代码应该在http://theia.example.com处打开theia,在http://app.example.com处打开Express应用。

在本地运行docker时,访问在theia容器中运行的应用程序不是问题。我可以获取theia容器的本地IP地址,并使用http://172.16.0.2:3000打开theia,并使用http://172.16.0.2:8001打开应用。

当我尝试在其他地方托管docker,然后尝试使用服务器的公共IP访问应用程序时,会出现问题。使用nginx-proxy,我可以路由到theia容器,但不确定如何路由到在theia容器中运行的应用程序。

我还尝试使用以下方法公开另一个端口:

docker run -d --name theia --expose 3000 --expose 8001 --net nginx-proxy -e VIRTUAL_HOST=theia.example.com theia:theia1

并将外部端口映射到内部端口:

docker run -d --name theia --expose 3000 -p 8001:8001 --net nginx-proxy -e VIRTUAL_HOST=theia.example.com theia:theia1

上述两种情况均会导致URL http://theia.example.com出现 502错误的网关错误。

以下是我使用的其他代码和命令:

快捷代码(app.js)

const express = require('express')
const app = express()
const port = 8001

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

使用npm install express安装Express并运行应用程序node app.js之后,以下是控制台上的输出:

theia@3a9d843bf70e:/home/project$ node app.js
Example app listening on port 8001!

Dockerfile

FROM ubuntu:18.04


RUN apt-get update && apt-get -y install curl xz-utils wget gpg


RUN curl -sL https://deb.nodesource.com/setup_8.x | bash 
RUN apt-get install --yes nodejs

#check for more node installation


RUN apt-get update && apt-get install -y python build-essential

RUN npm install -g yarn

RUN apt-get -y install git sudo

RUN adduser --disabled-password --gecos '' theia && \
    adduser theia sudo && \
    echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers;

RUN chmod g+rw /home && \
    mkdir -p /home/project && \
    chown -R theia:theia /home/project;

USER theia

WORKDIR /home/theia

ADD next.package.json ./package.json

RUN yarn --cache-folder ./ycache && rm -rf ./ycache

RUN  yarn theia build

EXPOSE 3000

ENV SHELL /bin/bash

ENTRYPOINT [ "yarn", "theia", "start", "/home/project", "--hostname=0.0.0.0" ]

next.package.json

{
  "private": true,
  "dependencies": {
    "typescript": "latest",
    "@theia/typescript": "next",
    "@theia/navigator": "next",
    "@theia/terminal": "next",
    "@theia/outline-view": "next",
    "@theia/preferences": "next",
    "@theia/messages": "next",
    "@theia/git": "next",
    "@theia/file-search": "next",
    "@theia/markers": "next",
    "@theia/preview": "next",
    "@theia/callhierarchy": "next",
    "@theia/merge-conflicts": "next",
    "@theia/search-in-workspace": "next",
    "@theia/json": "next",
    "@theia/textmate-grammars": "next",
    "@theia/mini-browser": "next"
  },
  "devDependencies": {
    "@theia/cli": "next"
  }
}

构建图像

docker build --tag "theia:theia1" .

1 个答案:

答案 0 :(得分:0)

您应该使用-p选项绑定端口。因此,事情就是这样,容器的端口8001应该已经在工作。您应该告诉您的机器请求容器8001端口。

测试您的nodejs

docker exec -it theiaContainerName sh,然后运行curl localhost:8001以确保容器正在该端口中侦听。

接下来,在docker run期间通过-p:8001将8001容器端口绑定到您的机器端口

docker run -d --name theia -p 800:8001 --expose 3000 --net nginx-proxy -e VIRTUAL_HOST=theia.example.com theia:theia1

理想情况下,这应该可行。