我遇到了一个本地主机没有发送任何数据的错误。

huangapple go评论77阅读模式
英文:

I am getting a localhost didn’t send any data error

问题

我是一个Docker新手,但我想尝试在我的现有Laravel项目中使用它。

请注意,我这里的东西只是从互联网上不同来源的学习中获取的。

首先,我可以使用以下的docker-compose.yml文件来启动nginx和php-fpm:

version: "3"

services:
    web:
        build: .
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "80:80"

    php:
        image: php:7.1-fpm
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "9000:9000"

Dockerfile:

FROM nginx:mainline-alpine
WORKDIR D:\code\project
COPY ./default.conf /etc/nginx/conf.d/default.conf

./default.conf:

server {
    listen 80;
    root /usr/share/nginx/html/public;
    index index.php index.html index.htm index.nginx-debian.html;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ ^/.+\.php(/|$) {
        fastcgi_pass php:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

http://localhost/phpinfo.php的结果:

我遇到了一个本地主机没有发送任何数据的错误。

http://localhost的结果:

我遇到了一个本地主机没有发送任何数据的错误。

但是,正如你所看到的,我需要laravel所需的PHP PDO扩展/驱动程序。

因此,我尝试通过Dockerfile安装所需的PHP扩展,并遇到了“localhost没有发送任何数据错误”的问题。

docker-compose.yml:

version: "3"

services:
    web:
        build: .
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "80:80"

    php:
        build: .
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "9000:9000"

Dockerfile:

FROM nginx:mainline-alpine
WORKDIR D:\code\project
COPY ./default.conf /etc/nginx/conf.d/default.conf

FROM php:7.1-fpm
RUN docker-php-ext-install pdo pdo_mysql

./default.conf - 没有更改,与上面相同。

http://localhost/phpinfo.php的结果:

我遇到了一个本地主机没有发送任何数据的错误。

http://localhost的结果:

我遇到了一个本地主机没有发送任何数据的错误。

容器已经启动并运行:

我遇到了一个本地主机没有发送任何数据的错误。

我是否漏掉了什么?我不知道如何使用Docker进行调试,也不知道在哪里查看日志或错误。

我已经被困扰了3天。有人能帮忙吗?

英文:

I am a newbie with docker but I want to try it so I can use it with my existing laravel project.

Please note that the things I have here are just from different sources of learning from the internet.

For starters, I can have nginx and php-fpm up and running with this docker-compose.yml

version: "3"

services:
    web:
        build: .
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "80:80"

    php:
        image: php:7.1-fpm
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "9000:9000"

Dockerfile:

FROM nginx:mainline-alpine
WORKDIR D:\code\project
COPY ./default.conf /etc/nginx/conf.d/default.conf

./default.conf

server {
    # The port to listen on
    listen 80;
    # The root directory, which must exactly match the internal volume share
    root /usr/share/nginx/html/public;

    index index.php index.html index.htm index.nginx-debian.html;
    
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?$query_string;
    }

    # For all files with the PHP extension run the following
    location ~ ^/.+\.php(/|$) {
        # Pass the request to the host "php" and port 9000 (default PHP-FPM port).
        # The "php" host name is generated from the application name in the
        # Docker Compose file that was previously defined.
        fastcgi_pass php:9000;
		# Include the default NGINX FastCGI Parameters
        include fastcgi_params;
		# Define one additional parameter telling PHP-FPM where to find the file
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

The result in http://localhost/phpinfo.php:

我遇到了一个本地主机没有发送任何数据的错误。

The result in http://localhost

我遇到了一个本地主机没有发送任何数据的错误。

But as you can see, I need the PHP PDO extension/driver for laravel.

So, I am trying to install said PHP extension thru the Dockerfile and I am stucked with the issue "localhost didn’t send any data error"

docker-compose.yml

version: "3"

services:
    web:
        build: .
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "80:80"

    php:
        build: .
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "9000:9000"

Dockerfile

FROM nginx:mainline-alpine
WORKDIR D:\code\project
COPY ./default.conf /etc/nginx/conf.d/default.conf

FROM php:7.1-fpm
RUN docker-php-ext-install pdo pdo_mysql

./default.conf - same as above, no changes

The result in http://localhost/phpinfo.php:

我遇到了一个本地主机没有发送任何数据的错误。

http://localhost:

我遇到了一个本地主机没有发送任何数据的错误。

The containers are up and running:

我遇到了一个本地主机没有发送任何数据的错误。

Am I missing something? I do not know how to debug with docker or where to view logs or errors.

I have been stucked with this for 3 days. Can someone please help?

答案1

得分: 1

我终于成功解决了它。

我所做的是将Dockerfile拆分为两个部分,分别放在不同的子文件夹中,一个用于nginx,另一个用于php。

./nginx/Dockerfile

FROM nginx:mainline-alpine
WORKDIR D:\code\project
COPY ./default.conf /etc/nginx/conf.d/default.conf

./php/Dockerfile

FROM php:7.1-fpm
RUN docker-php-ext-install pdo pdo_mysql

我还更新了docker-compose.yml文件以反映两个Dockerfile的路径。

version: "3"

services:
    web:
        build: ./nginx
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "80:80"

    php:
        build: ./php
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "9000:9000"
英文:

I finally managed to solve it.

What I did was to split the Dockerfile into two, putting it inside separate sub-folders, one for nginx and one for php.

./nginx/Dockerfile

FROM nginx:mainline-alpine
WORKDIR D:\code\project
COPY ./default.conf /etc/nginx/conf.d/default.conf

./php/Dockerfile

FROM php:7.1-fpm
RUN docker-php-ext-install pdo pdo_mysql

And I also updated the docker-compose.yml to reflect the path of both Dockerfiles

version: "3"

services:
    web:
        build: ./nginx
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "80:80"

    php:
        build: ./php
        volumes:
            - D:\code\project:/usr/share/nginx/html
        ports:
            - "9000:9000"

huangapple
  • 本文由 发表于 2023年7月23日 14:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定