【问题标题】:Docker, Nginx, localhost does not respondDocker、Nginx、localhost 没有响应
【发布时间】:2020-05-02 11:47:23
【问题描述】:

我希望你们在这段特殊的时间里都做得很好。

我遇到了关于我的 nginx+php 映像的问题,这是我从头开始构建的第一个映像(使用 Google),所以我不确定一切是否正确。

此图像将在多个家庭项目中为我服务,我在 Gitlab 容器注册表中使用此图像。

我正在使用这个 Dockerfile:

FROM alpine:latest

COPY --from=library/docker:latest /usr/local/bin/docker /usr/bin/docker
COPY --from=docker/compose:latest /usr/local/bin/docker-compose /usr/bin/docker-compose


RUN adduser -S www-data -u 1000
RUN apk upgrade -U
RUN apk add --update --no-cache \
        git \
        bash \
        vim
RUN apk --update --no-cache add php7 php7-fpm php7-mysqli php7-json php7-openssl php7-curl \
    php7-zlib php7-xml php7-phar php7-intl php7-dom php7-xmlreader php7-ctype php7-session \
    php7-mbstring php7-gd nginx curl

RUN apk add --update --no-cache nginx supervisor

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

ENV COMPOSER_ALLOW_SUPERUSER=1
RUN composer global require "hirak/prestissimo:^0.3" --prefer-dist --no-progress --no-suggest --classmap-authoritative \
&& composer clear-cache
ENV PATH="${PATH}:/root/.composer/vendor/bin"

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

RUN mkdir -p var/cache var/log var/sessions \
&& chown -R www-data var

VOLUME /srv/api/var
VOLUME /var/www/html
VOLUME /var/www/src

COPY index.php /var/www/html/

COPY src/run.sh /run.sh
RUN chmod u+rwx /run.sh

EXPOSE 80

ENTRYPOINT [ "/run.sh" ]
CMD ["init"]

调用这个 bash 脚本,用于 Symfony 迁移:

#!/bin/bash

INIT=false
MIGRATION=false
CREATEDB=true

for DOCKER_OPTION in $@
do
    case "$DOCKER_OPTION" in
    init )
        INIT=true
        shift
        ;;
    migration )
        MIGRATION=true
        shift
        ;;
    with-existing-db )
        CREATEDB=false
        shift
        ;;
    * )
        break
        ;;
  esac
done

APP_ENV=${APP_ENV:-dev}

init_project () {
    composer install --prefer-dist --no-progress --no-suggest --no-interaction
    chmod +x bin/console && sync
    php bin/console assets:install

    if $CREATEDB
    then
        php bin/console doctrine:schema:update -f
        php bin/console doctrine:fixtures:load -n
    fi
}

if $MIGRATION;
then
    php bin/console doctrine:migrations:migrate --no-interaction -vvv
    chown -R www-data:www-data /srv/api/var/
fi

if $INIT;
then
    mkdir -p /var/nginx/client_body_temp
    chown www-data:www-data /var/nginx/client_body_temp
    mkdir -p /var/run/php/
    chown www-data:www-data /var/run/php/
    touch /var/log/php-fpm.log
    chown www-data:www-data /var/log/php-fpm.log

    if [ "$APP_ENV" != 'prod' ];
    then
        init_project
    fi
    exec supervisord --nodaemon --configuration="/etc/supervisord.conf" --loglevel=info
fi

exec "$@";

由这个 Docker-compose 启动:

version: "3.7"

services:
  lamp:
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - 80:80
    volumes:
      - .:/var/www/html/
      - ./src:/var/www/src/

使用此 Nginx 配置:

user www-data;
worker_processes 4;
error_log /dev/stdout;
pid /tmp/nginx.pid;
events {
  worker_connections 1024;
}

http {
    types {
        text/html                             html htm shtml;
        text/css                              css;
        text/xml                              xml;
        image/gif                             gif;
        image/jpeg                            jpeg jpg;
        application/javascript                js;
        application/atom+xml                  atom;
        application/rss+xml                   rss;
        image/svg+xml                         svg;
    }

    client_body_temp_path /tmp/client_body;
    fastcgi_temp_path /tmp/fastcgi_temp;
    proxy_temp_path /tmp/proxy_temp;
    scgi_temp_path /tmp/scgi_temp;
    uwsgi_temp_path /tmp/uwsgi_temp;

    # mime types

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

    server {
        listen 80;
        server_name localhost;
        root /usr/share/nginx/html;
        access_log /dev/stdout;
        error_log /dev/stdout;

        location / {
                    root /usr/share/nginx/html;
                    index index.html index.htm;
         }
    }

}

一切似乎都很顺利。

很遗憾,我无法显示我的 index.php (php_info)。

当我这样做时

curl -vvv http://localhost:80

* Trying ::1:80...
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.69.1
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

当我这样做时

curl -vvv http://localhost:80 --trace-ascii dump.txt

== sync, corrected by elderman ==
== Info: Connected to localhost (::1) port 80 (#0)
=> Send header, 73 bytes (0x49)
0000: GET / HTTP/1.1
0010: Host: localhost
0021: User-Agent: curl/7.69.1
003a: Accept: */*
0047: 
== Info: Empty reply from server
== Info: Connection #0 to host localhost left intact

我已经在网上搜索过,但是我的技能在nginx的配置方面非常有限。

你有什么想法可以提交给我。

【问题讨论】:

  • 你能把这个减少到minimal reproducible example吗? (比如,你在没有详细的PHP配置的情况下只运行nginx是否有同样的问题?你的应用程序是否需要vim之类的包才能运行,或者它们可以省略吗?)
  • 嗨,大卫,我回复了新的答案。 stackoverflow.com/a/61559239/12070178

标签: php docker nginx curl docker-compose


【解决方案1】:

感谢您的回复,

对于VIM,他已经在这里添加了:

RUN apk add --update --no-cache \
        git \
        bash \
        vim

所以我尝试使用这个 Dockerfile,没有 PHP/Composer(它非常简约?):

FROM alpine:latest

COPY --from=library/docker:latest /usr/local/bin/docker /usr/bin/docker
COPY --from=docker/compose:latest /usr/local/bin/docker-compose /usr/bin/docker-compose

RUN adduser -S www-data -u 1000
RUN apk upgrade -U
RUN apk add --update --no-cache \
        git \
        bash \
        vim
RUN apk --update --no-cache add nginx curl

RUN apk add --update --no-cache nginx supervisor

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

RUN mkdir -p var/cache var/log var/sessions \
&& chown -R www-data var

VOLUME /srv/api/var
VOLUME /var/www/html
VOLUME /var/www/src

COPY index.php /var/www/html/

COPY src/run.sh /run.sh
RUN chmod u+rwx /run.sh

EXPOSE 80

ENTRYPOINT [ "/run.sh" ]
CMD ["init"]

对于

curl -vv http://localhost:80

*   Trying ::1:80...
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.69.1
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Closing connection 0

对于

curl -vv http://localhost:80 --trace-ascii dump.txt

== Info:   Trying ::1:80...
== Info: Connected to localhost (::1) port 80 (#0)
=> Send header, 73 bytes (0x49)
0000: GET / HTTP/1.1
0010: Host: localhost
0021: User-Agent: curl/7.69.1
003a: Accept: */*
0047: 
== Info: Recv failure: Connection reset by peer
== Info: Closing connection 0

你怎么看?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-22
    • 2021-10-11
    • 1970-01-01
    • 2017-01-04
    • 2012-05-10
    • 2016-11-10
    • 1970-01-01
    • 2019-08-21
    相关资源
    最近更新 更多