是否可以使用Http

时间:2018-05-11 17:48:31

标签: docker

我正在尝试使用Docker来帮助部署应用程序。这个想法是有两个容器。一个是包含Nginx和Angular应用程序的前端。

FROM nginx
COPY ./dist/ /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/nginx.conf

应该联系使用gradle-docker pluginSpring推荐的Dockerfile生成的基于Spring Boot的API:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

他们似乎单独运行(我可以在我的开发机器上访问它们);但是,我在连接两者时遇到了麻烦。

我的docker-compose.yml文件:

version: '3'
services:
  webapp:
    image: com.midamcorp/emp_front:latest
    ports: 
      - 80:80
      - 443:443
    networks:
      - app
  api:
    image: com.midamcorp/employee_search:latest
    ports: 
      - 8080:8080
    networks: 
      - app
networks: 
  app:

基于我对网络上Docker文档的理解,我的印象是容器将被放置在同一网络中,因此可以与服务名称(例如,api)作为&#34进行交互。宿主&#34 ;.基于这个假设,我试图通过以下方式从Angular应用程序访问API:

private ENDPOINT_BASE: string = "http://api:8080/employee";

这会返回错误:Http failure response for (unknown url): 0 Unknown Error

老实说,我看过的样本使用了这个概念(用服务名替换主机来连接两个容器)用于数据库应用而不是HTTP。我试图完成的是不可能的?

编辑:

我的nginx.conf

worker_processes  1;


events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
      listen 80;
        server_name  localhost;
   root /usr/share/nginx/html;
         location / {       
             index  index.html index.htm;
           try_files $uri $uri/ /index.html;
        }         

    }

}

编辑:

更新了nginx.conf

worker_processes  1;


events {
    worker_connections  1024;
}

http {

      upstream docker-java {
        server api:8080;
    }

    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;


       server {
        listen 8081;
         server_name  localhost;
        location / {
            proxy_pass         http://docker-java;
            proxy_redirect     off;
            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;
        }


}

    server {
      listen 80;
        server_name  localhost;
   root /usr/share/nginx/html;
         location / {       
             index  index.html index.htm;
           try_files $uri $uri/ /index.html;
        }         

    }

}

和docker-compose.yml

    version: '3'
services:
  webapp:
    image: com.midamcorp/emp_front:latest
    ports: 
      - 80:80
      - 443:443
    networks:
      - app
    depends_on:
      - api
  api:
    image: com.midamcorp/employee_search:latest
    networks: 
      - app
networks: 
  app:

客户端/ Angular应用使用以下内容联系API private ENDPOINT_BASE: string = "http://localhost:8081/employee";

docker ps的输出

> docker ps
CONTAINER ID        IMAGE                                  COMMAND                  CREATED             STATUS              PORTS                                      NAMES
947eb757eb4b        b28217437313                           "nginx -g 'daemon of…"   10 minutes ago      Up 10 minutes       0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   employee_service_webapp_1
e16904db67f3        com.midamcorp/employee_search:latest   "java -Djava.securit…"   10 minutes ago      Up 10 minutes                                                  employee_service_api_1

1 个答案:

答案 0 :(得分:1)

您遇到的问题并不奇怪。只是你没有明确地命名您的容器。相反,Docker自己生成了一个名称。因此,nginx 解析employee_service_api_1,但不会仅识别api。打开webapp容器并查看您的主机(cat /etc/hosts) - 它会显示employee_service_api_1及其IP地址。

如何修复它。

container_name添加到docker-compose.yml

version: '3'
services:
  webapp:
    image: com.midamcorp/emp_front:latest
    container_name: employee_webapp
    ports: 
      - 80:80
      - 443:443
    networks:
      - app
    depends_on:
      - api
  api:
    image: com.midamcorp/employee_search:latest
    container_name: employee_api
    networks: 
      - app
networks: 
  app:

我总是不使用“简单”名称(即只是api),因为在我的系统中,可能会显示多个名称相似的容器,所以我添加了一些前缀。在这种情况下,我将api容器命名为employee_api,现在重启容器后,nginx将解析为该名称。