Artisan can't connect to composer in containers

Viewed 725

I am doing laravel application and installing Laravel jetstream in Docker containers. I have separate containers for composer and artisan. And when I try to install jetstream by command:

docker-compose run --rm artisan jetstream:install inertia

I get an error:

Starting mysql ... done
sh: exec: line 1: composer: not found
Unable to locate publishable resources.
Publishing complete.

Inertia scaffolding installed successfully.
Please execute the "npm install && npm run dev" command to build your assets.

The webpage still doesn't work with error message Class 'Inertia\Inertia' not found. I assume there is a problem with connection between composer and artisan containers, but how I can set up this connection?

Docker-compose.yml

  composer:
    image: composer:latest
    container_name: composer
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - php
    networks:
      - laravel

  artisan:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: artisan
    volumes:
      - ./src:/var/www/html
    depends_on:
      - mysql
    working_dir: /var/www/html
    entrypoint: ['/var/www/html/artisan']
    networks:
      - laravel
3 Answers

Sadly I can't comment yet (< 50 reputation), but had a similar issue as you just now, an alternative to running it in different containers is to make a helper container and executing it all inside:

docker-compose.yml (note: ./code is your laravel root/folder)

version: '3'

services:
  helper:
    build: ./composer-artisan-helper
    volumes:
      - ./code:/app 

Make a folder composer-artisan-helper and create a Dockerfile inside:

FROM php:7.4-fpm

# Install git
RUN apt-get update && apt-get install -y git

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Keep running
ENTRYPOINT ["tail", "-f", "/dev/null"]

# Set work-directory
WORKDIR /app

Now run it and drop into that container:

docker-compose exec helper bash

make sure it now has all your laravel folder by doing a quick ls - if everything is fine execute

php artisan jetstream:install inertia

you should now be greeted eventually with Inertia scaffolding installed successfully..

Hope that helps! even though it's not split up into multiple containers (which isn't possible afaik anyway).

I was trying to install it with command:

docker-compose run --rm artisan jetstream:install inertia

But actually it works when I try to run it in second container:

docker-compose run --rm composer php artisan jetstream:install inertia

In my configuration artisan container doesn't have a connection to the composer, but the composer container has to the artisan obviously.

there is a right answer here! thank you Jsowa

But you can install related packages separately. You need to install inertiajs package for laravel with the following command.

  1. docker-compose run --rm composer require inertiajs/inertia-laravel
  2. docker-compose run --rm artisan inertia:middleware
Related