You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker部署Laravel+PostgreSQL项目后浏览器无法访问

Laravel PostgreSQL容器化部署后页面无法访问问题

我正在将连接PostgreSQL的Laravel项目容器化部署,以下是我的配置文件:

Dockerfile

FROM php:8.2-fpm

WORKDIR /var/www/html/myprojectsname

RUN apt-get update && apt-get install -y \
    libonig-dev \
    git \
    curl \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libzip-dev \
    zip \
    unzip \
    libpq-dev \
    postgis \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install pdo_mysql pdo_pgsql mbstring exif pcntl bcmath gd

RUN echo "upload_max_filesize = 100M" > /usr/local/etc/php/conf.d/uploads.ini \
    && echo "post_max_size = 100M" >> /usr/local/etc/php/conf.d/uploads.ini

COPY . /var/www/html/myprojectsname

RUN chown -R www-data:www-data /var/www/html/myprojectsname \
    && chmod -R 775 /var/www/html/myprojectsname/storage

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

WORKDIR /var/www/html/myprojectsname

RUN composer install

CMD php artisan serve --host=0.0.0.0 --port=8181
EXPOSE 8181

docker-compose.yml

version: '3.8'

services:

    myprject:
        container_name: myprject
        image : "laravel-app"
        working_dir: /var/www/html/myprojectname
        build: .
        volumes:
            - ../myprojectname:/var/www/html/myprojectname
        ports:
            - "8014:80"
        command: bash -c 'php artisan migrate && php artisan serve --host 0.0.0.0'
        depends_on:
            postgres_serv:
                condition: service_healthy  
        restart: on-failure  
        networks:
            - webServiceNet

    postgres_serv:
        container_name: postgres_serv
        image: "postgres_postgis"
        restart: always
        volumes:
            - ../postgres-data:/var/lib/postgresql/data
        environment:
            - POSTGRES_USER=ahmadbadpey
            - POSTGRES_PASSWORD=123456
        ports:
            - "5432:5432"
        networks:
            - webServiceNet
        healthcheck:
            test: ["CMD-SHELL", "pg_isready -U myuser"]
            interval: 10s
            retries: 5
            timeout: 2s

    pgAdmin:
        container_name: pgadmin-ProjectName
        image: "dpage/pgadmin4:latest"
        restart: always
        volumes:
            - ../pgAdmin-data:/var/lib/pgadmin
            - ../pgadmin-password.txt:/pgadmin-password.txt
        environment:
            - PGADMIN_DEFAULT_EMAIL=ahmad@gmail.com
            - PGADMIN_DEFAULT_PASSWORD_FILE=../pgadmin-password.txt
            - PGADMIN_LISTEN_PORT=8080
        ports:
            - "8080:8080"
        networks:
            - webServiceNet

networks:
    webServiceNet:
        driver: bridge

执行docker-compose up后,数据库迁移和serve启动命令均正常执行,但浏览器访问http://localhost:8014/、http://0.0.0.0:8000/等地址时,页面无任何内容显示。


问题排查与修复方案

1. 端口映射不匹配

  • 容器内php artisan serve默认监听8000端口(docker-compose的command未指定端口,覆盖了Dockerfile的CMD配置),但当前端口映射是8014:80,容器内无服务监听80端口(你用的是php-fpm镜像,但实际启动的是artisan serve,而非fpm+nginx组合)。
  • 修复:修改端口映射为8014:8000,或在serve命令中指定端口并对应映射:
    ports:
        - "8014:8000"
    command: bash -c 'php artisan migrate && php artisan serve --host 0.0.0.0 --port=8000'
    

2. 项目目录路径不一致

  • Dockerfile中工作目录是/var/www/html/myprojectsname,但docker-compose的working_dir和卷挂载路径是/var/www/html/myprojectname(少了一个s),路径不统一会导致Laravel无法加载正确文件。
  • 修复:统一所有路径,比如将Dockerfile中的路径全部改为/var/www/html/myprojectname,确保和docker-compose配置一致。

3. PostgreSQL健康检查用户不匹配

  • 健康检查命令使用pg_isready -U myuser,但环境变量设置的数据库用户是ahmadbadpey,用户不匹配可能导致健康检查异常(即使迁移暂时正常,也存在潜在风险)。
  • 修复:修改健康检查命令:
    healthcheck:
        test: ["CMD-SHELL", "pg_isready -U ahmadbadpey"]
        interval: 10s
        retries: 5
        timeout: 2s
    

4. pgAdmin密码文件路径错误

  • docker-compose中PGADMIN_DEFAULT_PASSWORD_FILE设置为../pgadmin-password.txt,但容器内的挂载路径是/pgadmin-password.txt,路径不匹配会导致pgAdmin无法读取密码。
  • 修复:修改环境变量:
    environment:
        - PGADMIN_DEFAULT_EMAIL=ahmad@gmail.com
        - PGADMIN_DEFAULT_PASSWORD_FILE=/pgadmin-password.txt
        - PGADMIN_LISTEN_PORT=8080
    

5. 确认Laravel数据库连接配置

  • 检查项目.env文件,确保数据库连接使用容器名作为主机:
    DB_CONNECTION=pgsql
    DB_HOST=postgres_serv
    DB_PORT=5432
    DB_DATABASE=ahmadbadpey
    DB_USERNAME=ahmadbadpey
    DB_PASSWORD=123456
    
    注:PostgreSQL默认会创建与用户名同名的数据库,因此DB_DATABASE可设置为ahmadbadpey。

内容的提问来源于stack exchange,提问作者Ahmad Badpey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 17:21:15