问题描述
我正在nginx代理服务器容器(jwilder/nginx-proxy
)后面运行一个theia docker容器.
I am running a theia docker container behind a nginx-proxy server container (jwilder/nginx-proxy
).
在theia内部,我正在端口号上运行一个简单的ExpressJS应用程序. 8001.
Inside theia, I am running a simple ExpressJS app on port no. 8001.
我可以使用子域公开访问容器.
I am able to access the container publicly using a sub-domain.
我如何公开访问在容器内运行的应用程序?
How do I access the app running inside the container publicly?
用于在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容器.
The theia container is accessible publicly using http://theia.example.com.
这显然行不通: http://theia.example.com:8001
我尝试实现 https://github.com/jwilder/nginx-proxy /pull/259 使用图像mashupmill/nginx-proxy
和ncadou/nginx-proxy
I have tried implementing https://github.com/jwilder/nginx-proxy/pull/259 using the image mashupmill/nginx-proxy
as well as ncadou/nginx-proxy
用mashupmill/nginx-proxy
替换运行jwilder/nginx-proxy
的容器后,我运行了:
After replacing the container running jwilder/nginx-proxy
with mashupmill/nginx-proxy
, I ran:
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 .
I am not sure if I am misunderstood what mashupmill/nginx-proxy
does or if I am doing something wrong.Ideally the above should have opened theia at http://theia.example.com and the Express app at http://app.example.com.
在本地运行docker时,访问在theia容器中运行的应用程序不是问题.我可以获取theia容器的本地IP地址,并使用 http://172.16.0.2:3000 打开theia以及具有 http://172.16.0.2:8001 的应用.
Accessing the app running inside the theia container is not a problem when running docker locally. I can get the local IP address of the theia container and open theia with http://172.16.0.2:3000 and the app with http://172.16.0.2:8001.
当我尝试在其他地方托管docker,然后尝试使用服务器的公共IP访问应用程序时,会出现问题.使用nginx-proxy,我可以路由到theia容器,但是不确定如何路由到theia容器中运行的应用程序.
The problem arises when I am trying to host docker elsewhere and then trying to access the app using the public IP of the server. Using nginx-proxy, I am able to route to the theia container but am not sure how to route to the app running inside the theia container as well.
我还尝试使用以下方法公开另一个端口:
I have also tried exposing another port using:
docker run -d --name theia --expose 3000 --expose 8001 --net nginx-proxy -e VIRTUAL_HOST=theia.example.com theia:theia1
并将外部端口映射到内部端口:
and mapping the external port to the internal port:
docker run -d --name theia --expose 3000 -p 8001:8001 --net nginx-proxy -e VIRTUAL_HOST=theia.example.com theia:theia1
以上两种情况均给出URL http://theia的 502错误网关错误.example.com .
Both of the above give a 502 Bad Gateway error for the URL http://theia.example.com.
以下是我使用的其他代码和命令:
Following are the other codes and commands I used:
快捷代码(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
之后,控制台上的输出如下:
After installing Express using npm install express
and running the app node app.js
, following is the output on the console:
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" .
推荐答案
您应使用-p选项绑定端口.因此,事情就是这样,容器的端口8001应该已经在工作.您应该告诉您的机器请求容器8001端口.
You should use -p option to bind the port. So, here's the thing, the container's port 8001 should be already working. You should tell your machine to request containers 8001 port.
要测试您的nodejs
To test your nodejs
docker exec -it theiaContainerName sh
,然后运行curl localhost:8001
以确保容器正在该端口中侦听.
docker exec -it theiaContainerName sh
and then run curl localhost:8001
to make sure container is listening in that port.
接下来,在docker run期间通过-p:8001将8001容器端口绑定到您的机器端口
Next, bind the 8001 containers port to your machines port by -p :8001 during docker run
docker run -d --name theia -p 800:8001 --expose 3000 --net nginx-proxy -e VIRTUAL_HOST=theia.example.com theia:theia1
理想情况下,这应该可行.
Ideally this should work.
这篇关于如何访问在nginx-proxy后面的theia docker容器上运行的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!