John
John

Reputation: 1665

Symfony docker configuration

I am trying to build a Docker image to run my Symfony3 app.

I have docker-composer.yml and site.conf

In my docker-composer I define two containers web and php and link these cntainers:

web:
   image: nginx:latest
   ports:
      - "80:80"
   volumes:
      - ./code/test:/var/www/test
      - ./site.conf:/etc/nginx/conf.d/default.conf
   links:
      - php
php:
   image: php:7-fpm
   volumes:
      - ./code/test:/var/www/test

I found offical nginx docs on how to set up my site.conf:

my site.conf:

server {
    server_name test-docker.local;
    root /code/test/web;

    location / {
        try_files $uri /app.php$is_args$args;
    }
    # DEV
    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass php:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }
    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass php:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
   }

   # return 404 for all other php files not matching the front controller
   # this prevents access to other php files you don't want to be accessible.

   error_log /var/log/nginx/project_error.log;
   access_log /var/log/nginx/project_access.log;
}

In the same dir that i have my docker.compose.yml and site.conf I have a code dir and inside of code dir i have test dir and inside test die its my php application.

The problem I have is that when i go my root url test-docker.local/

i get File Not Found

I entered the php container to see if the code got copied into the correct path and it is /var/www/test/

when I enter my web container the code id under the same path /var/www/test but is that the correct path for nginx container is that were nginx loads the code from...? and even if so why does nginx container has to have a copy of the code if in theory the code should be loaded from php container?

Upvotes: 2

Views: 571

Answers (2)

emarref
emarref

Reputation: 1311

Your Nginx config is pointing the root at root /code/test/web; but it seems to me like that path is only available on your docker host, not in the docker container. Your docker-compose.yml file mounts ./code/test at /var/www/test in the container, so try using root /var/www/test/web; in your nginx config.

Upvotes: 1

staskrak
staskrak

Reputation: 863

  1. You don't need repeat in both containers:

    volumes:
         - ./code/test:/var/www/test
    

    So in nginx container you could omit this volume

  2. Replace in your site.conf:

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

    with

    location / {
        try_files $uri @rewriteapp;
    }
    
    location @rewriteapp {
        rewrite ^(.*)$ /app_dev.php/$1 last; # dev mode
        # rewrite ^(.*)$ /app.php/$1 last; # prod mode
    }
    

Upvotes: 0

Related Questions