Docker 容器之间互相访问

1、创建 docker 网桥

docker network create hr-network

2、查看

[root@kvm81 frontend]# docker network list
NETWORK ID          NAME                DRIVER              SCOPE
a5f0dd433c6b        bridge              bridge              local
ff5e05f84fa0        host                host                local
9ff079df0e65        hr-network          bridge              local
0afffbfb091d        none                null                local

3、后端容器 添加到指定的 hr-network 网络

docker run --name hr-backend \
--network hr-network --network-alias hr-backend \
-p 8998:8998 -d hr-backend

---network 网桥名

--network-alias 网络别名 

4、前端容器 添加到指定的 hr-network 网络

docker run --name hr-frontend \
--network hr-network --network-alias hr-frontend \
-p 7879:7879 -d hr-frontend

5、容器之间访问方法

<网络别名>:<服务端口号>  

下面 前端 nginx 转发  java 后端接口 

upstream r6-hrtest-api {
        server                              hr-backend:8998;
}

server {

        listen          7879;

        location /hr/api {
                proxy_pass                   http://r6-hrtest-api;
                proxy_set_header             Host             $host;
                proxy_set_header             X-Real-IP        $remote_addr;
                proxy_set_header             X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

        location /swagger/ {
                proxy_pass                   http://r6-hrtest-api/hr/;
                proxy_set_header             Host             $host:$server_port;
                proxy_set_header             X-Real-IP        $remote_addr;
                proxy_set_header             X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

        location / {
                root                         /opt/hrtest/frontend;
        }
}

 

 

 

 

你可能感兴趣的:(Docker)