Home » Php » php – How to make API calls within docker environment?

php – How to make API calls within docker environment?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am building an app wrapped in docker, that consist of PHP backend (“API”) and NODE frontend, they are united by NGINX, where php app is served by the means of php-fpm and my node app is served by the reverse proxy. NGINX exposes phpMyAdmin app (phpmyadmin.test) and “API” (api.php.test) for dev purposes and NODE api (nodeapp.test).

NODE apps SSR (“Server-Side Rendering”) needs to fetch some data from an API within docker the network, and because domains such as api.php.test can’t be recognized from within docker I have to make calls to NGINX container which serves 3 different domains mentioned above, so I either need to fake ‘HOST’ header to get appropriate response from an API via NGINX which leads problems. Such as: Refused to set unsafe header "Host", Error: unable to verify the first certificate in nodejs etc.

Do I have to spin up Nginx container for each endpoint to avoid these issues? Or is there a better way to go around this?
Here is an example of my docker-compose.yml to give you a better idea of what happens in my app.

version: "3.7"
services:
  workspace:
    build:
      context: workspace
      args:
        WORKSPACE_USER: ${WORKSPACE_USER}
    volumes:
      - api:/var/www/api
      - site:/var/www/site
    ports:
      - "2222:22"
    environment:
      S3_KEY: ${S3_KEY}
      S3_SECRET: ${S3_SECRET}
      S3_BUCKET: ${S3_BUCKET}
      DB_CONNECTION: ${DB_CONNECTION}
      MYSQL_HOST: ${MYSQL_HOST}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MEDIA_LIBRARY_ENDPOINT_TYPE: ${MEDIA_LIBRARY_ENDPOINT_TYPE}
      MEDIA_LIBRARY_IMAGE_SERVICE: ${MEDIA_LIBRARY_IMAGE_SERVICE}
    tty: true

  php-fpm:
    build:
      context: ./php-fpm
    depends_on:
      - nodejs
    volumes:
      - api:/var/www/api
      - ./certs:/certs
    environment:
      S3_KEY: ${S3_KEY}
      S3_SECRET: ${S3_SECRET}
      S3_BUCKET: ${S3_BUCKET}
      DB_CONNECTION: ${DB_CONNECTION}
      MYSQL_HOST: ${MYSQL_HOST}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MEDIA_LIBRARY_ENDPOINT_TYPE: ${MEDIA_LIBRARY_ENDPOINT_TYPE}
      MEDIA_LIBRARY_IMAGE_SERVICE: ${MEDIA_LIBRARY_IMAGE_SERVICE}

  nodejs:
    build:
      context: ./nodejs
      args:
        NODEJS_SITE_PATH: ${NODEJS_SITE_PATH}
        NODEJS_VER: ${NODEJS_VER}
    volumes:
      - site:${NODEJS_SITE_PATH}
      - ./certs:/certs
    environment:
      NODEJS_ENV: ${NODEJS_ENV}
    ports:
      - 3000:3000
      - 3001:3001

  nginx:
    build:
      context: nginx
    depends_on:
      - php-fpm
      - mariadb
    restart: always
    volumes:
      - api:/var/www/api
      - site:/var/www/site
      - ./nginx/global:/etc/nginx/global
      - ./nginx/sites:/etc/nginx/sites-available
      - ./nginx/logs:/var/log/nginx
      - ./certs:/certs
    ports:
      - 80:80
      - 443:443

  mariadb:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - db:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - mariadb
    restart: always
    environment:
      PMA_HOST: ${MYSQL_HOST}
      PMA_USER: root
      PMA_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      UPLOAD_LIMIT: 2048M

volumes:
  phpmyadmin:
  db:
  site:
    external: true
  api:
    external: true
How to&Answers: