我一直在尝试使用nginx作为服务器构建docker容器,该容器在本地完全可以正常运行,但是由于某些原因,我无法将其部署到Google的App引擎。您能否先让我知道是否可行?如果可能的话,请您也指导我。我在GCP的容器存储库中也有该镜像,但是找不到将其部署到GAE的方法
这是我的 docker
#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端口以访问应用程序。如果我们要公开自己的端口,那将不起作用。如果有人有确切答案,欢迎您提供原因。另外要注意的另一件事是,如果存在任何撰写文件,则应用引擎不会考虑在内。当前没有基于该容器创建容器
关于angular - 将docker镜像部署到GAE以获得 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52510297/