<p>I am having two dockerized application which needs to run in kubernetes. </p>

<p>Here is the scenario which needs to achieve.</p>

<p>Docker-1 which is flask application.</p>

<p>Docker-2 which is python script will take input from the Docker-1 and execute and need to write some file in a shared volume of the Docker-1 container.</p>

<p>Here is the flask web-app code.</p>

<pre><code>from flask import Flask, request, Response, jsonify
app = Flask(__name__)

@app.route('/')
def root():
  return "The API is working fine"

@app.route('/run-docker')
def run_docker_2():
   args = "input_combo"
   query = &lt;sql query&gt;
   &lt;initiate the docker run and pass params&gt;
   exit
   #No return message need run as async

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=8080, threaded=True)
</code></pre>

<p>Docker file</p>

<pre><code>FROM ubuntu:latest
MAINTAINER Abhilash KK "abhilash.kk@searshc.com"
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential python-tk
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["/usr/bin/python"]
CMD ["app.py"]
</code></pre>

<p>requirements.txt</p>

<pre><code>flask
</code></pre>

<p>Python script for the second docker. start_docker.py</p>

<pre><code>import sys

input_combo = sys.argv[1]
query = sys.argv[2]

def function_to_run(input_combination,query):
    #starting the model final creating file

function_to_run(input_combo, query)
</code></pre>

<p>Docker file 2</p>

<pre><code>FROM python

COPY . /script
WORKDIR /script

CMD ["python", "start_docker.py"]
</code></pre>

<p>Please help me to connect with the docker images. or let me know any other way to achieve this problem. The basic requirement is to add a message to some queue and that queue listens for in time interval and starts the process with FIFO manner.</p>

<p>Any other approach in GCP service to initiate an async job will take input from the client and create a file which is accessible from web-app python.</p>

<p>First, create a Pod running "Docker-1" application. Then <a href="https://github.com/kubernetes-incubator/client-python" rel="nofollow noreferrer">Kubernetes python client</a> to spawn a second pod with "Docker-2".
You can share a volume between your pods in order to return the data to Docker1. In my code sample I'm using a host_path volume but you need to ensure that both pods are on the same node. I did add that code for readability.</p>

<pre><code>apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: docker1
  labels:
    app: docker1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: docker1
  template:
    metadata:
      labels:
        app: docker1
    spec:
      containers:
      - name: docker1
        image: abhilash/docker1
        ports:
        - containerPort: 8080
        volumeMounts:
        - mountPath: /shared
          name: shared-volume
      volumes:
      - name: shared-volume
        hostPath:
          path: /shared
</code></pre>

<p>The code of run_docker_2 handler:</p>

<pre><code>from kubernetes import client, config

...

args = "input_combo"
config.load_incluster_config()
pod = client.V1Pod()
pod.metadata = client.V1ObjectMeta(name="docker2")
container = client.V1Container(name="docker2")
container.image = "abhilash/docker2"
container.args = [args]
volumeMount = client.V1VolumeMount(name="shared", mount_path="/shared")
container.volume_mounts = [volumeMount]
hostpath = client.V1HostPathVolumeSource(path = "/shared")
volume = client.V1Volume(name="shared")
volume.host_path = hostpath
spec = client.V1PodSpec(containers = [container])
spec.volumes = [volume]
pod.spec = spec
v1.create_namespaced_pod(namespace="default", body=pod)
return "OK"
</code></pre>

<p>A handler to read the returned results:</p>

<pre><code>@app.route('/read-results')
def run_read():
   with open("/shared/results.data") as file:
      return file.read()
</code></pre>

<p>Note that it could be useful to add a <a href="https://github.com/kubernetes-incubator/client-python/blob/master/examples/example2.py" rel="nofollow noreferrer">watcher</a> to wait for the pod to finish the job and then do some cleanup (delete the pod for instance)</p>
