docker - how to reach another container from a dockerised nginx -
i have nginx in docker container, , nodejs webapp in docker container. nodejs server reachable host server on port 8080.
the nginx docker container listening port 80 (will certificate later, first base must working).
and want subdomain forwarded 8080 nodejs app. lets app1.example.com
from outside can reach app te server ip (or hostname) , port 8080 not on app1.example.com. , work on app1.example.com:8080 (i have opened port 8080 on host server).
i bad gateway nginx message when approaching app1.example.com in first nginx container, how host server proxy pass port 8080 of host server (and not port 8080 of nginx container). looking reverse expose syntax.
the main problem is, of course if use ip , port 127.0.0.1:8080 try on nginx container.... how let nginx container route host 127.0.0.1:8080?
i have tried 0.0.0.0 , defining upstream, been googling lot, , have tried lot of configurations... not yet found working one....
edit found out, command of docker might help:
sudo docker network inspect bridge
this shows ip address used inside cotainers (in case 172.17..0.2), not sure address stays same every time docker restart... (e.g. server reboot)
edit followning alkaline answer have (but still not working):
my docker-compose.yml file:
version: "2" services: nginx: container_name: nginx image: nginx_img build: ../docker-nginx-1/ ports: - "80:80" networks: - backbone nodejs: container_name: nodejs image: merites/docker-simple-node-server build: ../docker-simple-node-server/ networks: - backbone expose: - 8080 networks: backbone: driver: bridge
and nginx (skipped incluse in conf.d folder simplicity):
worker_processes 1;
events { worker_connections 1024; }
http { sendfile on; upstream upsrv { server nodejs:8080; } server { listen 80; server_name app1.example.com; location / { proxy_pass http://upsrv; } } }
edit 31-08-2016
this migth problem, name not backbone, called after folder started srevice from:
sudo docker network ls
out puts:
network id name driver scope 1167c2b0ec31 bridge bridge local d06ffaf26fe2 dockerservices1_backbone bridge local 5e4ec13d790a host host local 7d1f8c32f259 none null local
edit 01-09-2016
it might caused way have nginx docker container setup?
this docker file used:
############################################################ # dockerfile build nginx installed containers # based on ubuntu ############################################################ # set base image ubuntu ubuntu # file author / maintainer maintainer maintaner name # install nginx # add application repository url default sources # run echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list # update repository run apt-get update # install necessary tools run apt-get install -y nano wget dialog net-tools # download , install nginx run apt-get install -y nginx # remove default nginx configuration file run rm -v /etc/nginx/nginx.conf # copy configuration file current directory add nginx.conf /etc/nginx/ # append "daemon off;" beginning of configuration run echo "daemon off;" >> /etc/nginx/nginx.conf # expose ports expose 80 # set default command execute # when creating new container cmd service nginx start
my final solution 1th sept. 2016
i used compose file now:
version: "2" services: nginx: image: nginx container_name: nginx volumes: - ./nginx-configs:/etc/nginx/conf.d ports: - "80:80" networks: - backbone nodejs: container_name: nodejs image: merites/docker-simple-node-server build: ../docker-simple-node-server/ networks: - backbone expose: - 8080 networks: backbone: driver: bridge
in project folder, wich run docker-compose -d, added folder named nginx-configs. folder 'override' files in nginx container named /etc/nginx/conf.d
therefor copied default.cfg nginx container before added volume mount. using command:
docker exec -t -i container_name /bin/bash
and cat /etc/nginx/conf.d/default.conf
and added same default.conf in project folder nginx configs.
besides default added app1.conf content:
upstream upsrv1 { server nodejs:8080; } server { listen 80; server_name app1.example.com; location / { proxy_pass http://upsrv1; } }
this way, can add second app... third , on.
so basics working now...
here's best practice. expose port 80 outside of host. nodejs app can in private network accessible through nginx.
version: "2" services: nginx: ... ports: - "80:80" networks: - backbone nodejs: ... networks: - backbone expose: - 8080 networks: backbone: driver: bridge
in nginx.conf file, upstream servers can listed nodejs:8080
. docker daemon resolve correct internal ip.
Comments
Post a Comment