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

Using Symfony Docker with an existing project but routes missing #226

Closed
medusiora opened this issue Mar 11, 2022 · 18 comments
Closed

Using Symfony Docker with an existing project but routes missing #226

medusiora opened this issue Mar 11, 2022 · 18 comments
Labels
support Support requested

Comments

@medusiora
Copy link

Hi, I'm using Symfony with API Platform and I trying to add this docker stack on my existing project, following https://github.com/dunglas/symfony-docker/blob/main/docs/existing-project.md

In Dockerfile, I install some PHP extensions:

###> recipes ###
###> doctrine/doctrine-bundle ###
RUN docker-php-ext-install mysqli pdo pdo_mysql

# install php extensions for phpspreadsheet
RUN apk add --no-cache \
  freetype \
  libpng \
  libjpeg-turbo \
  freetype-dev \
  libpng-dev \
  libjpeg-turbo-dev \
  && docker-php-ext-configure gd --with-freetype --with-jpeg \
  && docker-php-ext-install -j$(nproc) gd
###< doctrine/doctrine-bundle ###
###< recipes ###

Others is the same as default Dockerfile https://github.com/dunglas/symfony-docker/blob/main/Dockerfile

After I build the Docker images

docker-compose build --no-cache --pull

And start the project

docker-compose up 

Some API(s) are missing!
image

So I tried to run the project with symfony command, All APIs are show up:

symfony serve

image

Then I trying to run the project with docker-compose again and try to remove some entities:
image
The rest APIs show up (without removed entities)
image

Do you have any idea?, Thanks

In docker-compose.yml:

version: "3.4"

services:
  php:
    build:
      context: .
      target: symfony_php
      args:
        SYMFONY_VERSION: ${SYMFONY_VERSION:-}
        SKELETON: ${SKELETON:-symfony/skeleton}
        STABILITY: ${STABILITY:-stable}
    restart: unless-stopped
    volumes:
      - php_socket:/var/run/php
    healthcheck:
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 30s
    environment:
      # Run "composer require symfony/mercure-bundle" to install and configure the Mercure integration
      MERCURE_URL: ${CADDY_MERCURE_URL:-http://caddy/.well-known/mercure}
      MERCURE_PUBLIC_URL: https://${SERVER_NAME:-localhost}/.well-known/mercure
      MERCURE_JWT_SECRET: ${CADDY_MERCURE_JWT_SECRET:-!ChangeMe!}

  caddy:
    build:
      context: .
      target: symfony_caddy
    depends_on:
      - php
    environment:
      SERVER_NAME: ${SERVER_NAME:-localhost, caddy:80}
      MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeMe!}
      MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeMe!}
    restart: unless-stopped
    volumes:
      - php_socket:/var/run/php
      - caddy_data:/data
      - caddy_config:/config
    ports:
      # HTTP
      - target: 80
        published: ${HTTP_PORT:-80}
        protocol: tcp
      # HTTPS
      - target: 443
        published: ${HTTPS_PORT:-443}
        protocol: tcp
      # HTTP/3
      - target: 443
        published: ${HTTP3_PORT:-443}
        protocol: udp

# Mercure is installed as a Caddy module, prevent the Flex recipe from installing another service
###> symfony/mercure-bundle ###
###< symfony/mercure-bundle ###

volumes:
  php_socket:
  caddy_data:
  caddy_config:
###> symfony/mercure-bundle ###
###< symfony/mercure-bundle ###

In docker-compose.override.yml:

version: "3.4"

# Development environment override
services:
  php:
    volumes:
      # The "cached" option has no effect on Linux but improves performance on Mac
      - ./:/srv/app:rw,cached
      - ./docker/php/conf.d/symfony.dev.ini:/usr/local/etc/php/conf.d/symfony.ini
      # If you develop on Mac you can remove the var/ directory from the bind-mount
      # for better performance by enabling the next line
      # - /srv/app/var
    environment:
      APP_ENV: dev
      DATABASE_URL: mysql://root:root@database:3306/app?serverVersion=8.0

  caddy:
    volumes:
      - ./docker/caddy/Caddyfile:/etc/caddy/Caddyfile:ro
      - ./public:/srv/app/public:ro
    environment:
      SERVER_NAME_PHPMYADMIN: ${SERVER_NAME_PHPMYADMIN:-phpmyadmin.localhost}

  ###> doctrine/doctrine-bundle ###
  database:
    image: 'mysql:8.0'
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: app
    ports:
      # To allow the host machine to access the ports below, modify the lines below.
      # For example, to allow the host to connect to port 3306 on the container, you would change
      # "3306" to "3306:3306". Where the first port is exposed to the host and the second is the container port.
      # See https://docs.docker.com/compose/compose-file/#ports for more information.
      - '3306'
  ###< doctrine/doctrine-bundle ###

  # phpmyadmin
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    environment:
      PMA_HOST: database
      PMA_PORT: 3306
      MYSQL_ROOT_PASSWORD: root
    depends_on:
      - database
    links:
      - database

###> symfony/mercure-bundle ###
###< symfony/mercure-bundle ###
@medusiora
Copy link
Author

I also tried to build the app with this template from API platform doc https://github.com/api-platform/api-platform
But I also got the same result

@medusiora
Copy link
Author

Update: It's also works on APP_ENV: prod but not for APP_ENV: dev.

@maxhelias
Copy link
Collaborator

Update: It's also works on APP_ENV: prod but not for APP_ENV: dev.

What did you mean ? "APP_ENV: prod" have the same result but not with "APP_ENV: dev" ?

@medusiora
Copy link
Author

Update: It's also works on APP_ENV: prod but not for APP_ENV: dev.

What did you mean ? "APP_ENV: prod" have the same result but not with "APP_ENV: dev" ?

I mean when I run on production mode all routes are show up but when I change to develop mode some routes are missing.

image

@michael-schaefer-eu
Copy link

Here some ideas, that you can check:

  • You have defined some routes under api/config/routes/prod so they are not available under the dev ENV ?
  • You have some cache configuration problem (apcu, doctrine autoload file etc) so that your changes are not reflected in cache updates (serving old files) ? ... you can try to clear cache
    • call doctrine dump-autoload in container console
    • call apcu_clear_cache() in a php file (via webserver , not console)

@maxhelias maxhelias added the support Support requested label Mar 28, 2022
@medusiora
Copy link
Author

Thanks for your help @michael-schaefer-eu
For option 1: I have no configuration file about dev or prod under api/config/routes/.
image

For option 2: I tried to run composer dump-autoload in php container console and tried to call apcu_clear_cache() in a php file, But it's doesn't make any different.

But I found something with command composer dump-autoload.
When I run composer dump-autoload in php container console, It fould only 5741 classes.
image

But when I run composer dump-autoload via composer in my machine, It fould 6194 classes.
image

I also tried to change apc config in docker\php\conf.d\symfony.dev.ini file
image

But still got the same response.

@denizen2244
Copy link

denizen2244 commented Mar 30, 2022

I have same issue too, pls help us !

@laryjulien
Copy link

Hi,

I have a few questions to determine what is happening

  1. Does the missing endpoints appears in bin/console debug:router (executed in php container)?
  2. Have you set opcache.validate_timestamps=0 in you php.ini production file?
  3. Do you have the same issue when uncommenting - /srv/app/var in docker-compose file in php/volumes section?
  4. Do you have a docker-entrypoint.sh file? Do you have composer commands? Executed depending of APP_ENV?

Thanks

@medusiora
Copy link
Author

Hi,

I have a few questions to determine what is happening

  1. Does the missing endpoints appears in bin/console debug:router (executed in php container)?
  2. Have you set opcache.validate_timestamps=0 in you php.ini production file?
  3. Do you have the same issue when uncommenting - /srv/app/var in docker-compose file in php/volumes section?
  4. Do you have a docker-entrypoint.sh file? Do you have composer commands? Executed depending of APP_ENV?

Thanks

@laryjulien Thanks for your help.

  1. Yes, It's missing in dev mode
  • actually it's also missing in prod mode. only first time when run docker-compose up with prod mode wil show all APIs but after I run php bin/console cache:clear in php container the routes are missing same as dev mode
  1. Yes, It's default one from this template

  2. Yes, It's the same

  3. Yes, I have docker-entrypoint.sh file and composer commands in php container. and yes it's it's executed depending of APP_ENV.

  • I tried to change only APP_ENV: to dev and prod in docker-compose.override.yml and run docker-compose down && docker-compose up --build and it's executed depending on this variable

@medusiora
Copy link
Author

I created this demo project with simple setup to check about this issue and I got the same response. You can check my repository in this link https://github.com/medusiora/symdony-docker

In my poject(real project) I have about ~60-70 Entities similar with this repository but more complex in term of relations and events

I used Windows 10 and Docker Desktop to run the application.

Thanks.

@laryjulien
Copy link

Thanks for providing a reproducer, I will check it.

@laryjulien
Copy link

Hi again,

I get all the 70 API endpoints when building and running your demo project.
I have just removed the database stuff to work with my Mac M1.

I will try to get a PC with Windows and Docker Desktop

@medusiora
Copy link
Author

I tried to run the app from different 4 machine in Windows OS but doesn't work for all of them. The routes are still missing (in case we have about 70 entries or more).
After that I tried to run the project with MAC M1 (from Sariz) - It's an error that can't connect the database.

So I tried to create new one from template (https://github.com/dunglas/symfony-docker) in MAC and just copy the entites from previous project (that created from Windows) - It's works fine, we see all 70 entities.

The project that was created on Windows 10:
https://github.com/medusiora/symfony-docker-2

  • Can run on Windows - Some APIs are missing
  • Can't run on MAC M1 - Can't connect the database.

The project that was created on MAC M1:
https://github.com/sariz-wachirasook/symfony-docker

  • Can run on Windows - Some APIs are missing
  • Can run on MAC M1 - See all entities

Nothing different in them of code between 2 repo.

@laryjulien
Copy link

laryjulien commented Apr 7, 2022

Seems related to filesystem/volumes handling with Windows. You should try to launch project without binding volumes (the volumes part of docker compose) and see if some routes are still missing.

Edit: Maybe, you can first try by removing the rw,cached options. See if it works, then removing the whole

@medusiora
Copy link
Author

Seems related to filesystem/volumes handling with Windows. You should try to launch project without binding volumes (the volumes part of docker compose) and see if some routes are still missing.

Edit: Maybe, you can first try by removing the rw,cached options. See if it works, then removing the whole

Many Thanks for your help, After changing the php volumes setting I found something

  1. When I remove rm,cached option in php service (docker-compose.override.yml) - Nothing changed
    image

  2. When I remove this line ./:/srv/app:rw,cached in php service (docker-compose.override.yml) - All routes are show up!
    image
    image

But when I try to make some new entity via command in php container it's doesn't sync files in container with my computer.
image
image
image

Directory in my computer
image

@laryjulien
Copy link

laryjulien commented Apr 22, 2022

So it seems really related to volumes.
By removing ./:/srv/app:rw,cached there will be no file sharing between your host (windows) and your container (php in docker). This particular instruction tells to bind all files in . relatively to your Docker Compose file (so your project root). That's why files which are added directly in container will not be copied to host and vice versa.
If you can try to keep this volumes binding and remove the var directory, this will maybe solve your problem. You can achieve this by uncommenting - /srv/app/var, this will tell Docker to mount all /srv/app to your host except for /srv/app/var directory.
You will end up with something like

volumes :
  - ./:/srv/app
  - /srv/app/var

To be cleaner, delete the var folder on windows before relaunching your containers

@medusiora
Copy link
Author

So it seems really related to volumes. By removing ./:/srv/app:rw,cached there will be no file sharing between your host (windows) and your container (php in docker). This particular instruction tells to bind all files in . relatively to your Docker Compose file (so your project root). That's why files which are added directly in container will not be copied to host and vice versa. If you can try to keep this volumes binding and remove the var directory, this will maybe solve your problem. You can achieve this by uncommenting - /srv/app/var, this will tell Docker to mount all /srv/app to your host except for /srv/app/var directory. You will end up with something like

volumes :
  - ./:/srv/app
  - /srv/app/var

To be cleaner, delete the var folder on windows before relaunching your containers

Thanks for your explanation!
I removed var folder and I changed volumes setting for php service :
image

And relaunching, I got the same response. (some APIs still missing)
image

I also try to run this project(https://github.com/medusiora/symfony-docker-2) on Pop!_OS 21 - It's working properly.

@maxhelias
Copy link
Collaborator

Thanks @laryjulien for the help :)

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

No branches or pull requests

5 participants