dunglas / symfony-docker

A Docker-based installer and runtime for Symfony. Install: download and `docker compose up`.
https://dunglas.dev/2021/12/symfonys-new-native-docker-support-symfony-world/
2.55k stars 759 forks source link

[support] Possible to move to a subfolder ? #660

Closed ssj17vegeta closed 3 weeks ago

ssj17vegeta commented 3 weeks ago

I'm currently trying to setup a dockerized Symfony application in a wider project containing a single-page application.

I have moved the contents of the symfony-docker project inside a subfolder named "backend" (since there will be other folders such as "frontend" for my SPA), and moved the compose.yml files at the root of the project.

I have modified the contents of the compose.yml files to reflect this change :

services:
  frontend:
    image: myproject-front
    build: ./frontend
    volumes:
      - ./frontend/:/app/
    ports:
      - "8100:8100"
  backend:
    build:
      context: backend
    image: myproject-back
    restart: unless-stopped
    environment:
      SERVER_NAME: ${SERVER_NAME:-localhost}, php:80
      MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
      MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
      # Run "composer require symfony/orm-pack" to install and configure Doctrine ORM
      DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-!ChangeMe!}@database:5432/${POSTGRES_DB:-app}?serverVersion=${POSTGRES_VERSION:-16}&charset=${POSTGRES_CHARSET:-utf8}
      # Run "composer require symfony/mercure-bundle" to install and configure the Mercure integration
      MERCURE_URL: ${CADDY_MERCURE_URL:-http://php/.well-known/mercure}
      MERCURE_PUBLIC_URL: ${CADDY_MERCURE_PUBLIC_URL:-https://${SERVER_NAME:-localhost}/.well-known/mercure}
      MERCURE_JWT_SECRET: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
      # The two next lines can be removed after initial installation
      SYMFONY_VERSION: ${SYMFONY_VERSION:-}
      STABILITY: ${STABILITY:-stable}
    volumes:
      - caddy_data:/data
      - caddy_config:/config
      - ./backend:/app
    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 ###

###> doctrine/doctrine-bundle ###
  database:
    image: postgres:${POSTGRES_VERSION:-16}-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-app}
      # You should definitely change the password in production
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!}
      POSTGRES_USER: ${POSTGRES_USER:-app}
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD", "pg_isready", "-d", "${POSTGRES_DB:-app}", "-U", "${POSTGRES_USER:-app}"]
      timeout: 5s
      retries: 5
      start_period: 60s
    volumes:
      - database_data:/var/lib/postgresql/data:rw
      # You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
      # - ./docker/db/data:/var/lib/postgresql/data:rw
###< doctrine/doctrine-bundle ###

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

###> doctrine/doctrine-bundle ###
  database_data:
###< doctrine/doctrine-bundle ###

Long story short, the database container works fine (I had to expose ports though), the server launches fine, php bin/console commands work fine, but every request gives a 404 :

[error] Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET https://localhost/"" at RouterListener.php line 149       {"syslog_level": "notice"}
backend-1   | 2024/09/02 22:43:14.800   INFO    http.log.access.log0    handled request {"request": {"remote_ip": "172.20.0.1", "remote_port": "57336", "client_ip": "172.20.0.1", "proto": "HTTP/2.0", "method": "GET", "host": "localhost", "uri": "/", "headers": {"Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-User": ["?1"], "Priority": ["u=0, i"], "Accept-Language": ["fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"], "Upgrade-Insecure-Requests": ["1"], "Accept-Encoding": ["gzip, deflate, br, zstd"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-Site": ["none"], "Te": ["trailers"], "User-Agent": ["Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/png,image/svg+xml,*/*;q=0.8"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "localhost"}}, "bytes_read": 0, "user_id": "", "duration": 0.0022853, "size": 669, "status": 404, "resp_headers": {"Alt-Svc": ["h3=\":443\"; ma=2592000"], "Content-Type": ["text/html; charset=UTF-8"], "Cache-Control": ["no-cache, private"], "Date": ["Mon, 02 Sep 2024 22:43:14 GMT"], "Content-Encoding": ["zstd"], "Vary": ["Accept-Encoding"], "Permissions-Policy": ["browsing-topics=()"], "Server": ["Caddy"]}

I know I could probably have completely separate docker-compose projects, modify the backend compose.yml to set a network and launch the frontend afterwards by settings the network as external, but I'm almost sure this can be done in compose.yml file. Any idea how ?

dunglas commented 3 weeks ago

Take a look to https://github.com/api-platform/api-platform

It's an adaptation of this skeleton for a similar use case (SPA + Symfony app in sub folders).

maxhelias commented 3 weeks ago

If you haven't created a controller / route, your 404 error shows that it's working

ssj17vegeta commented 3 weeks ago

Thanks a bunch for the link @dunglas , that project seems indeed to do exactly what I intended.

@maxhelias That's what I was thinking at first, but when I launched the project "alone", I was greeted with the Symfony debug home page and not a 404. That difference in behaviour was the reason I looked for mistakes on my part in the compose.yml file and eventually reached out to you guys. Thanks anyway ;)