node.js - Node http-proxy in Docker container -


i have following code works great in local environment. however, when try run same code docker container (via boot2docker), cannot https://[boot2docker_ip]:4000

i tried updating target value in code below these options none of them seemed trick:

target: 'http://localhost:3000',
target: 'http://0.0.0.0:3000',
target: 'http://127.0.0.1:3000',
target: 'http://<boot2docker_ip>:3000',

var fs = require('fs');      require('http-proxy').createproxyserver({   ssl: {     key: fs.readfilesync(__dirname + '/certs/ssl.key', 'utf8'),     cert: fs.readfilesync(__dirname + '/certs/ssl.crt', 'utf8')   },   target: 'http://localhost:3000',   ws: true,   xfwd: true }).listen(4000); 

i using node-http-proxy package https://github.com/nodejitsu/node-http-proxy

edit

here git repo try out behavior; have checked in fake ssl simplicity.

dockerfile:

from readytalk/nodejs  add ./src /app add ./ssl-proxy /proxy  copy ./run.sh /run.sh run chmod +x /run.sh  expose 3000 expose 4000  entrypoint ["/run.sh"] 

run.sh:

#!/bin/sh  /nodejs/bin/node /app/main.js; /nodejs/bin/node /proxy/main.js 

i had @ dockerfile , run.sh script use. line run.sh script:

/nodejs/bin/node /app/main.js; /nodejs/bin/node /proxy/main.js 

what's important know here each of these commands start long-running server process (theoretically) runs forever. means second process (/proxy/main.js) never start because shell wait first process finish.

this means you cannot access proxy server because never starts.

basically there 2 solutions think of. please note idiomatic "docker way" run one process per container only, though.

  1. i'd recommend running application , proxy server in two separate containers. can link 2 containers together:

    docker run --name app -p 3000 <your-image> /nodejs/bin/node /app/main.js docker run --name proxy -l app:app -p 4000:4000 <your-image> /nodejs/bin/node /proxy/main.js 

    the flag -l app:app cause app container available hostname app in proxy container (this done creating /etc/hosts entry in container). means, inside proxy container, can use http://app:3000 access upstream application port.

  2. an alternative solution use process manager tool supervisord manage several long-running processes in container in parallel. there's good article on in documentation. boils down following:

    1. install supervisord (apt-get install supervisor in ubuntu)
    2. create configuration file (typically in /etc/supervisor/conf.d/yourapplication.conf) in configure services need run:

      [supervisord] nodaemon=true  [program:application] command=/nodejs/bin/node /app/main.js  [program:proxy] command=/nodejs/bin/node /proxy/main.js 
    3. then use supervisord start command, example using cmd ["/usr/bin/supervisord"] in dockerfile.

    in case, both processes running in same container , can use http://localhost:3000 access upstream application.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -