简体   繁体   中英

Docker with Doctrine generate-proxies

I'm trying to create a Docker container (using docker-compose) for an application wit Doctrine, the problem is: if I just run the application, it works, but when I try to use the application before I run command ./vendor/bin/doctrine orm:generate-proxies , I get the error:

PHP Warning:  require(/tmp/__CG__DomainEntitiesAnyEntity.php): failed to open stream: No such file or directory in /var/www/html/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php on line 204
PHP Fatal error:  require(): Failed opening required '/tmp/__CG__DomainEntitiesAnyEntity.php' (include_path='.:/usr/local/lib/php') in /var/www/html/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php on line 204

OK, so just run the command on docker-compose.yml

version: '3'

services:
  apache_server:
    build: .
    working_dir: /var/www/html
    ports:
      - "80:80"
    volumes:
      - ./:/var/www/html
      - ../uploads:/var/www/uploads
      - ./.docker/apache2.conf:/etc/apache2/apache2.conf
      - ./.docker/000-default.conf:/etc/apache2/sites-avaliable/000-default.conf
      - ./.docker/php.ini:/etc/php/7.4/apache2/php.ini
    depends_on:
      - postgres_database
    command: sh -c "./vendor/bin/doctrine orm:generate-proxies"
    networks:
      - some-network

Yes, it works as expected and generates the proxies to /tmp folder, but after the command run and after the prompt with proxies generated, I get the message exited with code 0 . It happens because Docker finish the container execution after getting the status code 0 . So I tried two more things:

  1. Add tail to something:
    command: sh -c "./vendor/bin/doctrine orm:generate-proxies && tail -f /var/www/html/log.txt"

but when I do this, the server doesn't respond to requests (http://localhost/) anymore.

  1. Add tty before running the command:
    tty: true
    # restart: unless-stopped    <--- also tried this

and doesn't work also. Is there another way to solve this without I have to manually run the command inside the container every time?

PS: my dockerfile is this one:

FROM php:7.4-apache

WORKDIR /var/www/html

RUN a2enmod rewrite
RUN a2enmod headers

RUN mkdir /var/www/uploads
RUN mkdir /var/www/uploads/foo-upload-folder
RUN mkdir /var/www/uploads/bar-upload-folder
RUN chmod 777 -R /var/www/uploads

RUN apt-get update \
    && apt-get install -y \
        libpq-dev \
        zlib1g-dev \
        libzip-dev \
        unzip \
    && docker-php-ext-install \
        pgsql \
        pdo \
        pdo_pgsql \
        zip

RUN service apache2 restart

Cause of your issue

Your Docker Compose configuration of command

    command: sh -c "./vendor/bin/doctrine orm:generate-proxies"

in docker-compose.yml overwrites the Cmd in the Docker image php:7.4-apache that normally would start the Apache server, see

docker inspect php:7.4-apache

or more specific

docker inspect --format="{{ .Config.Cmd }}" php:7.4-apache

which gives you

[apache2-foreground]

Solution in general

If you like to run a command before the original command of a Docker image, use Entrypoint and make sure you call the original entrypoint, see

$ docker inspect --format="{{ .Config.Entrypoint }}" php:7.4-apache
[docker-php-entrypoint]

For example, instead of command define

    entrypoint: sh -c "./vendor/bin/doctrine orm:generate-proxies && docker-php-entrypoint"

Solution in your case

However, in your case, I would configure Doctrine like this (see Advanced Doctrine Configuration )

$config = new Doctrine\ORM\Configuration;
// ...
if ($applicationMode == "development") {
    $config->setAutoGenerateProxyClasses(true);
} else {
    $config->setAutoGenerateProxyClasses(false);
}

In development your code changes (mounted as volume) and proxies may have to be updated/generated. In production your code does not change anymore (copy code to Docker image). Hence, you should generate proxies in your Dockerfile (after you copied the source code), eg

FROM php:7.4-apache
WORKDIR /var/www/html
# ...
Copy . /var/www/html
RUN ./vendor/bin/doctrine orm:generate-proxies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM