25932

Docker: proxy_pass to another container - nginx: host not found in upstream

Question:

<em>(I know others have asked this question before, but I'm not able to solve the problem using the solutions proposed in other posts, so i figured i would try to post my configuration files and see if someone could help.)</em>

I want to create a container for nginx, and use proxy_pass to pass requests to the container with the running web application. I can't figure out how to communicate between the two containers. When i try to run docker stack deploy -c docker-compose.yml somename, only the web container starts. The nginx container fails to start, and is stuck in a loop of trying to restart. This is the log messages I get:

<blockquote>

2017/08/16 14:56:10 [emerg] 1#1: host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2 nginx: [emerg] host not found in upstream "web:8000" in /etc/nginx/conf.d/nginx.conf:2

</blockquote>

I found an answer that as long as you use the same name as under services in the docker-compose.yml file, nginx would find the variable. However that doesn't seem to help in my case.

How does communication like this between different containers work? Where is the 'web' variable

Most examples I've seen use version: "2" in the docker-compose.yml file, should this make a difference?

<strong>My docker-compose.yml:</strong>

version: "3" services: web: image: user/repo:web deploy: resources: limits: cpus: "0.1" memory: 50M restart_policy: condition: on-failure ports: - "8000:80" networks: - webnet nginx: image: user/repo:nginx ports: - 80:80 links: - web:web depends_on: - web networks: webnet:

<strong>Nginx config:</strong>

upstream docker-web { server web:8000; } server { listen 80; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { proxy_pass http://docker-web; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; } }

Answer1:

I figured out how to fix the problem. Got some help to fix the docker-compose.yml, so it looks like this:

<strong>docker-compose-yml:</strong>

version: "3" services: web: image: user/repo:web deploy: resources: limits: cpus: "0.1" memory: 50M restart_policy: condition: on-failure ports: - "8000:80" networks: main: aliases: - web nginx: image: user/repo:nginx ports: - 80:80 links: - web:web depends_on: - web networks: main: aliases: - nginx networks: main:

After this the nginx container actually ran, but it was still not capable of connecting to the web-container. Found out I was able to use both curl web and curl web -p 8000 to get the page from web, from inside the nginx container. Then I changed the upstream in my nginx.conf from this

upstream docker-web { server web:8000; }

to this:

upstream docker-web { server web; }

Answer2:

Well you could achieve that by editing your <strong>/etc/hosts</strong> file and add the proper entry for your web host

1.1.1.1 web

Obviouslly you'll have to replace 1.1.1.1 with the web server's real ip address (a private one I suppose). Save the file and try again. Another workaround would be to replace in the config:

upstream docker-web { server web:8000; }

with

upstream docker-web { server web-ip-address:8000; }

If you'll go for the 2nd, don't forget to restart/reload nginx!

Recommend

  • Using ExecutorService with a multithreaded version of Merge Sort
  • java.lang.IllegalArgumentException due to Spannable TextView
  • How do I avoid to call Application.CreateForm twice?
  • how to add part of Face A to Part of face B, most importantly matching color tones
  • Sizes calculation with JavaScript and jQuery
  • Is it possible to serialize string to json with single slashes?
  • How to add database query results to an array
  • What is are the differences between ConfigureServices and Configure in ASP .NET Core?
  • Can you review my Perl rewrite of Cucumber?
  • Simple integer comparison in HBase
  • Counting number of digits in a double C++
  • GC.start has no effect in Pry, but does in IRB
  • I keep getting this error, XML Parsing error: syntax error but still the website runs fine
  • Manifest marge error after migrating to androidX
  • Using downloaded font offline [duplicate]
  • Rxjs Observable Lifecycle
  • Variable amount of columns returned in mysqli prepared statement
  • Search for text in a string, copy & paste rows to new sheet
  • how java graphics repaint method actually works
  • Moving Circle on Live Wallpaper
  • Xamarin.Forms: How To Populate A Pie Chart From Web API Data?
  • handlers inside a component invoking eachothers code by mistake
  • PySpark: Get first Non-null value of each column in dataframe
  • ROR + MVC Disable Browser Cache
  • How to get WinForms custom control's default value to be respected when first dropped on a form
  • Jekyll - How do I create pages in the root directory?
  • Dynamically change JavaFX css property
  • android : speech recognition what are the technologies available
  • Splitting ReportLab table across PDF page (side by side)?
  • Java Collections.shuffle() weird behaviour [closed]
  • Typeahead.js does give me suggestions but doesn't select them
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)
  • Drag and drop unicode TText in DelphiXe4
  • Send array to next viewcontroller iOs xcode [duplicate]
  • What is the best way to cache and reuse immutable singleton objects in Java?
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}