首页 > 解决方案 > 将 docker 镜像部署到 GAE 以获取角度

问题描述

我一直在尝试使用 nginx 作为服务器构建 docker 容器,该容器在本地运行良好,但由于某种原因,我无法将其部署到 Google 的 App 引擎。您能否首先让我知道这是否可能?如果可能的话,请您也指导我。我在 GCP 的容器存储库中也有该图像,但找不到将其部署到 GAE 的方法

这是我的码头工人

  #STEP1
  # we name it as builder so as to use in the following instructions 
  further
 FROM node:8.12.0-alpine as builder

 #Now install angular cli globally
 RUN npm install -g @angular/cli@6.2.1
  #Install git and openssh because alpine image doenst have git and all 
  modules in npm has the dependicies which are all uploaded in git
  #so to use them we need to be able git
 RUN apk add --update git openssh

  #create a new direcotry for the prj and change its directory to it
 RUN mkdir ./adtech-prj

  #copy the package json #dont copy package.lock json now
 COPY package.json package-lock.json ./adtech-prj/

  #this is required to place all our files inside this directory
 WORKDIR ./adtech-prj

 #this copies all files to the working directory
  COPY . .
  # --RUN pwd && ls

  RUN npm cache clear --force && npm i


  RUN $(npm bin)/ng build --prod --aot

  ### STAGE 2: Setup ###

  FROM nginx:1.15-alpine
  RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
  ## Copy our default nginx config
  COPY nginx.conf /etc/nginx/conf.d/

  ## Remove default nginx website
  RUN rm -rf /usr/share/nginx/html/*

  RUN pwd && ls 

  COPY --from=builder /adtech-prj/dist /usr/share/nginx/html



   EXPOSE 80

   CMD ["nginx", "-g", "daemon off;"]

这是我的 nginx 配置 - 一个简单的

 server {


      listen 80 default_server;

      #server_name *.adtechportal.com;
      sendfile on;

      default_type application/octet-stream;



      root /usr/share/nginx/html;


      location / {
        try_files $uri $uri/ /index.html =404;
        #proxy_pass: "http://localhost:8080/AdTechUIContent"
        #uncomment to include naxsi rules
        #include /etc/nginx/naxsi.rules
       }

    }

标签: angulardockergoogle-app-enginenginxdockerfile

解决方案


感谢吉列尔莫。我确实找到了答案。我无法成功部署以前的 docker 映像的原因是我试图公开一个不是 8080 的端口

默认情况下,应用引擎侦听端口 8080,并且它希望 nginx 配置文件使用相同的“侦听”端口。此外,应用引擎默认提供 SSL,因此无需将 nginx 与 SSL 一起使用。

这是 nginx.conf 的修改版本

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Logs will appear on the Google Developer's Console when logged to this
    # directory.
    access_log /var/log/app_engine/app.log;
    error_log /var/log/app_engine/app.log;

    root /usr/share/nginx/html;

    server {
        # Google App Engine expects the runtime to serve HTTP traffic from
        # port 8080.
        listen 8080;

        location / {
            try_files $uri $uri/ /index.html =404;
        }      
    }
}

这是新的 Docker 文件

FROM node:8.12.0-alpine as builder

#Now install angular cli globally
RUN npm install -g @angular/cli

RUN apk add --update git openssh

#create a new direcotry for the prj and change its directory to it
RUN mkdir ./test

#copy the package json #dont copy package.lock json now
COPY package.json package-lock.json ./adtech-prj/

#this is required to place all our files inside this directory
WORKDIR ./test

#this copies all files to the working directory
COPY . .

RUN ng set -g warnings.versionMismatch=false
RUN npm cache clear --force && npm i


#Build the angular app in production mode and store the artifacts in dist 
folder
RUN $(npm bin)/ng build --prod

### STAGE 2: Setup ###

FROM nginx:1.15-alpine
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*

COPY nginx.conf /etc/nginx/conf.d/


# create log dir configured in nginx.conf
RUN mkdir -p /var/log/app_engine

RUN mkdir -p /usr/share/nginx/_ah && \
echo "healthy" > /usr/share/nginx/_ah/health

## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*


COPY --from=builder /adtech-prj/dist /usr/share/nginx/html


RUN chmod -R a+r /usr/share/nginx/html

需要注意的主要事情之一是我没有在这里公开端口 80。根据设计的应用程序引擎(我猜 Kubernetes 编排正在发生但不确定)公开端口 8080 以访问应用程序。如果我们要暴露我们自己的端口,它是行不通的。如果有人对此有确切的答案,欢迎您提供原因。

另外要注意的另一件事是,如果存在任何撰写文件,应用引擎不会考虑。目前没有基于它创建容器


推荐阅读