Docker部署Laravel应用:MacOS正常,Ubuntu 16.04运行异常求助
npm run dev Failure in Laravel Docker Setup on Ubuntu 16.04 Hey there! Let’s dig into why your Laravel Docker setup works perfectly on macOS but throws errors when running npm run dev on Ubuntu 16.04. Below are the most common fixes to resolve webpack build issues in this scenario:
1. Fix Node.js/NPM Version Compatibility
Ubuntu 16.04 ships with an outdated default Node.js version, which often conflicts with Laravel’s webpack/mix requirements. Update Node.js in your Dockerfile to a version compatible with your project:
Option 1: Use NodeSource Repository
Add these lines to your app.dockerfile.local:
# Install Node.js 16.x (adjust version to match your project's needs) RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - RUN apt-get install -y nodejs
Option 2: Use NVM for Version Management
If you prefer flexible version control:
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash RUN . ~/.nvm/nvm.sh && nvm install 16 && nvm alias default 16
2. Correct File Permissions
Ubuntu’s stricter permission model can prevent webpack from writing to the /var/www directory. Set proper ownership and permissions in your Dockerfile:
# Ensure web server user owns the application directory RUN chown -R www-data:www-data /var/www # Make storage and cache directories writable RUN chmod -R 755 /var/www/storage /var/www/bootstrap/cache
Alternatively, specify the user in your docker-compose-local.yml when mounting volumes:
services: app: volumes: - ./:/var/www:delegated - /var/www/node_modules # Avoid overwriting node_modules with local files user: "www-data:www-data"
3. Refresh Dependencies to Fix Cross-System Cache Issues
Sometimes cached dependencies from macOS cause conflicts on Ubuntu. Clear and reinstall dependencies inside the container:
# Enter the running container docker exec -it <your-container-name> bash # Clean up old dependencies rm -rf node_modules package-lock.json # Reinstall and rebuild npm install npm run dev
You can also bake these steps into your Dockerfile to automate the process.
4. Verify Webpack/Mix Path Configuration
Double-check your webpack.mix.js to ensure output paths are absolute and correct for the container environment:
mix.js('resources/js/app.js', '/var/www/public/js') .sass('resources/sass/app.scss', '/var/www/public/css');
This avoids path resolution discrepancies between macOS and Ubuntu.
5. Update Docker on Ubuntu 16.04
Outdated Docker versions on Ubuntu 16.04 can cause unexpected volume or filesystem issues. Upgrade to the latest stable Docker Engine:
# Uninstall old Docker packages sudo apt-get remove docker docker-engine docker.io containerd runc # Set up Docker repository sudo apt-get update sudo apt-get install ca-certificates curl gnupg lsb-release curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/trusted.gpg.d/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null # Install latest Docker sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
If none of these fixes work, sharing the specific error output from npm run dev will help pinpoint the exact issue.
内容的提问来源于stack exchange,提问作者Juliatzin

