<p>I am going to use GKE to build containers with rubyonrails,
I want to do db: create and db: migrate automatically</p>

<p>Dockerfile made this way</p>

<p>Dockerfile</p>

<pre><code>FROM ruby: 2.4.1-alpine

RUN apk update &amp;&amp; apk upgrade &amp;&amp; apk add --update - no - cache bash alpine - sdk tzdata postgresql - dev nodejs
RUN mkdir / app
WORKDIR / app

ADD Gemfile / app / Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install - path vendor / bundle
ADD. / App

RUN bundle exec rake assets: precompile

RUN chmod + x /app/post-start.sh

EXPOSE 3000
</code></pre>

<p>Setting db: create and db: migrate
/app/post-start.sh</p>

<pre><code>#!/bin/bash
RAILS_ENV = $ RAILS_ENV bundle exec rake db: create
RAILS_ENV = $ RAILS_ENV bundle exec rake db: migrate
</code></pre>

<p>It is deployment
rails.yml</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: rails
  labels:
    app: rails
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rails
  template:
    metadata:
      labels:
        app: rails
    spec:
      containers:
        - image: asia.gcr.io/balmy-geography-216916/railsinitpg:v1
          name: rails
          env:
            - name: RAILS_ENV
              value: "production"
            - name: DATABASE_HOST
              value: postgresql
            - name: DATABASE_USERNAME
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: database_user
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: database_password
            - name: SECRET_KEY_BASE
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: secret_key_base
            - name: DATABASE_PORT
              value: "5432"
          lifecycle:
        postStart:
         exec:
          command:
            - /bin/bash
            - -c
            - /app/post-start.sh
          ports:
            - containerPort: 3000
              name: rails
          command: ["bundle", "exec", "rails", "s", "-p", "3000", "-b", "0.0.0.0"]
</code></pre>

<p>kubectl create -f /kubernetes/rails.yml
I did it</p>

<p>Errors - Contents</p>

<pre><code>error: valid validating "STDIN": error validating data: ValidationError (Deployment.spec.template.spec.containers [0]): unknown field "postStart" in io.k8s.api.core.v1.Container; if you choose to ignore these errors, turn validation off with --validate = false
</code></pre>

<p>The above error ceased to exist, but there was an error again</p>

<pre><code>Warning  FailedPostStartHook    5m1s (x4 over 5m49s)   kubelet, gke-cluster-1-default-pool-04b4ba0b-680k  Exec lifecycle hook ([/bin/bash -c /app/post-start.sh]) for Container "rails" in Pod "rails-5c5964445-6hncg_default(1d6a3479-1fcc-11e9-8139-42010a9201ec)" failed - error: command '/bin/bash -c /app/post-start.sh' exited with 126: , message: "rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused \"exec: \\\"/bin/bash\\\": stat /bin/bash: no such file or directory\"\n\r\n"
</code></pre>

<p>Fix the Dockerfile
Fixed Deployment.yml
It went well.
Thank you very much.</p>

<p>The issue is in indentation in your yaml file, the correct yaml is:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: rails
  labels:
    app: rails
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rails
  template:
    metadata:
      labels:
        app: rails
    spec:
      containers:
        - image: asia.gcr.io/balmy-geography-216916/railsinitpg:v1
          name: rails
          env:
            - name: RAILS_ENV
              value: "production"
            - name: DATABASE_HOST
              value: postgresql
            - name: DATABASE_USERNAME
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: database_user
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: database_password
            - name: SECRET_KEY_BASE
              valueFrom:
                secretKeyRef:
                  name: rails
                  key: secret_key_base
            - name: DATABASE_PORT
              value: "5432"
          lifecycle:
            postStart:
              exec:
                command:
                - /bin/bash
                - -c
                - /app/kubernetes-post-start.sh
          ports:
            - containerPort: 3000
              name: rails
          command: ["bundle", "exec", "rails", "s", "-p", "3000", "-b", "0.0.0.0"]    
</code></pre>

<p>This should resolve the above error.</p>
