简体   繁体   中英

How to Run a script at the start of Container in Cloud Containers Engine with Kubernetes

I am trying to run a shell script at the start of a docker container running on Google Cloud Containers using Kubernetes. The structure of my app directory is something like this. I'd like to run prod_start.sh script at the start of the container (I don't want to put it as part of the Dockerfile though). The current setup fails to start the container with Command not found file ./prod_start.sh does not exist . Any idea how to fix this?

app/
  ...
  Dockerfile
  prod_start.sh
  web-controller.yaml
  Gemfile
  ...

Dockerfile

FROM ruby
RUN mkdir /backend
WORKDIR /backend
ADD Gemfile /backend/Gemfile
ADD Gemfile.lock /backend/Gemfile.lock
RUN bundle install

web-controller.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: backend
  labels:
    app: myapp
    tier: backend
spec:
  replicas: 1
  selector:
    app: myapp
    tier: backend
  template:
    metadata:
      labels:
        app: myapp
        tier: backend
    spec:
      volumes:
      - name: secrets
        secret:
          secretName: secrets
      containers:
      - name: my-backend
        command: ['./prod_start.sh']
        image: gcr.io/myapp-id/myapp-backend:v1
        volumeMounts:
        - name: secrets
          mountPath: /etc/secrets
          readOnly: true
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 80
          name: http-server

After a lot of experimentations I believe adding the script to the Dockerfile :

ADD prod_start.sh /backend/prod_start.sh

And then calling the command like this in the yaml controller file:

command: ['/bin/sh', './prod_start.sh']

Fixed it.

you can add a config map to your yaml instead of adding to your dockerfile.

apiVersion: v1
kind: ReplicationController
metadata:
  name: backend
  labels:
    app: myapp
    tier: backend
spec:
  replicas: 1
  selector:
    app: myapp
    tier: backend
  template:
    metadata:
      labels:
        app: myapp
        tier: backend
    spec:
      volumes:
      - name: secrets
        secret:
          secretName: secrets
      - name: prod-start-config
        configMap:
          name: prod-start-config-script
          defaultMode: 0744
      containers:
      - name: my-backend
        command: ['./prod_start.sh']
        image: gcr.io/myapp-id/myapp-backend:v1
        volumeMounts:
        - name: secrets
          mountPath: /etc/secrets
          readOnly: true
        - name: prod-start-config
          mountpath: /backend/
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 80
          name: http-server

Then create another yaml file for your script: script.yaml

apiVersion: v1
kind: ConfigMap
metadata: 
  name: prod-start-config-script
data:
  prod_start.sh: |
    apt-get update 

When deployed the script will be in the scripts directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM