Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

How to access an app running on a theia docker container behind nginx-proxy?

I am running a theia docker container behind a nginx-proxy server container (jwilder/nginx-proxy).

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?

Code used to run nginx-proxy on docker

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

Code used to run theia on docker

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

The theia container is accessible publicly using http://theia.example.com.

This obviously does not work: http://theia.example.com:8001

I have tried implementing https://github.com/jwilder/nginx-proxy/pull/259 using the image mashupmill/nginx-proxy as well as ncadou/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

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.

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.

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

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:

Express code (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}!`))

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"
  }
}

Building the image

docker build --tag "theia:theia1" .
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

To test your nodejs

docker exec -it theiaContainerName sh and then run curl localhost:8001 to make sure container is listening in that port.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...