<p>I'm looking for the best possible Kubernetes configuration to be able to run this command in a <strong>local Kubernetes environment</strong> (via Minikube)</p>

<pre><code>composer install
</code></pre>

<p>What I was hoping to do is something like the docker version:
This will spin up a container, run the composer install command and then remove the container again. </p>

<pre><code>docker run --rm -i -t -v $(pwd):/app composer:latest composer install
</code></pre>

<p>This doesn't work because kubectl doesn't have a option to mount a volume (this is required to indicate the project's composer.json file) </p>

<p>What I'm doing now is, including a <strong>composer container configuration</strong> within a Kubernetes deployment.yml file.</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-site
  labels:
    app: web
spec:
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        # The Application
        - name: api-app
          image: api_app:my_company
          volumeMounts:
            - name: shared-api-data
              mountPath: /var/www
          env:
            - name: DB_HOST
              value: "xx.xx.xx.xx"
            - name: DB_DATABASE
              value: "my_database"
            - name: DB_USER
              value: "my_database_user"
            - name: DB_PASSWORD
              value: "my_database_password"
        # The Web Server
        - name: api-web
          image: api_web:my_company
          volumeMounts:
            - name: shared-api-data
              mountPath: /var/www
          ports:
            - containerPort: 80
        # Composer
        - name: composer
          image: composer
          volumeMounts:
            - name: shared-api-data
              mountPath: /app
           args:
            - composer
            - install
      volumes:
        - name: shared-api-data
          hostPath:
            path: /var/www
</code></pre>

<p>I create the actual deployment with this command: </p>

<pre><code>kubectl create -f deployment.yml
</code></pre>

<p>I keep seeing this error (in my Minikube dashboard): 'back-off failed to restart container' (probably because the composer install command finished and therefore the container exits). I know there's a <strong>restartPolicy</strong> config parameter for pods, but I don't want to set this value for my other containers (web &amp; app). So it might be better to create a separate deployment.yml file just for composer? </p>

<p>Another option (that I'm keeping for last, as I'm not really a fan of it) is  to install composer in my php-fpm image. </p>

<p>Cheers!</p>

mattyh88
<p>You should build a custom Docker image containing your application’s code.  The <code>composer install</code> command should be in your <code>Dockerfile</code>.  When you build a new version of your application that you want to deploy, you (or, better, your CI system) need to <code>docker build</code> a new image, <code>docker push</code> it to a registry, and then update the version tag in the Kubernetes Deployment object.</p>

<p>The Docker development pattern of “running a Docker container” where the image only has the language runtime and the application code actually lives on a bind-mounted host directory doesn’t really work in Kubernetes, or any other clustered installation (people have had trouble trying to do it in Docker Swarm too, for similar reasons).</p>
