wmnnd / nginx-certbot

Boilerplate configuration for nginx and certbot with docker-compose
MIT License
3.16k stars 1.17k forks source link

[Question] How do you refer to other docker containers? #58

Open elyran opened 4 years ago

elyran commented 4 years ago

The host runs nginx-certbot. It also runs a Wordpress docker.

In the data/nginx/app.conf, on the proxy_pass directive, what host/port do you recommend to use? How do you forward the request to another dockerized service? Obviously something like http://127.0.0.1:12345 doesn't work, as these two on different (docker) networks.

How do you solve this?

elyran commented 4 years ago

So far solved this by using docker's IP (172.17.0.1), but this is not a clean solution.

Anyway, this lead to the next step - Wordpress. Although another subject, but worth mentioning: At this point I had to enable the following header on the HTTPS config part of NGINX, and I recommend you add it to the boilerplate: proxy_set_header X-Forwarded-Proto $scheme;

The above enables SSL on WP. Otherwise WP generates HTTP (non SSL) URLs which breaks the pages.


Still looking for a clean solution to the original question.

sa2812 commented 4 years ago

If you have a docker-compose for both of these docker images together, then you should be able to refer to the others by their service names, like nginx-certbot://, for example.

There's more information in the Docker documentation here

elyran commented 4 years ago

The proxy'ing is done in data/nginx/app.conf.

sa2812 commented 4 years ago

Here's a snippet of my code to achieve what you're looking for.

app.conf

upstream web {
    server web:8000;
}

server {
    listen 443 ssl;
   server_name yourdomain.com;

    ...

    location / {
        proxy_pass http://web;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}

Note where the upstream web is defined.

docker-compose.yml

services:
  nginx:
    image: nginx:1.15-alpine
    # rest of nginx service config
    depends_on:
      - web # This will make the script start your web service before nginx
  web:
    # configure your wordpress service here (or point to its dockerfile)

As you can see in my app.conf, you can directly refer to your wordpress service inside nginx with http://web without the need to manually figure out the IP address for the service.

This only works if you're docker-composeing them together, which might be a sensible option for you

elyran commented 4 years ago

@sa2812 I appreciate your effort. Really, thanks. However, what I'm looking for is achieving the above with two separate docker-compose.yml files. Why? Separation of concerns. On NGINX, one DB, and multiple websites.

localsnet commented 4 years ago

@elyran If it helps you. I use separate compose file for proxy and separate compose file for service, but they both use one network (you should create network previously, if compose files in different directories) that I specified in both files:

web_app:
  image:web_app
  restart: always
  networks:
    - my_network

networks:
  my_network:
    external: true 
elyran commented 4 years ago

@localsnet and how do you refer to the service through NGINX's config file? Same as @sa2812 said? Just the service name?

localsnet commented 4 years ago

@elyran, yes .

ср, 29 янв. 2020, 21:41 elyran notifications@github.com:

@localsnet https://github.com/localsnet and how do you refer to the service through NGINX's config file? Same as @sa2812 https://github.com/sa2812 said? Just the service name?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/wmnnd/nginx-certbot/issues/58?email_source=notifications&email_token=AG5MAG4ZDLG2MKQQGIYHCFTRAHLXLA5CNFSM4KI3HAMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKIPQ4A#issuecomment-579926128, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG5MAGYVSIWPFTKRQRWFNMLRAHLXLANCNFSM4KI3HAMA .

felipepaes commented 11 months ago

@localsnet and how do you refer to the service through NGINX's config file? Same as @sa2812 said? Just the service name?

Sorry, I know this an old one but I would like to let this info here for others that may run iinto this:

Both container should use the same network so that nginx.conf may resolve hostnames to container gateways

# service 1
hostname: potato
networks:
  - my_app_nework

# service 2
hostname: apple
networks:
  - my_app_network

networks:
  my_app_network:

if you use potato:8000 in nginx.conf inside apple service, potato will resolve to service 1 gateway, so no need to hard set the container's ip