11

How can I recreate this?

Create install from Laravel 8 docs and Laravel Sail docs.

I use the sail up command, which works great. The command builds docker containers, connects them, and makes development as easy as we can imagine, especially for VSCode, and this works fine, but it's slow in development with WSL2. I mean commands like `sail npm run dev.' Any ideas on how to speed this up?

FYI: The same project that runs on the same machine is at least 10x faster. For more information, I ran tests on i9-10900X, 32 GB RAM on Docker Desktop for Windows 10.

docker-compose.yml

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${DB_PORT}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
    redis:
        image: 'redis:alpine'
        ports:
            - '${REDIS_PORT}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - 1025:1025
            - 8025:8025
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local
IMSoP
  • 89,526
  • 13
  • 117
  • 169
Bart
  • 1,889
  • 1
  • 21
  • 38

2 Answers2

18

You should run docker from WSL2 if possible.

  1. Install docker and WSL2.
  2. Move your project to WSL by opening \\wsl$\ in explorer and navigating to your VM's home, in my case \\wsl$\Ubuntu-20.04\home\thomas

enter image description here

  1. Run docker-compose up -d / sail up from the VM

enter image description here

online Thomas
  • 8,864
  • 6
  • 44
  • 85
  • The latest Docker and WSL2 installed. I'm using Ubuntu 20. I've copied project to wsl$ route then permission problems emerged.... – Bart Dec 11 '20 at 02:52
  • I've tried to solve permissions, but it is still not working. It seems like a different folder structure. FYI: 'code' directory was hidden, by default. – Bart Dec 11 '20 at 03:01
  • 1
    Code is a directory I have created. Permission problems are solvable by defining the user and group in your docker-compose. If you can point out what exactly goed wrong I could try and help. – online Thomas Dec 11 '20 at 10:20
  • An easy way to start at first is accessing the container and changing ownership to the running user with `chown`. Another option (but not my advise) is using `chmod 777`. – online Thomas Dec 11 '20 at 10:25
  • 1
    From 15 seconds to 29ms, wow! However I face a similar problem, moving code to wsl including vendor code brings some permissions problems which are solved with 777. Deleting vendor and doing a `composer install` would require php with all required extensions, which defeats the purpose of using containers (I guess?). Am I missing something, or is this a necessary evil? – gX. Dec 11 '20 at 19:42
  • @gX. For development this is more than fine, for production it's also OK but yes than you can gain even more by using a seperate building container and production container in steps as can be read [here](https://docs.docker.com/develop/develop-images/multistage-build/) – online Thomas Dec 11 '20 at 21:14
  • @onlineThomas I've updated my question with my docker-compose.yml config. I'm really thankful for your help and tips. I know how to handle permissions on Linux|docker, but my initial question was how to handle all of this with `sail ` commands, `docker desktop` and starting fast development with VSCode or any other IDE on Windows10. Do you have an idea of how the workflow should look like? – Bart Dec 12 '20 at 01:54
  • Sail is probably not especially made to handle every optimization for windows. It's just to provide a docker environment for your app, after that you have to do these steps yourself. – online Thomas Dec 12 '20 at 16:40
  • From my experience, unfortunately docker on WSL still has issues regarding accessing files. It is not really a Sail problem but a docker/WSl problem. I personnaly am using Docker-desktop for 2-3 years now and tried moving to WSL 2. While being a lot faster, it was not stable at all and got a lot of permissions issues. I guess we windows users stilll have to wait a little more for it to work smoothly :) – jeremy_nikolic Dec 17 '20 at 08:24
  • 1
    @jeremy_nikolic As Linux and Windows permissions are around for some time now and never have been compatible I'm affraid this is something you need to do yourself. The thing is: you can run your commands from your WSL-OS (Ubuntu for example), [share your users between this host and the container](https://medium.com/faun/set-current-host-user-for-docker-container-4e521cef9ffc) and a lot of permission problems should be solved. – online Thomas Dec 17 '20 at 08:35
  • Unfortunately WSL, docker desktop, and other windows 10 VM features make my VirtualBox v6.1.16 unusable. I will return to this topic, as soon as these Windows services can work properly and coexist with VirtualBox. At the moment of writing, I am unable to run my VirtualBox VMs. – Bart Dec 18 '20 at 20:02
  • @Bart They probably never will, this is the second time you assume your very specific problem will be fixed in a near update. It probably won't if you know the history and how hyper-v works – online Thomas Dec 18 '20 at 21:56
  • @onlineThomas VBox team puts a lot of effort to make these stacks work together in the harsh Win10 environment, where VBox needs to coexist with WSL. So yes I don't expect fixes from Microsoft, but I have some faith in guys from VirtualBox, even though it is ORACLE. – Bart Dec 18 '20 at 23:35
  • 1
    @onlineThomas Thanks for your guidance, I´ve only need to 'sudo chmod -R a+rwx /path/to/project' so i can execute 'sail up' commads without issues on the wsl virtual directory, I can see a notable performance in local web execution. – Kadaiser Apr 26 '23 at 07:45
2

I was going to explain this but just go here and read for yourself. This is what helped me. VSCode kinda yelled at me when I opened up a project in the default location and gave me this link. https://learn.microsoft.com/en-us/windows/wsl/compare-versions

Performance across OS file systems

We recommend against working across operating systems with your files, unless you have a specific reason for doing so. For the fastest performance speed, store your files in the WSL file system if you are working in a Linux command line (Ubuntu, OpenSUSE, etc). If you're working in a Windows command line (PowerShell, Command Prompt), store your files in the Windows file system.

For example, when storing your WSL project files:

  • Use the Linux file system root directory: \\wsl$\Ubuntu-18.04\home\<user name>\Project
  • Not the Windows file system root directory: C:\Users\<user name>\Project

All currently running distributions (wsl -l) are accessible via network connection. To get there run a command [WIN+R] (keyboard shortcut) or type in File Explorer address bar \\wsl$ to find respective distribution names and access their root file systems.

You can also use windows commands inside WSL's Linux Terminal. Try opening a Linux distribution (ie Ubuntu), be sure that you are in the Linux home directory by entering this command: cd ~. Then open your Linux file system in File Explorer by entering (don't forget the period at the end): powershell.exe /c start .

Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
charlyRoot
  • 161
  • 1
  • 9
  • Thanks Clement. I didn't know I should do that. I'll do that from now on. Wish I had your OCR tools. – charlyRoot Mar 09 '21 at 22:59
  • Clement didn't do any *OCR* conversion, he just copied and pasted the text from the Microsoft Docs and formatted it using [SO markdown](https://stackoverflow.com/editing-help) which you could do as well. We should never post text, code and errors as images and you can read about it here [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). – Christos Lytras Apr 18 '21 at 22:27