0

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" .
abhi
  • 11
  • 2

1 Answers1

0

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.

theBuzzyCoder
  • 2,652
  • 2
  • 31
  • 26
  • The app is listening on the port. Doing `curl localhost:8001` does give the output, `Hello World!` in my case. But as I have explained, I tried mapping the external port to the internal port using `docker run -d --name theia --expose 3000 -p 8001:8001 --net nginx-proxy -e VIRTUAL_HOST=theia.example.com theia:theia1` and it gave *502 Bad Gateway* error for the URL http://theia.example.com – abhi Apr 09 '19 at 20:15
  • How are making sure whether the request is coming to your local machine and since nginx is a proxy I think you need a server directive in nginx conf that will accept request in nginx proxy’s 8001 port – theBuzzyCoder Apr 09 '19 at 20:18
  • The request is coming to the container after nginx routes it. The container is accessible at http://theia.example.com and visiting this URL opens up the theia IDE running on port 3000. This is because I have used `--expose 3000` in the `docker run` command. The Express app is also running on the same container at port 8001, which is not accessible. Adding `--expose 8001` to `docker run` doesn't make it work and also gives **502 Bad Gateway** on opening theia.example.com – abhi Apr 10 '19 at 06:17