skip to Main Content

I was creating docker container for laravel with postgres. containers running but cant find laravel in web.

Dockerfile:

FROM php:7.4-fpm-alpine

RUN docker-php-ext-install pdo pdo_pgsql

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN composer install

WORKDIR /var/www/html
COPY . .

CMD php artisan serve --host=0.0.0.0

EXPOSE 8000

My docker-compose.yml file

version: '3'

services:
  php_3:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./:/var/www/html
    ports:
      - "8000:8000"

  postgres_3:
    image: postgres:12.3-alpine
    restart: unless-stopped
    ports:
      - "5431:5432"
    volumes:
      - ./docker/postgres:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_DB: lara_3

Without error both services are running but cant find laravel runnning in browser. what i want to change.please help me to fix this.

2

Answers


  1. In Dockerfile instead of

    php artisan serve --host=0.0.0.0

    write:

    php artisan serve --host=0.0.0.0 --port=80

    and EXPOSE 80

    Login or Signup to reply.
  2. When you install php-fpm what you get is a server process, and then you need to config nginx to forward all requests to php files to be parsed by this php-fpm process.

    Alternatively you can install php-apache docker image that include the apache server.

    docker-compose.yml

    version: "3"
    
    services:
      php_3:
        build:
          context: .
          dockerfile: Dockerfile
        volumes:
          - ./:/var/www/html
      postgres_3:
        image: postgres:12.3-alpine
        restart: unless-stopped
        ports:
          - "5431:5432"
        volumes:
          - ./docker/postgres:/var/lib/postgresql/data
        environment:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: root
          POSTGRES_DB: lara_3
      nginx:
        image: nginx:alpine
        restart: unless-stopped
        ports:
          - "8000:80"
        volumes:
          - ./:/var/www
          - ./:/etc/nginx/conf.d
    

    Dockerfile

    FROM php:7.4-fpm
    
    RUN docker-php-ext-install pdo_mysql
    
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
    
    WORKDIR /var/www/html
    

    conf/nginx.conf

    server {
        listen 80;
        index index.php index.html;
        error_log  /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        root /var/www/public;
        location ~ .php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+.php)(/.+)$;
            fastcgi_pass php_3:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
        location / {
            try_files $uri $uri/ /index.php?$query_string;
            gzip_static on;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search