首页 > 解决方案 > Docker有一个开放的端口,但配置文件中没有描述?

问题描述

我有 2 个 Dockerfile:一个带有 nginx,另一个带有 php。带有 php 的容器暴露了 9000 端口,但在 docker-compose.yml 或 Dockerfile 中没有描述。

是什么让docker打开9000端口?

有没有人遇到这个问题?任何帮助表示赞赏)))

重现步骤

码头工人-compose.yml

version: "3.4"

services:
    nginx:
        build:
            context: .
            dockerfile: ./Dockerfile-Nginx
        container_name: nginx
        image: nginx
        ports:
            - "80:80"
            - "443:443"
        volumes:
            - ./web:/var/www/vhosts
            - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
            - ./config/nginx/common-nginx-host.conf:/etc/nginx/common-nginx-host.conf:ro
            - ./config/nginx/csp.conf:/etc/nginx/csp.conf:ro
            - ./config/nginx/expires-map.conf:/etc/nginx/expires-map.conf:ro
            - ./config/nginx/mime.types:/etc/nginx/mime.types:ro
            - ./config/nginx/sites-enabled:/etc/nginx/sites-enabled:ro
            - ./logs/nginx:/var/log/nginx
            - ./data/letsencrypt:/etc/letsencrypt
        environment:
            - TZ=Etc/UTC
        links:
            - php
        networks:
          - local
        restart: always
    php:
        build:
            context: .
            dockerfile: ./Dockerfile-PHP7.4
        container_name: php
        image: php
        volumes:
            - ./web:/var/www/vhosts
            - ./logs/php/7.4:/var/log/php
            - ./config/php/7.4/www.conf:/usr/local/etc/php-fpm.d/www.conf:ro
        environment:
            - TZ=Etc/UTC
        networks:
            - local
        restart: always
    networks:
        local:
           driver: bridge

Dockerfile-Nginx

FROM ubuntu:latest as nginx

RUN \
        apt-get -yqq update && \
        apt-get -yqq install nginx && \
        apt-get install -y tzdata && \
        apt-get install -yqq --no-install-recommends apt-utils && \
        apt-get -yqq install software-properties-common && \
        apt-get -yqq install && \
        LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php && apt-get update && \
        apt-get install -yqq php7.4 php7.4-cli php7.4-common php7.4-mysql && \
        apt-get -yqq install letsencrypt && \
        apt-get clean


WORKDIR /var/www/vhosts

CMD ["nginx", "-g", "daemon off;"]

Dockerfile-PHP7.4

FROM php:7.4-fpm as php-fpm-7.4

RUN \
        apt-get update  -yqq && \
        apt-get install -yqq \
                xvfb libfontconfig wkhtmltopdf \
                git sass \
                libpng-dev \
                libjpeg-dev \
                libzip-dev \
                libfreetype6-dev \
        libxrender1 \
        libfontconfig1 \
        libx11-dev \
        libxtst6 \
        zlib1g-dev \
        imagemagick \
        libmagickwand-dev \
        libmagickcore-dev \
        libmemcached-dev && \
        pecl install memcached && \
    yes '' | pecl install imagick && \
        docker-php-ext-install gd && \
    docker-php-ext-enable gd && \
    docker-php-ext-enable imagick && \
    docker-php-ext-enable memcached && \
        docker-php-ext-configure pdo_mysql --with-pdo-mysql && \
        docker-php-ext-install -j$(nproc) \
        pdo_mysql \
    zip

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

COPY config/ImageMagick-6/policy.xml /etc/ImageMagick-6/policy.xml


RUN ${TZ} > /etc/timezone

CMD ["php-fpm"]

部分 nginx 配置文件

location /index.php {

                # Rate limiting
                limit_req zone=defaultlimit burst=40 nodelay;
                limit_req_status 444;
                limit_req_log_level warn;

                fastcgi_split_path_info  ^(.+.php)(.*)$;

                set $fsn /index.php;
                if (-f $document_root$fastcgi_script_name){
                        set $fsn $fastcgi_script_name;
                }

                #include snippets/fastcgi-php.conf;
                fastcgi_pass php:9000;

                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;

                #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
                fastcgi_param  PATH_INFO        $fastcgi_path_info;
                fastcgi_param  PATH_TRANSLATED  $document_root$fsn;
        }

Docker 开放了 9000 端口

标签: dockerdocker-composedockerfiledocker-machine

解决方案


这是因为它暴露在父 docker 镜像中(php:7.4-fpm)

https://github.com/docker-library/php/blob/master/7.3/alpine3.12/fpm/Dockerfile#L233


推荐阅读