docker-compose for nginx and nodejs server
11,252
Solution 1
You use the container's name
as you defined it in docker-compose.yml
. Docker provides the IP addresses for the named containers via DNS within each container, and updates them if the containers are updated.
In this case, you called it server
, so that is what you would use.
proxy_pass http://server:8000;
Documentation: Networking in Compose
Solution 2
In this case, you need to use upstream like:
upstream backend {
server express-server:81; #you container/service name
}
and then use:
proxy_pass http://backend
Related videos on Youtube
Author by
dagda1
Updated on September 18, 2022Comments
-
dagda1 over 1 year
I have the following
docker-compose.yml
file:version: '3' services: server: build: context: ../../ dockerfile: ./packages/website/Dockerfile command: node /cutting/index.js environment: PORT: 8000 NODE_ENV: production restart: always nginx: build: context: ./ dockerfile: ./nginx/Dockerfile command: tail -f /dev/null depends_on: - server ports: - "8000:8000" restart: always
And the following
nginx.conf
:worker_processes 1; user nobody nogroup; pid /tmp/nginx.pid; error_log /tmp/logs/nginx.error.log; events { worker_connections 1024; accept_mutex off; } http { include mime.types; default_type application/octet-stream; access_log /tmp/logs/nginx.access.log combined; sendfile on; server { proxy_http_version 1.1; # this is essential for chunked responses to work listen 80; ## listen for ipv4; this line is default and implied listen [::]:80 default ipv6only=on; ## listen for ipv6 client_max_body_size 4G; server_name frontend; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types application/javascript text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; keepalive_timeout 5; location /static/ { alias /static/; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; # UNCOMMENT LINE BELOW IF THIS IS BEHIND A SSL PROXY #proxy_set_header X-Forwarded-Proto https; proxy_redirect off; proxy_pass http://localhost:8000; } } }
If this creates 2 containers how can I get nginx to see the localhost:8000 on the other container?
-
scniro almost 6 yearsMy man. I spent all day contemplating hulk smashing my laptop until your answer calmed me down off the ledge. This deserves 10x upvotes. I owe you a beer.