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

Docker运行Laravel Sail命令报错:无法找到sail用户(passwd无匹配)

问题描述

在Docker环境中使用Laravel框架,尝试运行命令./vendor/bin/sail composer install管理依赖并安装Tailwind CSS时,始终报错:

unable to find user sail: no matching entries in passwd file

以下是我的配置文件:

docker-compose.yml

version: '2.0'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: abdelazizmirasoft/php
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www/html/
    volumes:
      - ./:/var/www/html/
      - ./php/laravel.ini:/usr/local/etc/php/conf.d/laravel.ini
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver-upwork
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www/html/
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:5.7.32
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge

#Volumes
volumes:
  dbdata:
    driver: local

.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:5ndGfShEF7w6wfnnzMlGrG7A9IFOJrlh9G7VVw0BTgM=
APP_DEBUG=true
APP_URL=http://localhost
APP_SERVICE=app

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=upwork_clone
DB_USER=upwork_user
DB_PASSWORD=p@$sw0rd

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Dockerfile

FROM php:8.0.5-fpm

# Copy composer.lock and composer.json into the working directory
COPY composer.lock composer.json /var/www/html/

# Set working directory
WORKDIR /var/www/html/

# Install dependencies for the operating system software
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    libzip-dev \
    unzip \
    git \
    libonig-dev \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions for php
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd

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

# Copy existing application directory contents to the working directory
COPY . /var/www/html

# Assign permissions of the working directory to the www-data user
RUN chown -R www-data:www-data \
        /var/www/html/storage \
        /var/www/html/bootstrap/cache

# Expose port 9000 and start php-fpm server (for FastCGI Process Manager)
EXPOSE 9000
CMD ["php-fpm"]
解决方案

问题根源

你当前使用的是自定义Docker容器配置,并非Laravel Sail官方生成的环境。Sail命令默认会以sail用户身份执行,但你的app容器中根本没有创建这个用户,因此触发报错。

方案一:直接在现有容器中执行Composer命令

你的app容器已经预装了Composer,无需依赖Sail,直接通过docker exec操作即可:

  • 进入容器终端:
    docker exec -it app bash
    
    然后执行:
    composer install
    
  • 或者直接执行单行命令:
    docker exec -it app composer install
    

方案二:切换到Laravel Sail官方环境(推荐,后续更易维护)

如果想使用sail系列命令,需要切换到Sail的标准配置:

  1. 停止当前运行的容器:
    docker-compose down
    
  2. 安装Laravel Sail依赖(本地有Composer直接执行,没有的话可以用临时容器):
    composer require laravel/sail --dev
    
  3. 生成Sail的Docker配置文件:
    php artisan sail:install
    
  4. 启动Sail容器:
    ./vendor/bin/sail up -d
    
  5. 之后即可正常使用sail composer install等命令。

Tailwind CSS安装步骤(两种环境通用)

完成依赖安装后,按照以下步骤配置Tailwind:

  1. 安装Tailwind及相关依赖:
    # 方案一:在现有app容器中执行(需先装Node.js,可在Dockerfile中添加node安装步骤)
    docker exec -it app npm install -D tailwindcss postcss autoprefixer
    # 方案二:用Sail命令执行
    ./vendor/bin/sail npm install -D tailwindcss postcss autoprefixer
    
  2. 生成Tailwind配置文件:
    # 对应方案一
    docker exec -it app npx tailwindcss init -p
    # 对应方案二
    ./vendor/bin/sail npx tailwindcss init -p
    
  3. 编辑tailwind.config.js,添加模板文件路径:
    /** @type {import('tailwindcss').Config} */
    module.exports = {
      content: [
        "./resources/**/*.blade.php",
        "./resources/**/*.js",
        "./resources/**/*.vue",
      ],
      theme: {
        extend: {},
      },
      plugins: [],
    }
    
  4. 在resources/css/app.css中导入Tailwind指令:
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
  5. 编译前端资源:
    # 方案一
    docker exec -it app npm run dev
    # 方案二
    ./vendor/bin/sail npm run dev
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 08:54:24