Cannot load PHP class when running Symfony command - php

I would appreciate any help I can get with following issue.
I've set up Symfony Messenger to use AMQP and RabbitMQ. I can dispatch message and make use of AMQPConnection within my project.
But when I try to consume the message I get following:
$ php bin/console messenger:consume-messages amqp
Attempted to load class "AMQPConnection" from the global namespace.
Did you forget a "use" statement for "PhpAmqpLib\Connection\AMQPConnection"?
The console command calls a method within AmqpFactory.php
public function createConnection(array $credentials): \AMQPConnection
{
return new \AMQPConnection($credentials);
}
So when I run the project from the browser, everything works fine. I can use AMQPConnection and all. But when running from terminal, it cannot find the AMQPConnection class.
From within my composer.json I also have the amqplib installed
"php-amqplib/php-amqplib": "^2.7",
I use Docker and the Dockerfile contains:
FROM php:7-apache
RUN usermod -u 1000 www-data &&\
a2dissite 000-default &&\
apt-get update &&\
apt-get install -y \
zlib1g-dev \
libicu-dev \
g++ \
libfreetype6-dev \
libssh-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
librabbitmq-dev \
libpng-dev &&\
docker-php-ext-configure intl &&\
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ &&\
docker-php-ext-install -j$(nproc) gd pdo_mysql intl opcache zip pcntl exif bcmath sockets &&\
a2enmod rewrite
RUN pecl install amqp \
&& docker-php-ext-enable amqp
COPY docker/apache2.conf /etc/apache2/apache2.conf
WORKDIR /var/www/project
UPDATE!
I've printed the phpinfo() and when dispatching the message I have version 7.2.8 and when executing the terminal command I have 7.2.7. So terminal is not using the Docker instance of PHP when executing the command.

Solved this, finally. I had no idea I had to go via Docker to run this command. This worked for me:
docker exec -ti container_name sh -c "cd /var/www/project/ && php bin/console messenger:consume-messages amqp"

When using Docker, Symfony commands needs to be executed inside needed container.
The problem come from trying to execute php from the local host:
$ php bin/console messenger:consume-messages amqp
whereas the local host does not have the necessary dependencies, RabbitMQ in this case.
Instead the command should be run using the right container:
$ docker exec -ti container_name sh -c "cd /var/www/project/ && php bin/console messenger:consume-messages amqp"
container_name being the name of the container having the RabbitMQ dependencies.

You are missing package librabbitmq4 in your Docker container. Add RUN apt-get update --fix-missing && apt-get update && apt-get install php-amqp to your Dockerfile and rebuild the image.

Related

Problem: Different encoding in two equal docker containers

