-1

我在 nginx-proxy 服务器容器 ( 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

Theia 容器可通过http://theia.example.com公开访问。

这显然不起作用: http ://theia.example.com:8001

我已经尝试使用图像实现https://github.com/jwilder/nginx-proxy/pull/259mashupmill/nginx-proxy以及ncadou/nginx-proxy

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 Bad Gateway错误。

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

快递代码(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" .
4

1 回答 1

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

理想情况下,这应该有效。

于 2019-04-09T19:53:29.950 回答