首页 > 解决方案 > Laravel docker容器不服务网站

问题描述

我正在尝试使用Laravel网站的 Dockerfile 为 kubernetes 集群构建一个 docker 映像。我对 docker 没有太多经验,所以我搜索了互联网并找到了一个带有 docker-compose 的 Dockerfile,我可以在其中运行该网站。但我只想使用 Dockerfile 并将网站仅保存在单个容器中,没有数据库或其他容器,也没有 docker-compose.yaml 文件。你能建议我做什么来制作一个只为网站服务的 docker 容器吗?我想使用 nginx 作为网络服务器。

这是我的 Dockerfile

FROM php:7.4-fpm

RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    libzip-dev \
    nginx

COPY nginx.conf /etc/nginx/nginx.conf

RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd xml zip

RUN pecl install -o -f redis \
    &&  rm -rf /tmp/pear \
    &&  docker-php-ext-enable redis

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -d /home/hridoy hridoy
RUN mkdir -p /home/hridoy/.composer && \
    chown -R hridoy:hridoy /home/hridoy

# Set working directory
WORKDIR /var/www

ADD . .

RUN composer install --no-interaction

USER hridoy

我正在使用自定义 nginx 配置文件,这是文件

server {
    listen 80;
    root /var/www/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

当我运行 docker build 命令时,它会成功构建映像,我也可以使用运行映像docker run -p 8080:80 app 但是当我转到 时localhost:8080,我无法访问该网站。浏览器给了我错误-

This page isn’t working
localhost didn’t send any data.
ERR_EMPTY_RESPONSE

标签: laraveldockerkubernetesdockerfile

解决方案


推荐阅读