How to run composer install in a mini(Kubernetes) environment

7/23/2018

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

composer install

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.

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

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)

What I'm doing now is, including a composer container configuration within a Kubernetes deployment.yml file.

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

I create the actual deployment with this command:

kubectl create -f deployment.yml

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 restartPolicy config parameter for pods, but I don't want to set this value for my other containers (web & app). So it might be better to create a separate deployment.yml file just for composer?

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.

Cheers!

-- mattyh88
composer-php
containers
docker
kubernetes
minikube

1 Answer

7/23/2018

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

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).

-- David Maze
Source: StackOverflow