为什么 docker 没有自动为我的 laravel 提供服务?

问题描述 投票:0回答:1

所以我有一个问题,似乎无法解决,也找不到解决方案..

我正在使用 React、Sass 和 Laravel 与 docker 制作一个项目,而 docker 不会在线为我的 Laravel 提供服务。它会无限加载,我不知道该怎么办了。

这是我的目录树,我知道它已经磨损,但它可以工作:)

my-project/   
├── backend/   
│ └── (Laravel files here)   
├── frontend/   
│ └── (React files here)  
├── docker/   
│ ├── backend/  
│     └── Dockerfile  
│ ├── frontend/   
│     └── Dockerfile   
└── docker-compose.yml  

这是我的 docker-compose.yml 文件:

version: '3.8'

services:
  backend:
    build:
      context: ../  # Root of the project
      dockerfile: docker/backend/Dockerfile  # Path to the Dockerfile
    image: lerenlerentool-backend
    container_name: lerenlerentool_backend
    restart: unless-stopped
    working_dir: /var/www
    entrypoint: php artisan serve --host=0.0.0.0
    volumes:
      - ../backend:/var/www  # Maps the backend folder into the container
      - ./backend/php-local.ini:/usr/local/etc/php/conf.d/local.ini
    ports:
      - "8000:8000"
    networks:
      - app-network
    environment:
      - APP_ENV=local
      - APP_DEBUG=true
      - APP_KEY=base64:ertrdSfHv01dD/4R6N6acsRQW+hLn3MTNmk84ypaW4I=
      - DB_HOST=127.0.0.1
      - DB_PORT=3306
      - DB_DATABASE=lerenlerentool
      - DB_USERNAME=test
      - DB_PASSWORD=1234
    depends_on:
      - db

  frontend:
    build:
      context: ../
      dockerfile: docker/frontend/Dockerfile
    image: lerenlerentool-frontend
    container_name: lerenlerentool_frontend
    restart: unless-stopped
    ports:
      - "3000:80"
    volumes:
      - ../frontend:/app
      - /app/node_modules
    networks:
      - app-network

  db:
    image: mysql:8.0
    container_name: lerenlerentool_db
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: lerenlerentool
      MYSQL_USER: test
      MYSQL_PASSWORD: 1234
      MYSQL_ROOT_PASSWORD: 1234
    volumes:
      - dbdata:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - app-network

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: lerenlerentool_phpmyadmin
    restart: unless-stopped
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: 1234
    ports:
      - "8080:80"
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  dbdata:

这是我的 dockerfile:

# Use a PHP image with FPM support
FROM php:8.2-fpm

# Install necessary PHP extensions
RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev zip git unzip

# Install Composer (for Laravel dependency management)
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

# Set the working directory to where the Laravel project is
WORKDIR /var/www/backend

RUN apt-get update && apt-get install -y \
    libpng-dev libjpeg-dev libfreetype6-dev \
    libonig-dev libzip-dev zip unzip git curl \
    && docker-php-ext-install pdo pdo_mysql bcmath mbstring

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql

# Copy the Laravel project files into the container
COPY backend /var/www/backend  

# Install Laravel dependencies via Composer
RUN composer install --optimize-autoloader --no-dev

# Install Node.js and npm (if necessary for any assets)
RUN curl -sL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get install -y nodejs

# Run Laravel migrations or other setup commands if needed
CMD php artisan serve --host=0.0.0.0 --port=8000

# Expose the correct port (if needed for API)
EXPOSE 8000

我尝试用这个编辑我的dockerfile:

CMD php artisan serve --host=0.0.0.0 --port=8000

我用这个撰写:

entrypoint: php artisan serve --host=0.0.0.0

但我仍然需要进入我的 laravel 项目所在的文件夹,并手动将服务命令输入到该文件夹的 cmd 中,laravel 才能工作。

php reactjs laravel docker sass
1个回答
0
投票

您需要手动运行

php artisan serve
的事实表明 Docker 容器中的入口点或 CMD 未正确配置为自动运行 Laravel 服务器。

  • 在您的
    docker-compose.yml
    中,删除入口点字段,因为您 已经在 Dockerfile 中处理了。

enter image description here

  • 在 Dockerfile 中,您已设置 CMD
    php artisan serve --host=0.0.0.0 --port=8000
    。您可能想确保该命令正确执行,尤其是在
    composer install
    之后。

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.