Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't make it work with symfony on the same docker container #35

Closed
fvhde opened this issue May 23, 2019 · 17 comments · Fixed by #69
Closed

Can't make it work with symfony on the same docker container #35

fvhde opened this issue May 23, 2019 · 17 comments · Fixed by #69
Labels

Comments

@fvhde
Copy link

fvhde commented May 23, 2019

I'm working on a symfony project on docker container and i set up a Mailer service.I installed binaries on the container with npm install mjml. In the code :

$mail = new \Swift_Message();
$mail
     ->setFrom($from)
     ->setTo($to)
     ->setSubject($subject)
     ->setBody($body)
     ->setContentType('text/mjml');
$this->mailer->send($mail);

The thing is that when i request the function which send the mail via the docker container, there is no error but it does not send the mail. But when i start a server with php -S adress -t public and i request the function which send the mail, the mail is sent.In the log for the container there is could not found mjml binary but when i enter in the container the mjml command works

@notFloran notFloran added the bug label May 29, 2019
@notFloran
Copy link
Member

Can you try this action and copy/paste the result:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Process\Process;
use Symfony\Component\Routing\Annotation\Route;

class MjmlTestAction extends AbstractController
{
    /**
     * @Route("/mjml-test")
     */
    public function __invoke()
    {
        $process = new Process([
            $this->getParameter('kernel.project_dir') . '/node_modules/.bin/mjml',
            '-V',
        ]);
        $process->run();

        die($process->getOutput() . $process->getErrorOutput());
    }
}

@notFloran
Copy link
Member

Or try to update the package to "dev-binary-renderer-exceptions" with : composer require notfloran/mjml-bundle:dev-binary-renderer-exceptions

And then checks if the log: the error message will be more detailed

@fvhde
Copy link
Author

fvhde commented May 29, 2019

I'm getting /usr/bin/env: 'node': No such file or directory as the first answer result

@notFloran
Copy link
Member

Can you execute which node ?

mjml does not find node in the $PATH

@fvhde
Copy link
Author

fvhde commented May 29, 2019

which node on the container return me that : /usr/local/lib/nodejs/node-v12.0.0-linux-x64/bin/node

@notFloran
Copy link
Member

Can you execute echo $PATH in the terminal.

And the following code in the test action:

use Symfony\Component\Process\Process;

$process = new Process('echo $PATH');
$process->run();
echo $process->getOutput();

@fvhde
Copy link
Author

fvhde commented May 29, 2019

echo $PATH => /usr/local/lib/nodejs/node-v12.0.0-linux-x64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

and the code => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

@notFloran
Copy link
Member

I'm going to do some tests to see how to handle that.

Similar issue here: symfony/symfony#18847

@fvhde
Copy link
Author

fvhde commented May 30, 2019

Yes it's a similar issue. Thank you and let me know if there is a solution

@notFloran
Copy link
Member

@fahde-sorgho do you have an example of Dockerfile?

@fvhde
Copy link
Author

fvhde commented Jun 13, 2019

FROM php:7.3-cli

MAINTAINER Tobias Munk tobias@diemeisterei.de

# Install required system packages
RUN apt-get update && \
    apt-get -y install \
            git \
            zlib1g-dev \
            libssl-dev \
            libzip-dev \
            unzip \
        --no-install-recommends && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Install php extensions
RUN docker-php-ext-install \
    bcmath \
    zip

# Install pecl extensions
RUN pecl install \
        mongodb \
        apcu \
        xdebug-2.7.1 && \
    docker-php-ext-enable \
        apcu.so \
        mongodb.so \
        xdebug

# Configure php
RUN echo "date.timezone = UTC" >> /usr/local/etc/php/php.ini

# Install composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN curl -sS https://getcomposer.org/installer | php -- \
        --filename=composer \
        --install-dir=/usr/local/bin
RUN composer global require --prefer-dist --no-interaction --optimize-autoloader --apcu-autoloader \
        "hirak/prestissimo"

# Prepare application
WORKDIR /repo

# Install vendor
COPY ./composer.json /repo/composer.json
RUN composer install --prefer-dist --no-interaction --optimize-autoloader --apcu-autoloader

# Add source-code
COPY . /repo

ENV PATH /repo:${PATH}
ENTRYPOINT ["codecept"]

