skip to Main Content

I have problems to configure docker with two symfony apps under docker:

  • front (symfony)
  • backend (symfony)
  • nginx

When I use my navigator, i can access to http://dev.front.local and http://dev.backend.local but i have to call backend from front, and it doesn’t work, dev.backend.local is not recognized (I have symfony error : Couldn’t connect to server for “http://dev.backend.local“).

How can i force front nginx to “see” backend url ? I see nginx proxy but I don’t know how to do that in my case, and if it is really mandatory.

Excuse me for my bad english…

Thanks

docker-compose.yaml :

version: '3.7'

services:
  mysql:
    build:
      context: .
      dockerfile: docker/mariadb/Dockerfile
    ports:
      - '13306:3306'
    restart: always
    volumes:
      - './database/:/var/lib/mysql'
    networks:
      api_network:
        ipv4_address: 172.28.1.4

  php:
    container_name: backend
    build:
      context: .
      dockerfile: docker/backend/Dockerfile
    env_file:
      - .env
    ports:
      - '9001:9001'
    links:
      - mysql:db
    volumes:
      - './app/backend/:/var/www/app/backend:cached'
      - './logs/xdebug:/var/log/xdebug:cached'
    networks:
      api_network:
        ipv4_address: 172.28.1.1

  front:
    container_name: front-web
    build:
      context: .
      dockerfile: docker/front/Dockerfile
    env_file:
      - .env
    ports:
      - '9002:9002'
    links:
      - mysql:db
    volumes:
      - './app/front/:/var/www/app/front'
    networks:
      api_network:
        ipv4_address: 172.28.1.6


  nginx:
    container_name: nginx
    build: ./docker/nginx
    ports:
      - '80:80'
    links:
      - php
      - phpmyadmin
      - front
    volumes:
      - './docker/nginx/conf:/etc/nginx/conf.d/:cached'
      - './app/backend/:/var/www/app/backend:cached'
      - './app/front/:/var/www/app/front:cached'
    depends_on:
      - php
      - front
    networks:
      api_network:
        ipv4_address: 172.28.1.2

  phpmyadmin:
    container_name: phpmyadmin
    image: phpmyadmin/phpmyadmin
    ports:
      - '8080:80'
    links:
      - mysql:db
    environment:
      MYSQL_ROOT_PASSWORD: ****
    networks:
      api_network:
        ipv4_address: 172.28.1.3

networks:
  api_network:
    ipam:
      driver: default
      config:
        - subnet: 172.28.0.0/16

nginx front.conf :

server {
   listen 80;
   listen [::]:80;

   server_name dev.front.local;
   root /var/www/app/front/public;

   location / {
        try_files $uri /index.php$is_args$args;
   }

   location ~ ^/.+.php(/|$) {
      fastcgi_pass php-front;
      fastcgi_split_path_info ^(.+.php)(/.*)$;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
      fastcgi_param DOCUMENT_ROOT $realpath_root;
      internal;
   }

   location ~ .php$ {
       return 404;
       #error_page 404 /404_error.html;
   }

   error_log /var/log/nginx/front-error.log;
   access_log /var/log/nginx/front-access.log;
}

nginx back.conf :

server {
   listen 80;
   listen [::]:80;

   server_name dev.backend.local;
   root /var/www/app/backend/public;

   location / {
      try_files $uri /index.php$is_args$args;
   }

   location ~ ^/.+.php(/|$) {
      fastcgi_pass php-upstream;
      fastcgi_split_path_info ^(.+.php)(/.*)$;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
      fastcgi_param DOCUMENT_ROOT $realpath_root;
      internal;
   }

   error_log /var/log/nginx/backend-error.log;
   access_log /var/log/nginx/backend-access.log;
}

nginx.conf

user www;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 2048;
    multi_accept on;
    use epoll;
}

http {
    upstream php-upstream {
        server php:9001 max_fails=3 fail_timeout=30;
    }

    upstream php-front {
        server front:9002 max_fails=3 fail_timeout=30;
    }

    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 15;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log off;
    error_log off;
    gzip on;
    gzip_disable "msie6";
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
    open_file_cache max=100;
    client_body_temp_path /tmp 1 2;
    client_body_buffer_size 256k;
    client_body_in_file_only off;
}

daemon off;

2

Answers


  1. internally docker in her sub network make aliases to each container that make posible comunicate between container because the ips of containers are dinamic. if you want make a requet of your front to you back container the route is : http://php or http://backend because u are using container_name: backend

    Login or Signup to reply.
  2. Docker-compose can make services communicante through their name (php for your backend, front for your frontend, etc…) si if you want to your service nginx communicate with your backend, you must usé the name of service. (its like Docker-compose fill your /etc/host with service names).

    For nginx, you must write fastcgi-pass php and fastcgi-pass front.

    The communication is not relative to container name but with service name (with Docker-compose)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search