编辑1

编辑1

我正在尝试为 Laravel 应用程序启动 docker 容器:-

编辑

Dockerfile.nginx

FROM nginx:alpine

COPY ./nginx/my-site-config.conf /etc/nginx/conf.d/default.conf

nginx/my-site-config.conf

server {
 listen       8027;
 server_name  localhost;

 # handle .php
 location ~ \.php$ {
    # 404
    try_files                     $fastcgi_script_name =404;

    # default fastcgi_params
    include                       fastcgi_params;

    # fastcgi settings
    fastcgi_pass                  php-fpm-container:9000;
    fastcgi_index                 index.php;
    fastcgi_buffers               8 16k;
    fastcgi_buffer_size           32k;

    # fastcgi params
    fastcgi_param DOCUMENT_ROOT   $realpath_root;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param PHP_ADMIN_VALUE "open_basedir=$base/:/usr/lib/php/:/tmp/";
 }
}

docker-compose.yml

#Nginx Service <-- doesn't work
 #  webserver:
 #    build:
 #      context: .
 #      dockerfile: Dockerfile.nginx
 #    container_name: webserver
 #    restart: unless-stopped
 #    tty: true
 #    ports:
 #      - "8027:80"
 #      - "443:443"
 #    networks:
 #      - app-network

# works
webserver:
 image: nginx:alpine
 container_name: webserver
 restart: unless-stopped
 tty: true
 ports:
  - "8027:80"
  - "443:443"
 networks:
  - app-network

注释掉的代码无法启动:-

在此输入图像描述

使用未注释的代码并nginx:alpine作为图像 - 服务器托管 nginx 启动屏幕。我的Dockerfile.nginx似乎有问题。

答案1

检查nginx:alpinenginx 的配置

docker run -it --rm nginx:alpine /bin/sh
/ # cd /etc/nginx
/etc/nginx # ls -l
total 44
drwxr-xr-x    2 root     root            24 Dec 17 15:01 conf.d
-rw-r--r--    1 root     root          1077 Dec 15 14:55 fastcgi.conf
-rw-r--r--    1 root     root          1007 Dec 15 14:55 fastcgi_params
-rw-r--r--    1 root     root          2837 Dec 15 14:55 koi-utf
-rw-r--r--    1 root     root          2223 Dec 15 14:55 koi-win
-rw-r--r--    1 root     root          5231 Dec 15 14:55 mime.types
lrwxrwxrwx    1 root     root            22 Dec 17 15:01 modules -> /usr/lib/nginx/modules
-rw-r--r--    1 root     root           646 Dec 15 14:55 nginx.conf
-rw-r--r--    1 root     root           636 Dec 15 14:55 scgi_params
-rw-r--r--    1 root     root           664 Dec 15 14:55 uwsgi_params
-rw-r--r--    1 root     root          3610 Dec 15 14:55 win-utf

这是默认的 nginx.conf

/etc/nginx # cat nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

conf.d文件夹内,有一个默认的服务器配置

/etc/nginx # ls -l conf.d/
total 4
-rw-r--r--    1 root     root          1093 Dec 15 14:55 default.conf

其中有一个非常基本的服务器部分

/etc/nginx # grep -vE " *#|^ *$" conf.d/default.conf 
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

正如您所看到的,它仅配置为服务器静态内容,并且没有将请求转发到*.php支持的 php-fpm 的部分。

通常您需要替换此文件(或者如果您需要更多控制,请nginx.conf一起覆盖)。

至少您需要在服务器配置中有一个部分来将 PHP 请求重定向到后端。

    # handle .php
    location ~ \.php$ {
# 404
try_files                     $fastcgi_script_name =404;

# default fastcgi_params
include                       fastcgi_params;

# fastcgi settings
fastcgi_pass                  php-fpm-container:9000; # <--- this is the PHP-FPM container
fastcgi_index                 index.php;
fastcgi_buffers               8 16k;
fastcgi_buffer_size           32k;

# fastcgi params
fastcgi_param DOCUMENT_ROOT   $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$base/:/usr/lib/php/:/tmp/";
    }

您可以使用该网站DO 的 NGINX 配置工具帮助您为应用程序创建一个良好的起点整体配置,然后您可以在nginx:apline容器中覆盖它。

请注意,nginx 容器和 php 容器都需要有权访问您的 PHP 应用程序文件。

编辑1

自定义 nginx 镜像

FROM nginx:alpine

# If you have a bundle of config created by DO config file, 
# you can copy them all to overwrite all the settings.
# or use
# to overwrite a single file
# COPY my-site-config.conf /etc/nginx/conf.d/default.conf

更新您的 docker-compose 以使用您的自定义版本的 nginx 映像

version: '3'
services:

 #PHP Service
 app:
  build:
   context: .
   dockerfile: Dockerfile
  image: digitalocean.com/php
  container_name: app
  restart: unless-stopped
  tty: true
  environment:
    SERVICE_NAME: app
    SERVICE_TAGS: dev
  working_dir: /var/www
  networks:
   - app-network

 #Nginx Service
 webserver:
  build:
    context: .
    dockerfile: Dockerfile.nginx
  container_name: webserver
  restart: unless-stopped
  tty: true
  ports:
   - "8027:80"
   - "443:443"
  networks:
   - app-network

编辑2

似乎你遗漏了 Docker 的工作原理以及如何在多容器环境中工作的一些关键部分,首先,我建议你从这样的工作模板开始(阅读所有页面以了解绑定安装点的含义)因为您可以看到app(PHP 容器)并且可以webserver通过绑定安装访问文件,并且此模板还为您提供了覆盖 php 配置和的选项nginx 无需构建新容器。

version: '3'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: your_mysql_root_password
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge
#Volumes
volumes:
  dbdata:
    driver: local

相关内容