# Prepare host-volume working directory
RUN mkdir /project
WORKDIR /project

@kariae
Copy link

kariae commented Jun 22, 2019

you need to install nodejs apt-get install nodejs

@notFloran
Copy link
Member

I don't really have any leads to fix that so I'm closing the issue. If anyone has a clue to correct this, I would reopen the issue.

@mdkbdg
Copy link

mdkbdg commented Jan 12, 2021

not sure if this problem relate here, I did not use docker but I bumped here when facing following problem.
Hope it help someone (and maybe code modif suggestion below?)

Mac Catalina 10.15.4
Symfony 5.1

Problem
Running these lines work well on CLI

which node
which mjml

Check my config :

# config/packages/mjml.yaml
...
binary: '%kernel.project_dir%/node_modules/.bin/mjml'
...

BUT
when accessing url (e.g localhost/send-email)

The command """ '/Users/user/.nvm/versions/node/v10.16.0/bin/mjml' '--version'" failed.

Exit Code: 127(Command not found)

Working directory: xxx

Output:
================

Error Output:
================
env: node: No such file or directory

Yeah I know the mjml path is different from the config coz I reinstall it using
npm install -global mjml
Please read below on 'Try 2'

Looking at the error, I figured the error happened here
BinaryRenderer::getMjmlVersion()

In search of solution :
Try 1
Put this line right above $process = new Process
putenv('PATH='.$path.':/usr/local/bin');

var_dump-ing it produces
'PATH' => '/usr/bin:/bin:/usr/sbin:/sbin'
on browser screen
(different from echo $PATH run in Terminal)

Try 2
Look for node and mjml binary in /usr/local/bin , /usr/bin but did not find any.
Not sure if this affect anything : uninstall mjml then install it using -global
Then, tried to create symlink of node to /usr/bin

sudo ln -s /Users/user/.nvm/versions/node/v10.16.0/bin/node /usr/bin/node

But Operation not permitted (apple....)

Try 3
Tried to put env var here
/System/Library/LaunchDaemons/org.apache.httpd.plist
but again, Catalina put this as read only

Doing csrutil disable does not resolve the issue.
I could not change the file, nor create symlink :(

Last Resort
No choice, I have to modify the code.
Please let me know if this is not at all best practice but this has helped me

(Worth to note that the problem above does NOT happen in CentOS 7)

BinaryRenderer

...
public function getMjmlVersion(): int
{ 
...
$process = new Process([
                $this->node, // add this
                $this->bin,
                '--version',
...

Configuration

...
->arrayNode('options')
...
                        ->scalarNode('node_path') // add this
                            ->defaultValue(function () use ($finder) {
                                return $finder->find('mjml');
                            })
                            ->info('Path to the binary')
                        ->end()
                        ->scalarNode('binary')
...

MJMLExtension

...
if ('binary' === $config['renderer']) {
            $rendererDefinition = new Definition(BinaryRenderer::class);
            $rendererDefinition->addArgument($config['options']['node_path']); // add this
            $rendererDefinition->addArgument($config['options']['binary']);
...

So now I could have full path of node like so :
mjml.yaml

mjml:
    renderer: 'binary'
    options:
        node_path: '/Users/user/.nvm/versions/node/v10.16.0/bin/node' # or '%kernel.project_dir%/node_modules/.bin/node' if you prefer
        binary: '/Users/user/.nvm/versions/node/v10.16.0/bin/mjml' # or '%kernel.project_dir%/node_modules/.bin/mjml' if you prefer
        minify: false
        validation_level: skip

Same issue :
https://stackoverflow.com/questions/59004529/run-mjml-cli-using-symfony-process

@notFloran notFloran reopened this Jan 12, 2021
@notFloran
Copy link
Member

@mdkbdg Thank you for this detailed feedback!

I will test that in detail at the end of the week.
I am also on Mac with node installed with homebrew and no problem, I will give it a try with nvm.

At first glance your approach with the node_path option is the right one, I'll make a merge request to implement this.

@notFloran
Copy link
Member

I release a new version (3.5) with your implementation @mdkbdg

@mdkbdg
Copy link

mdkbdg commented Jan 20, 2021

wonderful! Many thanks @notFloran

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants