简体   繁体   English

Docker - nginx 代理 - 访问容器之间的主机

[英]Docker - nginx proxy - access hosts between containers

I have web application.我有网络应用程序。

  1. Public web app (app1)公共网络应用程序 (app1)
  2. api web app (app2) api 网络应用程序 (app2)

I make docker configuration for this apps.我为这个应用程序做了 docker 配置。 Each application in its container.每个应用程序都在其容器中。 To access this applications from web, configured container with nginx, where nginx proxy all requests.要从 Web 访问此应用程序,请使用 nginx 配置容器,其中 nginx 代理所有请求。
So i can run - http://app1.dev/ and http://app2.dev/所以我可以运行 - http://app1.dev/http://app2.dev/

But i need have access from app1 to http://app2.dev/ (access hosts app2.dev from app1 container).但我需要从 app1 访问http://app2.dev/ (从 app1 容器访问主机 app2.dev)。

Ping (from app1 container): Ping(来自 app1 容器):

PING app2.dev (127.0.53.53) 56(84) bytes of data.
64 bytes from 127.0.53.53: icmp_seq=1 ttl=64 time=0.027 ms
64 bytes from 127.0.53.53: icmp_seq=2 ttl=64 time=0.038 ms
64 bytes from 127.0.53.53: icmp_seq=3 ttl=64 time=0.038 ms

What i should configure else, to have access to http://app2.dev/ host from app1 container?我还应该配置什么,才能从 app1 容器访问http://app2.dev/主机?

Nginx proxy config Nginx 代理配置

upstream app1_upstream {
    server app1;
}
upstream app1_upstream {
    server app2;
}
server {
    listen 80;

    server_name app1.dev
                app2.dev;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        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-Proto $scheme;
        proxy_cache_bypass $http_upgrade;

        if ($host = "app1.dev") {
            proxy_pass http://app1;
        }

        if ($host = "app2.dev") {
            proxy_pass http://app2;
        }
    }

    error_log  /var/log/nginx/proxy_error.log;
    access_log /var/log/nginx/proxy_access.log;
}

Docker compose Docker 组合

version: '2'
services:
    proxy:
        build: ./proxy/
        ports:
            - "80:80"
            - "443:443"
        links:
            - app1
            - app2
            - app1:app1
            - app2:app2
        hostname: proxy

    app1:
        build: ./app1/
        volumes:
            - ../app1/:/var/www/app1
        hostname: app1

    app2:
        build: ./app2/
        volumes:
            - ../app2/:/var/www/app2
        hostname: app2

docker-compose ps

app1      /sbin/my_init  Up      80/tcp                                   
app2     /sbin/my_init  Up      80/tcp                                   
proxy_1   /sbin/my_init  Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

Not sure what version of docker you running, but if you are (or are able to) run 1.10 you should use a docker network instead of using "link".不确定您运行的是哪个版本的 docker,但如果您(或能够)运行 1.10,您应该使用 docker 网络而不是使用“链接”。

If you run all three containers on the same docker network then they will have access to one another through their container name.如果您在同一个 docker 网络上运行所有三个容器,那么它们将可以通过容器名称相互访问。

That will allow you to make the call from app1 to app2 without going back through your proxy (although I would call that an anti-pattern as if you were to change the interface to app2 you would have to update app1 and the proxy, I would have app1 call app2 through your proxy so you maintain one interface).这将允许您在不通过代理返回的情况下从 app1 调用 app2(尽管我将其称为反模式,就好像您要将接口更改为 app2 一样,您必须更新 app1 和代理,我会让 app1 通过您的代理调用 app2,以便您维护一个接口)。

For more info on Docker networks: https://docs.docker.com/engine/userguide/networking/dockernetworks/有关 Docker 网络的更多信息: https : //docs.docker.com/engine/userguide/networking/dockernetworks/

TLDR:域名注册地址:

# create bridge network (for single host)
docker networks create my-network

then change your compose too:然后也更改您的撰写:

version: '2'
services:
    proxy:
        build: ./proxy/
        ports:
            - "80:80"
            - "443:443"
        networks:
            - my-network
        hostname: proxy

    app1:
        build: ./app1/
        volumes:
            - ../app1/:/var/www/app1
        networks:
            - my-network
        hostname: app1

    app2:
        build: ./app2/
        volumes:
            - ../app2/:/var/www/app2
        networks:
            - my-network
        hostname: app2

networks:
   my-network:
    external: true
ports:
    - "80:80"
    - "443:443"

exposes ports to the host machine.将端口暴露给主机。 When you do当你做

docker ps -a

you will see these ports listed您将看到列出的这些端口

However, to expose ports between containers you need to use the EXPOSE command in your dockerfile.但是,要在容器之间公开端口,您需要在 dockerfile 中使用 EXPOSE 命令。

https://docs.docker.com/engine/reference/builder/#expose https://docs.docker.com/engine/reference/builder/#expose

What i should configure else, to have access to http://app2.dev/ host from app1 container?

You must EXPOSE ports in dockerfile!您必须在 dockerfile 中公开端口!

also if you do a ...另外如果你做...

docker exec -it containerName bash

you will be able to explore.你将能够探索。

View the hosts file inside the container.查看容器内的hosts文件。

cat /etc/hosts

you will see an entry for the other container in the hosts file if you have --link the containers correctly.如果您正确地 --link 容器,您将在 hosts 文件中看到另一个容器的条目。

you can ping using the domain name in the hosts file.您可以使用主机文件中的域名进行 ping 操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM