Docker compose: connections between multiple projects

1. The problem I’m having:

I am using 2 different docker compose projects.

1.backend.localhost
2.frontend.localhost

The one I want to send request from frontend to backend but it failed.

2. Error messages and/or full log output:

docker compose exec php curl --location --request GET ‘https://backend.localhost/api/test

curl: (7) Failed to connect to backend.localhost port 443 after 0 ms: Couldn’t connect to server

*** curl request wokrs with Postman ***

ttp-frontend-en-caddy-1  | {"level":"debug","ts":1684494691.0470858,"logger":"http.handlers.rewrite","msg":"rewrote request","request":{"remote_ip":"172.21.0.1","remote_port":"47350","proto":"HTTP/2.0","method":"GET","host":"frontend.localhost:4433","uri":"/","headers":{"Accept-Language":["en-US,en;q=0.9"],"Sec-Ch-Ua":["\"Chromium\";v=\"112\", \"Google Chrome\";v=\"112\", \"Not:A-Brand\";v=\"99\""],"Sec-Ch-Ua-Mobile":["?0"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-User":["?1"],"Sec-Fetch-Dest":["document"],"Accept-Encoding":["gzip, deflate, br"],"Cache-Control":["max-age=0"],"Sec-Ch-Ua-Platform":["\"Linux\""],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"],"Sec-Fetch-Site":["cross-site"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"frontend.localhost"}},"method":"GET","uri":"/index.php"}
ttp-frontend-en-caddy-1  | {"level":"debug","ts":1684494691.0472257,"logger":"http.handlers.reverse_proxy","msg":"selected upstream","dial":"/var/run/php/php-fpm.sock","total_upstreams":1}
ttp-frontend-en-caddy-1  | {"level":"debug","ts":1684494691.0478456,"logger":"http.reverse_proxy.transport.fastcgi","msg":"roundtrip","request":{"remote_ip":"172.21.0.1","remote_port":"47350","proto":"HTTP/2.0","method":"GET","host":"frontend.localhost:4433","uri":"/index.php","headers":{"User-Agent":["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"],"Sec-Fetch-Dest":["document"],"Accept-Encoding":["gzip, deflate, br"],"X-Forwarded-Proto":["https"],"Sec-Ch-Ua-Platform":["\"Linux\""],"Accept-Language":["en-US,en;q=0.9"],"Sec-Fetch-Mode":["navigate"],"X-Forwarded-For":["172.21.0.1"],"Sec-Ch-Ua-Mobile":["?0"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"],"Sec-Fetch-Site":["cross-site"],"Cache-Control":["max-age=0"],"Upgrade-Insecure-Requests":["1"],"Sec-Ch-Ua":["\"Chromium\";v=\"112\", \"Google Chrome\";v=\"112\", \"Not:A-Brand\";v=\"99\""],"Sec-Fetch-User":["?1"],"X-Forwarded-Host":["frontend.localhost:4433"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"frontend.localhost"}},"env":{"HTTP_ACCEPT":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","HTTP_SEC_FETCH_SITE":"cross-site","HTTP_UPGRADE_INSECURE_REQUESTS":"1","QUERY_STRING":"","SERVER_NAME":"frontend.localhost","HTTP_ACCEPT_ENCODING":"gzip, deflate, br","HTTP_HOST":"frontend.localhost:4433","HTTP_X_FORWARDED_PROTO":"https","HTTP_ACCEPT_LANGUAGE":"en-US,en;q=0.9","HTTP_SEC_CH_UA":"\"Chromium\";v=\"112\", \"Google Chrome\";v=\"112\", \"Not:A-Brand\";v=\"99\"","HTTP_X_FORWARDED_HOST":"frontend.localhost:4433","REMOTE_USER":"","REQUEST_METHOD":"GET","DOCUMENT_URI":"/index.php","REMOTE_HOST":"172.21.0.1","SCRIPT_NAME":"/index.php","HTTP_USER_AGENT":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","HTTPS":"on","SSL_CIPHER":"TLS_AES_128_GCM_SHA256","HTTP_SEC_FETCH_USER":"?1","GATEWAY_INTERFACE":"CGI/1.1","REMOTE_IDENT":"","REQUEST_URI":"/","SSL_PROTOCOL":"TLSv1.3","HTTP_SEC_FETCH_DEST":"document","PATH_INFO":"","REMOTE_PORT":"47350","SERVER_PORT":"4433","SERVER_SOFTWARE":"Caddy/v2.6.4","HTTP_SEC_FETCH_MODE":"navigate","HTTP_SEC_CH_UA_MOBILE":"?0","DOCUMENT_ROOT":"/srv/app/public","SCRIPT_FILENAME":"/srv/app/public/index.php","HTTP_SEC_CH_UA_PLATFORM":"\"Linux\"","HTTP_X_FORWARDED_FOR":"172.21.0.1","AUTH_TYPE":"","REQUEST_SCHEME":"https","SERVER_PROTOCOL":"HTTP/2.0","HTTP_CACHE_CONTROL":"max-age=0","CONTENT_LENGTH":"","CONTENT_TYPE":"","REMOTE_ADDR":"172.21.0.1"},"dial":"/var/run/php/php-fpm.sock","env":{"CONTENT_LENGTH":"","CONTENT_TYPE":"","REMOTE_ADDR":"172.21.0.1","HTTP_CACHE_CONTROL":"max-age=0","QUERY_STRING":"","SERVER_NAME":"frontend.localhost","HTTP_ACCEPT_ENCODING":"gzip, deflate, br","HTTP_ACCEPT":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","HTTP_SEC_FETCH_SITE":"cross-site","HTTP_UPGRADE_INSECURE_REQUESTS":"1","HTTP_X_FORWARDED_HOST":"frontend.localhost:4433","REMOTE_USER":"","REQUEST_METHOD":"GET","DOCUMENT_URI":"/index.php","HTTP_HOST":"frontend.localhost:4433","HTTP_X_FORWARDED_PROTO":"https","HTTP_ACCEPT_LANGUAGE":"en-US,en;q=0.9","HTTP_SEC_CH_UA":"\"Chromium\";v=\"112\", \"Google Chrome\";v=\"112\", \"Not:A-Brand\";v=\"99\"","REMOTE_HOST":"172.21.0.1","SCRIPT_NAME":"/index.php","HTTP_USER_AGENT":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36","GATEWAY_INTERFACE":"CGI/1.1","REMOTE_IDENT":"","REQUEST_URI":"/","HTTPS":"on","SSL_CIPHER":"TLS_AES_128_GCM_SHA256","HTTP_SEC_FETCH_USER":"?1","PATH_INFO":"","REMOTE_PORT":"47350","SERVER_PORT":"4433","SSL_PROTOCOL":"TLSv1.3","HTTP_SEC_FETCH_DEST":"document","SERVER_SOFTWARE":"Caddy/v2.6.4","HTTP_SEC_FETCH_MODE":"navigate","HTTP_SEC_CH_UA_MOBILE":"?0","AUTH_TYPE":"","REQUEST_SCHEME":"https","SERVER_PROTOCOL":"HTTP/2.0","DOCUMENT_ROOT":"/srv/app/public","SCRIPT_FILENAME":"/srv/app/public/index.php","HTTP_SEC_CH_UA_PLATFORM":"\"Linux\"","HTTP_X_FORWARDED_FOR":"172.21.0.1"},"request":{"remote_ip":"172.21.0.1","remote_port":"47350","proto":"HTTP/2.0","method":"GET","host":"frontend.localhost:4433","uri":"/index.php","headers":{"Sec-Ch-Ua-Platform":["\"Linux\""],"Accept-Language":["en-US,en;q=0.9"],"Sec-Fetch-Mode":["navigate"],"X-Forwarded-For":["172.21.0.1"],"Sec-Ch-Ua":["\"Chromium\";v=\"112\", \"Google Chrome\";v=\"112\", \"Not:A-Brand\";v=\"99\""],"Sec-Ch-Ua-Mobile":["?0"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"],"Sec-Fetch-Site":["cross-site"],"Cache-Control":["max-age=0"],"Upgrade-Insecure-Requests":["1"],"Sec-Fetch-User":["?1"],"X-Forwarded-Host":["frontend.localhost:4433"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"],"Sec-Fetch-Dest":["document"],"Accept-Encoding":["gzip, deflate, br"],"X-Forwarded-Proto":["https"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"frontend.localhost"}}}
ttp-frontend-en-caddy-1  | {"level":"debug","ts":1684494691.1786916,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"unix//var/run/php/php-fpm.sock","duration":0.131376496,"request":{"remote_ip":"172.21.0.1","remote_port":"47350","proto":"HTTP/2.0","method":"GET","host":"frontend.localhost:4433","uri":"/index.php","headers":{"Sec-Ch-Ua-Platform":["\"Linux\""],"Accept-Language":["en-US,en;q=0.9"],"Sec-Fetch-Mode":["navigate"],"X-Forwarded-For":["172.21.0.1"],"Sec-Ch-Ua-Mobile":["?0"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"],"Sec-Fetch-Site":["cross-site"],"Cache-Control":["max-age=0"],"Upgrade-Insecure-Requests":["1"],"Sec-Ch-Ua":["\"Chromium\";v=\"112\", \"Google Chrome\";v=\"112\", \"Not:A-Brand\";v=\"99\""],"Sec-Fetch-User":["?1"],"X-Forwarded-Host":["frontend.localhost:4433"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"],"Sec-Fetch-Dest":["document"],"Accept-Encoding":["gzip, deflate, br"],"X-Forwarded-Proto":["https"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"frontend.localhost"}},"headers":{"Status":["500 Internal Server Error"],"Content-Type":["text/html; charset=UTF-8"],"X-Debug-Exception":["Failed%20to%20connect%20to%20backend.localhost%20port%20443%20after%200%20ms%3A%20Couldn%27t%20connect%20to%20server%20for%20%22https%3A%2F%2Fbackend.localhost%2Fapi%2Ftest%22."],"X-Debug-Exception-File":["%2Fsrv%2Fapp%2Fvendor%2Fsymfony%2Fhttp-client%2FResponse%2FCommonResponseTrait.php:144"]},"status":500}
ttp-frontend-en-caddy-1  | {"level":"error","ts":1684494691.180841,"logger":"http.log.access","msg":"handled request","request":{"remote_ip":"172.21.0.1","remote_port":"47350","proto":"HTTP/2.0","method":"GET","host":"frontend.localhost:4433","uri":"/","headers":{"Cache-Control":["max-age=0"],"Sec-Ch-Ua-Platform":["\"Linux\""],"Upgrade-Insecure-Requests":["1"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"],"Sec-Fetch-Site":["cross-site"],"Sec-Ch-Ua":["\"Chromium\";v=\"112\", \"Google Chrome\";v=\"112\", \"Not:A-Brand\";v=\"99\""],"Sec-Ch-Ua-Mobile":["?0"],"Sec-Fetch-Mode":["navigate"],"Sec-Fetch-User":["?1"],"Sec-Fetch-Dest":["document"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-US,en;q=0.9"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"frontend.localhost"}},"user_id":"","duration":0.134292483,"size":276043,"status":500,"resp_headers":{"Content-Type":["text/html; charset=UTF-8"],"X-Debug-Exception":["Failed%20to%20connect%20to%20backend.localhost%20port%20443%20after%200%20ms%3A%20Couldn%27t%20connect%20to%20server%20for%20%22https%3A%2F%2Fbackend.localhost%2Fapi%2Ftest%22."],"X-Debug-Exception-File":["%2Fsrv%2Fapp%2Fvendor%2Fsymfony%2Fhttp-client%2FResponse%2FCommonResponseTrait.php:144"],"Server":["Caddy"],"Alt-Svc":["h3=\":443\"; ma=2592000"],"Status":["500 Internal Server Error"]}}
ttp-frontend-en-php-1    | - -  19/May/2023:11:11:31 +0000 "GET /index.php" 500

3. Caddy version:

`v2.6.4

4. How I installed and ran Caddy:

a. System environment:

Docker

b. Command:

docker compose build --no-cache

it is in docker-compose.yml
caddy:
    command: ["caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile", "--watch"]

c. My complete Caddy config and docker-compose.yml:

I set SERVER_NAME=frontend.localhost in .env

and set getway in /etc/hosts
172.20.0.1 backend.localhost
172.21.0.1 frontend.localhost

{
    debug
    {$CADDY_DEBUG}
}

{$SERVER_NAME}

{$CADDY_EXTRA_CONFIG}

log

route {
    root * /srv/app/public
    php_fastcgi unix//var/run/php/php-fpm.sock
    encode zstd gzip
    file_server browse
}

version: "3.4"

networks:
  front:
    ipam:
      driver: default
      config:
        -   subnet: 172.21.0.0/24
services:
  php:
    build:
      context: .
      target: app_php
      args:
        SYMFONY_VERSION: ${SYMFONY_VERSION:-}
        STABILITY: ${STABILITY:-stable}
    restart: unless-stopped
    networks:
      - front
    volumes:
      - php_frontend_en_socket:/var/run/php
    healthcheck:
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 30s
    environment:
      APP_ENV: ${APP_ENV:-}
    ports:
      - "9001:9000"
      
  caddy:
    build:
      context: .
      target: app_caddy
    depends_on:
      - php
    environment:
      SERVER_NAME: ${SERVER_NAME:-localhost, caddy:80}
      ACME_AGREE: true
    restart: unless-stopped
    networks:
      - front
    volumes:
      - php_frontend_en_socket:/var/run/php
      - caddy_frontend_en_data:/data
      - caddy_frontend_en_config:/config
    ports:
      - "8000:80"
      - "4433:443"
    extra_hosts:
      - "backend.localhost:172.20.0.1"
      - "backend.localhost:172.20.0.6"
      - "backend.localhost:host-gateway"
      - "host.docker.internal:host-gateway"

volumes:
  php_frontend_en_socket:
  caddy_frontend_en_data:
  caddy_frontend_en_config:

5. Links to relevant resources:

I used this configuration basis of dunglas/symfony-docker for backend and frontend:

That’s more of a Docker networking question than a Caddy question. You might get better help from Docker forums about that, I’m not a Docker networking expert.

Keep in mind that “localhost” in Docker means “this same container”.

Using /etc/hosts for *.localhost is not ideal because many clients now hard-code localhost as ::1 or 127.0.0.1. It would be better to use something like *.test which is also reserved and has no specific predefined use.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.