我在nginx代理服务器容器(jwilder/nginx-proxy)后面运行一个theia码头容器。
在theia内部,我在8001端口上运行一个简单的ExpressJS应用程序。
我可以使用子域公开访问容器。
如何公开访问在容器内运行的应用程序?
代码用于在docker上运行nginx代理
docker run -d -p 80:80 --name nginx-proxy --net nginx-proxy -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy代码用于在码头上运行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
我尝试过使用映像https://github.com/jwilder/nginx-proxy/pull/259和ncadou/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代理,我可以路由到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提供了一个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" .发布于 2019-04-09 19:53:29
您应该使用-p选项绑定端口。所以,事情是这样的,集装箱的8001港口应该已经开始运作了。你应该告诉你的机器请求集装箱8001端口。
测试nodejs
docker exec -it theiaContainerName sh,然后运行curl localhost:8001以确保容器正在侦听该端口。
接下来,在docker运行期间,通过-p :8001将8001容器端口绑定到机器端口。
docker run -d --name theia -p 800:8001 --expose 3000 --net nginx-proxy -e VIRTUAL_HOST=theia.example.com theia:theia1理想情况下,这应该是可行的。
https://stackoverflow.com/questions/55599751
复制相似问题