DEV Community

Burhanuddin Ahmed
Burhanuddin Ahmed

Posted on • Edited on

Chapter 2: Dokerize Laravel App with Nginx and Postgres.

Nginx

So previously we have set our app Dockerfile and docker-compose.

Now we will set up our Nginx docker-compose.

For this part, we don't want to set up Dockerfile for Nginx image, because we don't want to customize Nginx image.

At this point, we don't need to create a custom Nginx Dockerfile. We can use the Nginx image from Docker, nginx:1.19.8-alpine.

Before we head to the docker-compose file, first we need to create Nginx conf to configure our web server.

Go to ./infra/nginx/conf and create app.conf, we can name it anything as long as it's ended with .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 app: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;
    }
}
Enter fullscreen mode Exit fullscreen mode

You can modify it later, for example if you want to add SSL or something else.

Now we head to our docker-compose.yml file.


version: "3.7"

networks:
  app-network:
    driver: bridge

services: 
  app:
    container_name: app
    build:
      context: ./infra/app
      dockerfile: Dockerfile
    image: php-laravel-7.0
    restart: unless-stopped
    tty: true
    working_dir: /var/www
    volumes: 
      - ./:/var/www
    networks: 
      - app-network

  nginx:
    image: nginx:1.19.8-alpine
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports: 
        - 8100:80
    volumes: 
        - ./infra/nginx/conf:/etc/nginx/conf.d
    networks: 
        - app-network
Enter fullscreen mode Exit fullscreen mode

In port we can see 8100:80, We expose our Nginx in port 80 and expose it to the public using port 8100. So we can access our Laravel app via Nginx using localhost:8100.

And also we copy again our Nginx conf to the container Nginx folder.

...continue...

Top comments (0)