Currently I have a very weird docker behavior and I really hope someone has an idea.
Here are some information about the setup:
My Dockerfile is based on php:7.4.27-apache-buster.
The container is build on two Kubernetes clusters (Staging and Productive).
The PHP application fetches data by a curl request two a third party system and tries to process this data and store it into a mariaDB database.
The mariaDB databases on staging and productive are equally setup.
The third party system responds with same data and encoding on both environments.
The weird behavior is, the staging container works as expected but the production container doesn't encode unicode characters properly.
Because both containers are build with the same Dockerfile, use the same application code, receive the same data and work with equally configured databases, I have no clue how this problem can be even possible. We also already tried to rebuild the container for both environments but nothing changed.
Did anybody experience something like that? Or has anybody any idea what the reason of this difference could be?
Application description:
The PHP application runs on a cron job. Every 15 minutes, a request is sent to a third party system, which response with a dataset in json format. This data is decoded processed by the application. While processing, the application checks for data validity and removes invalid data from the set. As a last step, the data is stored in the database.
MariaDB version:
Prod: 10.1.1
Dev: 10.5.17
PHP version: 7.4.27
Docker version: 20.10.18
Kubernetes version: 1.20.6
Go version: 1.18.6
Dockerfile:
FROM php:7.4.27-apache-buster
RUN curl -sL https://deb.nodesource.com/setup_17.x | bash -
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs
RUN apt-get remove -y libstdc++-8-dev && \
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
apt-get install -y fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf && \
apt-get install -y libx11-xcb1 libxtst6 libnss3 libxss1 libasound2 libatk-bridge2.0-0 libgtk-3-0 && \
apt-get install -y xvfb
RUN apt-get install -y cron sendmail && \
apt-get install -y libzip-dev && \
apt-get install -y chromium && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /var/cache/*
RUN docker-php-ext-install pdo pdo_mysql mysqli zip
RUN apt-get update -y && apt-get install -y zlib1g-dev libpng-dev libjpeg-dev libfreetype6-dev imagemagick && \
docker-php-ext-configure gd --with-jpeg --with-freetype && \
docker-php-ext-install gd
COPY container/policy.xml /etc/ImageMagick-6/policy.xml
RUN apt-get update -y && \
apt-get install -y libmagickwand-dev && \
pecl install imagick && \
docker-php-ext-enable imagick
RUN groupadd -g 118 myapp
RUN useradd -d /myapp -G audio,video -g myapp -u 113 myapp
RUN chsh -s /bin/bash myapp
COPY --chown=myapp . .
COPY container/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod a+rx /usr/local/bin/entrypoint.sh
WORKDIR /app
EXPOSE 80
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

Docker fails to enable php extensions

I am trying to install some php extensions all day but an error I do not understand occurs. Basically if I remove the install docker-php-ext-enable line everything seems to work normally (except my app that depends on them).
Dockerfile
FROM php:8.0-apache
RUN apt-get update && \
apt-get install -y unzip libcurl4-openssl-dev zlib1g-dev libpng-dev libxml2-dev libzip-dev
RUN a2enmod rewrite
RUN docker-php-ext-enable bcmath gd mysqli soap zip xml ctype curl fileinfo json
COPY idoit-1.16.2.zip /temp/idoit-1.16.2.zip
RUN unzip /temp/idoit-1.16.2.zip -d /var/www
RUN rm /temp/idoit-1.16.2.zip
COPY apache-vhost.conf /etc/apache2/sites-available/000-default.conf
# RUN chown -R www-data:www-data /var/www
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
EXPOSE 80
Here is what I get when i run sudo docker build -t test .
Sending build context to Docker daemon 153.9MB
Step 1/23 : FROM php:8.0-apache
---> 1157b188bd87
Step 2/23 : RUN apt-get update && apt-get install -y unzip libcurl4-openssl-dev zlib1g-dev libpng-dev libxml2-dev libzip-dev
---> Using cache
---> aacd74715e3f
Step 3/23 : RUN a2enmod rewrite
---> Using cache
---> 1ab1cd3d1228
Step 4/23 : RUN docker-php-ext-enable fileinfo
---> Running in 7d8a84075a50
error: 'fileinfo' does not exist
usage: /usr/local/bin/docker-php-ext-enable [options] module-name [module-name ...]
ie: /usr/local/bin/docker-php-ext-enable gd mysqli
/usr/local/bin/docker-php-ext-enable pdo pdo_mysql
/usr/local/bin/docker-php-ext-enable --ini-name 0-apc.ini apcu apc
Possible values for module-name:
opcache.so sodium.so
Some of the above modules are already compiled into PHP; please check
the output of "php -i" to see which modules are already loaded.
Any ideas?

How to run cron and web application in same container?

I am new to docker. Hardly I have containerized my php application to run it in the web interface. But I have some cron to run with it. I learnt how to create separate cron image and run it from How to run a cron job inside a docker container?. But my use case is different. I need to use the php files from my php application container which seems not possible from my way. I tried creating the docker-compose.yml as follow to see if it would work
docker-compose.yml:
version: "3"
services:
app:
build:
context: ./docker/php
container_name: 'app'
restart: 'always'
ports:
- "80:80"
- "443:443"
links:
- db
volumes:
- ${DOCUMENT_ROOT-./src}:/var/www/html
- ${PHP_INI-./docker/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./docker/apache2/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./docker/logs/apache2}:/var/log/apache2
extra_hosts:
- "test.local:127.0.0.1"
hostname: cloudservice.local
domainname: local
#entrypoint: sh /var/www/html/cron.sh
As I have commented entry point here and if I do docker-compose up, everything works perfectly fine, My Dockerfile is as under
Dockerfile:
FROM php:7.2.27-apache
RUN apt-get update
RUN apt-get install -y cron
RUN apt-get -y update --fix-missing
RUN apt-get upgrade -y
# Install useful tools
RUN apt-get -y install apt-utils nano wget dialog
# Install important libraries
RUN apt-get -y install --fix-missing apt-utils build-essential git curl libcurl4 libcurl4-openssl-dev zip
# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install xdebug
#RUN pecl install xdebug-2.5.0
#RUN docker-php-ext-enable xdebug
# Other PHP7 Extensions
RUN apt-get -y install libsqlite3-dev libsqlite3-0 default-mysql-client
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install pdo_sqlite
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install curl
RUN docker-php-ext-install tokenizer
RUN docker-php-ext-install json
RUN apt-get -y install zlib1g-dev
RUN docker-php-ext-install zip
RUN apt-get -y install libicu-dev
RUN docker-php-ext-install -j$(nproc) intl
RUN docker-php-ext-install mbstring
RUN apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd
RUN pecl install redis-5.1.1 \
&& docker-php-ext-enable redis
# Enable apache modules
RUN a2enmod rewrite headers
My cron.sh file is as under
#!/usr/bin/env bash
# Ensure the log file exists
touch /var/www/html/logs/crontab.log
# Ensure permission on the command
chmod a+x /var/www/html/cron-local/hn-shc-rapid-daily.sh
# Added a cronjob in a new crontab
echo "* * * * * bash /var/www/html/cron-local/hn-shc-rapid-daily.sh >> /var/www/html/logs/crontab.log 2>&1" > /etc/crontab
# Registering the new crontab
crontab /etc/crontab
# Starting the cron
/usr/sbin/service cron start
# Displaying logs
# Useful when executing docker-compose logs mycron
tail -f /var/www/html/logs/crontab.log
But with the entry point commented, i cannot run cron. If i don't comment entrypoint, then cron runs, my web application doesn't. Is there any possibility to fix this?
Thanks
At the end of the docker file i added the following code after removing
RUN a2enmod rewrite headers
# Copy hello-cron file to the cron.d directory
COPY hello-cron /etc/cron.d/hello-cron #just any name
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Apply cron job
RUN crontab /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Enable apache modules
RUN a2enmod rewrite headers
CMD cron && /usr/sbin/apache2ctl -D FOREGROUND
and cron.sh was changed to
* * * * * echo "hello world" >> /var/www/html/logs/crontab.log 2>&1
This worked for me. I need not have to add entry neither on docker-compose nor on docker file, but I guess entry also would work.
I don't know if this is the right way to do. If anyone would give me more advance idea, I would be happy to give it a try.
Thanks
I think it's better if you specify the entry point in the docker-compose file without "sh" in front of it. Remember that declaring a new entrypoint in the docker-compose file overwrites the entrypoint in the dockerfile. Link
I would advise you to create your own Entrypoint Script which will execute your crons in the container CMD ["/entrypoint.sh"]
Example:
Create an file and named "entrypoint.sh" or whatever and save it in the same folder where your Dockerfile is located. In this file push your Content from your cron.sh.
RUN apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd
RUN pecl install redis-5.1.1 \
&& docker-php-ext-enable redis
# Enable apache modules
RUN a2enmod rewrite headers
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]
But you could also just add your cron directly in the docker file.
...
# Enable apache modules
RUN a2enmod rewrite headers
COPY your-cron.cron /
RUN crontab /your-cron.cron
...

Can't build php docker image [duplicate]

This question already has answers here:
ADD failed : No such file/Directory while building docker image
(3 answers)
Closed 2 years ago.
I got this error when building docker php image
Step 13/25 : ADD php.ini /usr/local/etc/php/php.ini
ERROR: Service 'phpt3' failed to build: ADD failed: stat
/var/lib/docker/tmp/docker-builder310748204/php.ini: no
such file or directory
Below is the docker file:
FROM php:7.3-fpm
# install the PHP extensions we need
RUN apt-get update \
&& apt-get install -y --no-install-recommends msmtp mailutils vim curl debconf subversion git apt-transport-https apt-utils \
build-essential locales acl mailutils wget nodejs \
gnupg gnupg1 gnupg2 \
zlib1g-dev zlib1g-dev libicu-dev g++ \
sudo
# Install GD
RUN apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install gd
# MYSQLI
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
# Install ext-zip
RUN apt-get install -y unzip libzip-dev
RUN docker-php-ext-install zip
RUN docker-php-ext-configure intl
RUN docker-php-ext-install pdo_mysql json calendar intl
ADD php.ini /usr/local/etc/php/php.ini
COPY additionnal.ini /usr/local/etc/php/conf.d/
COPY php-fpm-pool.conf /usr/local/etc/php/pool.d/www.conf
RUN rm -rf /var/lib/apt/lists/* \
echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer --version
# set up sendmail config, see http://linux.die.net/man/5/ssmtp.conf for options
RUN echo "hostname=localhost.localdomain" > /etc/msmtp/msmtp.conf
RUN echo "mailhub=maildevt3" >> /etc/msmtp/msmtp.conf
# The above 'maildevt3' is the name you used for the link command
# in your docker-compose file or docker link command.
# Docker automatically adds that name in the hosts file
# of the container you're linking MailDev to.
# Set up php sendmail config
RUN echo "sendmail_path=sendmail -i -t" >> /usr/local/etc/php/conf.d/php-sendmail.ini
# Fully qualified domain name configuration for sendmail on localhost.
# Without this sendmail will not work.
# This must match the value for 'hostname' field that you set in ssmtp.conf.
RUN echo "localhost localhost.localdomain" >> /etc/hosts
WORKDIR /var/www/
EXPOSE 9000
CMD ["php-fpm"]
Can anyone help me to overcome this bug.
I'm doing this tutorial
https://passions.miary.dev/2019/08/30/docker-maildev-fr/
It looks like the image installer does not have the rights to write to the installation directory.
-> How to fix:
ADD failed : No such file/Directory while building docker image
If that doesn't work: test if the folder, which is shown in the error exists.
Give enough permissions to the directory with chmod -> https://wiki.ubuntuusers.de/chmod/

How to add Zend Guard Loader support in docker php official image instance?

I want to add Zend Guard Loader support on my php instance.
http://www.zend.com/en/products/loader/downloads#Linux
Normally, I will download the package, and then add the following settings into php.ini
[Zend Guard Loader]
zend_extension="/usr/local/webserver/php/ext/ZendGuardLoader.so"
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path="/var/developer.zl"
But, now I'm running the instance within docker.
docker run --name php_instance php:5-fpm
And I tried to get into the shell:
docker exec -it php_instance bash
But I cannot find the php.ini, how can I make it work?
Did you add a command in your Dockerfile to copy the php.ini file from local to docker container?
Similar to
FROM php:7.1-fpm
# Install system packages
RUN apt-get update && apt-get install -y \
openssl \
libssh2-1 \
libssh2-1-dev \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
RUN pecl install xdebug
# Enable php extensions
RUN docker-php-ext-install mysqli pdo_pgsql
RUN pecl install ssh2-1.1.2
# Copy custom php.ini file
ADD ./deployment/my.php.ini /usr/local/etc/php/

Categories