From c71854ec7e2b4302d325e9b8b8ec0ff4f017acf0 Mon Sep 17 00:00:00 2001 From: Marcus Smith Date: Fri, 5 Jan 2018 17:44:13 -0500 Subject: [PATCH] Improve Angular Dockerfile, factor out configurable k8s values --- Dockerfile | 24 ++++++++++++++++++++++-- default.conf | 10 ++++++++++ k8s/deployment.yaml | 21 +++++++++++++-------- nginx.conf | 27 +++++++++++++++++++++++++++ web/main.dart | 15 +-------------- 5 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 default.conf create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile index 60e86fc..e77dd4c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,25 @@ +FROM google/dart AS build-env +WORKDIR /app + +ADD pubspec.* /app/ +RUN pub get --no-precompile +ADD . /app/ +RUN pub get --offline --no-precompile +RUN pub build + FROM nginx -COPY ./build/web /usr/share/nginx/html +COPY --from=build-env /app/build/web /usr/share/nginx/html +COPY default.conf /etc/nginx/conf.d/default.conf +COPY nginx.conf /etc/nginx/nginx.conf + +RUN groupadd -r angular +RUN useradd -m -r -g angular angular + +RUN touch /var/run/nginx.pid && \ + chown -R angular:angular /var/run/nginx.pid && \ + chown -R angular:angular /var/cache/nginx + +USER angular -EXPOSE 80 \ No newline at end of file +EXPOSE 8080 diff --git a/default.conf b/default.conf new file mode 100644 index 0000000..d6e747f --- /dev/null +++ b/default.conf @@ -0,0 +1,10 @@ +server { + listen 8080; + server_name localhost; + + location / { + root /usr/share/nginx/html; + index index.html; + try_files $uri $uri/ /index.html; + } +} \ No newline at end of file diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml index 4a8239b..cdaffca 100644 --- a/k8s/deployment.yaml +++ b/k8s/deployment.yaml @@ -2,10 +2,11 @@ apiVersion: v1 kind: Service metadata: name: web-service - namespace: aqueduct-tutorial + namespace: $NAMESPACE spec: selector: - app: aqueduct-tutorial + app: $APP_NAME + role: Frontend ports: - port: 80 targetPort: 80 @@ -14,31 +15,35 @@ apiVersion: apps/v1beta1 kind: Deployment metadata: name: web-deployment - namespace: aqueduct-tutorial + namespace: $NAMESPACE spec: replicas: 1 template: metadata: labels: - app: aqueduct-tutorial + app: $APP_NAME + role: Frontend spec: containers: - - name: aqueduct-tutorial + - name: $APP_NAME imagePullPolicy: Always image: $IMAGE_NAME ports: - containerPort: 80 + securityContext: + runAsNonRoot: true + readOnlyRootFilesystem: true --- apiVersion: extensions/v1beta1 kind: Ingress metadata: - name: tutorial-ingress - namespace: aqueduct-tutorial + name: $INGRESS_NAME + namespace: $NAMESPACE annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - - host: aqueduct-tutorial.stablekernel.io + - host: $HOSTNAME http: paths: - path: / diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..8920d6d --- /dev/null +++ b/nginx.conf @@ -0,0 +1,27 @@ +worker_processes 1; + +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; + + +events { + worker_connections 1024; +} + + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + + keepalive_timeout 65; + + include /etc/nginx/conf.d/*.conf; +} diff --git a/web/main.dart b/web/main.dart index addd67e..fdecba9 100644 --- a/web/main.dart +++ b/web/main.dart @@ -1,7 +1,6 @@ import 'package:angular/angular.dart'; import 'package:angular_router/angular_router.dart'; import 'package:angular_tour_of_heroes/app_component.dart'; -import 'package:angular_tour_of_heroes/in_memory_data_service.dart'; import 'package:http/http.dart'; import 'package:http/browser_client.dart'; @@ -9,21 +8,9 @@ void main() { bootstrap(AppComponent, [ ROUTER_PROVIDERS, // Remove next line in production - provide(LocationStrategy, useClass: HashLocationStrategy), +// provide(LocationStrategy, useClass: HashLocationStrategy), // Using a real back end? // Import browser_client.dart and change the above to: provide(Client, useFactory: () => new BrowserClient(), deps: []) ]); } -/* -import 'package:http/browser_client.dart'; - -void main() { - bootstrap(AppComponent, [ - ROUTER_PROVIDERS, - // Remove next line in production - provide(LocationStrategy, useClass: HashLocationStrategy), - provide(BrowserClient, useFactory: () => new BrowserClient(), deps: []) - ]); -} -*/ -- GitLab