Docker + PHP + RabbitMQ-ThrowExceptions

Exception or error:

I am trying to run a php program in a Docker container that listens for data from RabbitMQ.

Here’s what I have done so far:

  • Multi stage build with composer:1.9.3, php:7.4.3-cli
  • Installed phpAmqpLib/php-amqplib

When I run the program I get “Warning: require(./vendor/autoload.php): failed to open stream: No such file or directory in /usr/src/myapp/test.php on line 3”

I ran “composer install” and I am still getting an error please assist if possible.

# PHP dependency manager
FROM composer:1.9.3 AS composer

FROM php:7.4.3-cli

# Copies dependency manager into PHP image
COPY --from=composer /usr/bin/composer /usr/local/bin/composer

# Install RabbitMQ PHP client
RUN apt-get -qq update && apt-get -qq -y install \
    git \
    libzip-dev \
    unzip \
    && docker-php-ext-install \
       sockets \
       zip \
    && composer require php-amqplib/php-amqplib \

    && composer install
# Copies files from a source directory into the containers filesystem
COPY . /usr/src/myapp
CMD ["php", "/usr/src/myapp/test.php"]

test.php

<?php
require 'vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;

$connection = new AMQPStreamConnection('messaging', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);

echo " [*] Waiting for messages. To exit press CTRL+C\n";

$callback = function ($msg) {
    echo ' [x] Received ', $msg->body, "\n";
};

$channel->basic_consume('hello', '', false, true, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}

$channel->close();
$connection->close();

Edit: Line 1 of test.php was previously using require_once __DIR__ . '/vendor/autoload.php'; and pointing to the current directory when in fact vendor was installed in the root directory.

How to solve:

Line 1 of test.php was previously using require_once __DIR__ . '/vendor/autoload.php'; and pointing to the current directory when in fact ‘vendor’ was installed in the root directory.

Leave a Reply

Your email address will not be published. Required fields are marked *