Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 570
mkhatib Avatar asked Nov 19 '15 08:11

mkhatib


People also ask

How do I run a command in a container?

To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd.

How do I run a program on Kubernetes?

To run an application in a cluster, you have to pack our app into a container, then create Docker image from the container and finally send it to the Docker registry. After that, you need to define which image should be installed on Kubernetes node. Kubernetes uses a mechanism called Pod to manage containers.


1 Answers

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.

like image 101
mkhatib Avatar answered Sep 28 '22 08:09

mkhatib