【问题标题】:Deploy docker image to GAE for angular将 docker 镜像部署到 GAE 以获取角度
【发布时间】:2020-09-30 02:00:34
【问题描述】:

我一直在尝试使用 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
       }

    }

【问题讨论】:

    标签: angular docker google-app-engine nginx dockerfile


    【解决方案1】:

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

    App 引擎默认侦听端口 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 以访问应用程序。如果我们要暴露我们自己的端口,它是行不通的。如果有人对此有确切的答案,欢迎您提供原因。

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

    【讨论】:

    • 很好地找到了解决方案!很高兴我发布的信息对您有所帮助。你能accept your own answer吗?它将使其更加明显,并在您找到解决方案时帮助遇到相同问题的人。谢谢!
    【解决方案2】:

    绝对可以使用 Google Cloud Platform 中的 Dockerfilenginx 服务器部署到 Google App Engine。 Custom runtimes 就是为此目的而实现的。

    要创建自定义运行时,请使用您选择的基本映像创建 Dockerfile,然后添加构建所需运行时环境的 docker 命令。

    按照custom runtimes in GAE Flex quickstart 进行测试。它使用Dockerfile 来定义环境,还向App Engine 启动nginx 网络服务器。在查看快速入门示例的工作原理后,将 Dockerfilenginx.conf 文件替换为您的文件。您可能需要稍微调整一下。

    【讨论】:

    • 感谢@Guillermo。我先试试看
    猜你喜欢
    • 2020-09-09
    • 2020-08-19
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2022-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多