api-platform / api-platform

🕸️ Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.
https://api-platform.com
MIT License
8.56k stars 960 forks source link

Can't run api platform with local database #2779

Open victoria557 opened 3 days ago

victoria557 commented 3 days ago

API Platform version(s) affected: 3.2

Description
I try to run api platform on production without database service on docker, because I want to use a local database. I start the project with the command provided in your docs, bu I run it with a specific compose.yaml and a compose.prod.yaml. I added to compose.prod.yaml file network_mode: "host" so I bring to the container the same networking as my host.

But when I run the project, I get this error :

"duration":0.013162129
"err_id":"y7f98si24"
"err_trace":"reverseproxy.statusError (reverseproxy.go:1267)"
"level":"error"
"logger":["http.log.error.log0"](https://logs.betterstack.com/team/249665/http.log.error.log0)
"msg":"dial tcp: lookup pwa on 212.227.123.16:53: no such host"

From what I understand, it doesn't find pwa host. I can't understand why.

How to reproduce

I run this command to starting api platform on production :

SERVER_NAME=preprod.website.fr APP_SECRET=a_secret_key CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey docker compose -f compose.yaml -f compose.prod.yaml up --wait

compose.yaml :

services:
  php:
    image: ${IMAGES_PREFIX:-}app-php   
    restart: unless-stopped
    environment:
      PWA_UPSTREAM: pwa:3000
      SERVER_NAME: ${SERVER_NAME:-localhost}, php:80
      MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercure>
      MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercur>
      TRUSTED_PROXIES: ${TRUSTED_PROXIES:-127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,>
      TRUSTED_HOSTS: ^${SERVER_NAME:-example\.com|localhost}|php$$
      DATABASE_URL: mysql://${MARIADB_USER:-myuser}:${MARIADB_PASSWORD>
      MERCURE_URL: ${CADDY_MERCURE_URL:-http://php/.well-known/mercure}
      MERCURE_PUBLIC_URL: https://${SERVER_NAME:-localhost}/.well-known/mercure
      MERCURE_JWT_SECRET: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTS>
    volumes:
      - 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
   pwa:
    image: ${IMAGES_PREFIX:-}app-pwa
    environment:
      NUXT_PUBLIC_ENTRYPOINT: http://php

# Mercure is installed as a Caddy module, prevent the Flex recipe from installi>
###> symfony/mercure-bundle ###
###< symfony/mercure-bundle ###
volumes:
  caddy_data:
  caddy_config:
###> symfony/mercure-bundle ###
###< symfony/mercure-bundle ###

compose.prod.yaml :

# Production environment override
services:
  php:
    build:
      context: ./api
      target: frankenphp_prod
    environment:
      APP_SECRET: ${APP_SECRET}
      MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET}
      MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET}
      DATABASE_URL: mysql://${MARIADB_USER:-myuser}:${MARIADB_PASSWORD:-mypassword}@127.0.0.1:3306/${MARIADB_DATABASE:-mydatabase}?serverVersion=mariadb-${MARIADB_VERSION:-10.11>
    network_mode: "host"
  pwa:
    build:
      context: ./pwa
      target: prod

  vector:
    container_name: vector
    image: timberio/vector:0.34.0-alpine
    restart: always
    volumes:
      - ./vector.yaml:/etc/vector/vector.yaml
      - /var/run/docker.sock:/var/run/docker.sock:ro    
ngrafa commented 2 days ago

I think 212.227.123.16:53 is the host's DNS resolver (as port 53 is for DNS resolution). It can't resolve host pwa as it's no longer using Docker's internal DNS resolver.

If you don't really need network_mode: "host" and your database instance is running on the host, you can use the docker IP address (something like 172.17.0.1) as part of your database URL to the host from the docker network.