将 docker 镜像部署到 GAE 以获得角度



我一直在尝试使用 nginx 作为服务器构建 docker 容器,这在本地运行良好,但由于某种原因我无法将其部署到 Google 的应用程序引擎中。你能告诉我是否有可能吗?如果可能的话,您也请指导我。我在 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
}
}

感谢吉列尔莫。我确实找到了答案。我无法成功部署以前的 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 以访问应用程序。如果我们要公开我们自己的端口,它不起作用。如果有人对此有确切的答案,非常欢迎您提供原因。

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

绝对可以在Google Cloud Platform中使用Dockerfilenginx服务器部署到Google App Engine。为此实现了自定义运行时。

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

按照 GAE Flex 快速入门中的自定义运行时对其进行测试。它使用Dockerfile来定义环境,还会向 App Engine 启动nginxWeb 服务器。了解快速入门示例的工作原理后,将Dockerfilenginx.conf文件替换为你的文件。您可能需要稍微调整一下它们。

最新更新