Get a free advice now!

    Pick the topic

    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    Tags

    Slow Docker on Windows WSL2? Here’s a solution

    Slow Docker on Windows WSL2? Here’s a solution

    CHALLENGE: Incorrectly configured Docker on Windows leads to slow Docker performance

    SOLUTION: Run Docker Windows from Ubuntu 20.04 LTS with WSL2 enabled

    One of the more common problems for Developers that use Windows is that the projects with Docker configuration work really slowly, to a point when sometimes a single browser request needs to wait 30-60 seconds to be completed. This is obviously a problem, one that negatively affects project progression and generally makes the life of developers more difficult.

    Why is Docker so slow? The root of the issue is that Windows 10 is (was) using WSL (Windows Subsystem for Linux), which is a layer between Windows and Linux. Communication between these two (Hard Drive operations) can be quite slow.

    Solution for Docker performance improvement

    The suggested solution is:

    • use WSL 2 in Windows 10
    • and start Docker directly from Ubuntu in Windows

    Check out the step-by-step tutorial below. The “WSL 2” feature, released as Windows Update in mid 2020, was designed to increase file system performance and support full system call compatibility. Properly configured Docker and Windows WSL2 will give you really good performance in terms of speed.

    Step 1: ENABLE WSL2 on Windows

    A. Enable WSL 2 on your Windows
    B. Install Linux on Windows 10 (Ubuntu 20.04 LTS from Microsoft store)
    C. Enable WSL 2 on Ubuntu 20.04
    D. Install Docker Desktop
    E. Enable WSL 2 for Docker
    More info: https://www.padok.fr/en/blog/docker-windows-10

    Step 2: Install SSH on Ubuntu

    We want to enable SSH connection to Ubuntu and we will be using it for local files’ “deployment”. Our goal here is:

    – We have a GIT project cloned on the main Windows hard drive (SSD)
    – We’re developing using PHPStorm
    – PHPStorm Files’ deployment is configured in a way to automatically send files to Ubuntu.
    – With this setup, we will instantly see the changes to our code in the browser.

    How to configure SSH on Ubuntu:
    https://linuxize.com/post/how-to-enable-ssh-on-ubuntu-18-04/

    Step 3: Configure PHPStorm Deployment

    A. File / Settings / Build, Execution, Deployment – Deployment
    B. Click + (to add a new deployment config) – choose SFTP
    C. Fill in ‘SSH configuration’
    – Host: your Ubuntu public IP
    (to find the proper IP address, type the following command in your Ubuntu: ip a)
    – User name: your ubuntu username
    – Authentication type: Password
    – Password: your ubuntu pass
    D. Set proper directories mapping in: Deployment / Mappings
    E. Deployment / Options:
    – find the option ‘Upload change files automatically to the default server’ and set to ‘Always’

    Step 4: ssh to Ubuntu, run Docker

    A. Open your SSH Client (ex: Cmder ) and connect to Ubuntu using SSH connection:
    $ ssh [email protected]
    (use your Ubuntu credentials here, we were using the ones in PHPStorm Deployment configuration)
    B. Log in to the root account:
    $ sudo su
    C. Run Docker:
    $ docker-compose up -d

    Step 5: Connect to the database

    A. Log into your Docker container:
    $ docker exec -it myproject_php bash
    $ cd /var/www/html/public/
    B. Download MYSQL Client:
    $ wget https://github.com/vrana/adminer/releases/download/v4.7.7/adminer-4.7.7-mysql.php
    C. Find database credentials (in Docker configuration files), ex:
    DATABASE_URL=mysql://root:mypass@mysql:3306/mydatabasename
    C. Open the following link in the browser: http://localhost/adminer-4.7.7-mysql.php
    Server: mysql
    Username: root
    Password: mypass
    Database name: mydatabasename

    Summary

    That’s it. We’ve configured:
    – WSL2 on Windows
    – Ubuntu Linux on Windows
    – PHPStorm Deployment
    – Running Docker from Ubuntu
    – Have ability to access Docker database
    This configuration allows Docker to run really fast. Instead of waiting 60 seconds, now the browser request will run faster than 1 second!

    Troubleshooting

    * Error after trying to start Docker, ex: ERROR: Encountered errors while bringing up the project.
    – always use Ubuntu being logged in as a root:
    $ sudo su
    * I see an error related to “not sufficient permissions”
    – Run Powershell or the SSH Client using the ‘Run as administrator’ option
    * I can’t install SSH on Win Ubuntu:
    sudo su
    apt install openssh-server
    service ssh start
    service ssh status

    If you get the following error: sshd: no hostkeys available – exiting :
    https://www.garron.me/en/linux/sshd-no-hostkeys-available-exiting.html

    If you get the following error: Permission denied (publickey):
    nano /etc/ssh/sshd_config

    Find: PasswordAuthentication
    Set it to: yes
    #and then restart the ssh service:
    service ssh restart

    https://askubuntu.com/questions/311558/ssh-permission-denied-publickey/881518#881518

    * hyper-v is not enabled

    To Enable Hyper-V
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V –All

    To Disable Hyper-V
    Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All

    https://www.poweronplatforms.com/enable-disable-hyper-v-windows-10-8/ 1

    * Check if hyper-v is enabled:
    Run WIN Powershell (with Admin rights) and execute:

    $hyperv = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online
    if($hyperv.State -eq “Enabled”) {
    Write-Host “Hyper-V is enabled.”
    } else {
    Write-Host “Hyper-V is disabled.”
    }

    Do you need someone to implement this solution for you? Check out our specialists for hire in the outsourcing section.

    Are you considering a global project and are uncertain how to proceed? See how we do it.

    Comments
    6 response
    1. thanks, is much faster now, amazing
      although I have to figure out how to have git in windows and keep consistent with ubuntu,
      thanks

      • I’m glad to hear that the Docker performance on your Windows machine is much faster now! Regarding keeping Git consistent between Windows and Ubuntu, here are some suggestions:
        1. Using an IDE that supports WSL: If you use an Integrated Development Environment (IDE) like Visual Studio Code, you can use the Remote – WSL extension. This allows you to open any folder in your WSL2 environment and automatically syncs changes between the two environments. So, you can use Git from Windows, and the changes will reflect in Ubuntu automatically.

        2. Working Directly on the Linux Filesystem: To avoid syncing issues entirely, you could do all your work directly on the Linux filesystem (\\wsl$\) using either the Linux command line or Windows applications. This way, any changes you make will automatically be in the right place for Ubuntu, and you won’t need to worry about syncing. The Linux filesystem performs better when accessed from within the WSL2 environment, so this method can also improve performance.

        3. Use ‘localhost’ or ‘127.0.0.1’: Your WSL2 instance is essentially a virtual machine with its own separate network interface, which is why it has its own IP address that may change from time to time. This can indeed cause problems when trying to connect to it via SFTP if the IP address changes. However, since your WSL2 instance is on the same machine as your Windows system, you can use localhost or 127.0.0.1 as the SFTP host when setting up your PhpStorm Deployment settings. This way, even if the IP address of the WSL2 instance changes, you should still be able to connect to it.

        You can test your SSH connection to your WSL2 instance using SSH command. Open a Command Prompt, PowerShell, or a terminal emulator like Windows Terminal in Windows and run:

        ssh myusername@localhost

        If your SSH server is running and configured properly in your WSL2 instance, you should be prompted for your password. Enter the password for your username in WSL2 Ubuntu.

        After entering your password, if everything is set up correctly, you should be logged in to your WSL2 Ubuntu environment via SSH. You’ll see a shell prompt that you can use to enter commands in your WSL2 environment.

        Remember, if you see an error message like “Connection refused”, it usually means the SSH server isn’t running on your WSL2 Ubuntu. You can start it with the command: sudo service ssh start from within your WSL2 Ubuntu environment.

    2. The performance for me from a network perspective is still abysmal. The network tops out at 10MB/s on transfers on a gigabit infrastructure. Is there anything to investigate from that perspective?

      • Thank you for your comment and for bringing up this issue. While the article was primarily focused on improving Docker’s file system performance on Windows, it seems you’re experiencing a specific network issue.

        There are several things that could potentially be causing your slow network speeds:

        1. Network Configuration: Check your network configuration settings in Docker. Make sure that Docker is configured to use the correct network interface and that there are no limitations set on the network bandwidth.

        2. Hardware or Driver Issues: It’s possible that the issue could be related to your network adapter or its drivers. Try updating your network drivers, or if possible, testing with a different network adapter.

        3. Docker Version: Make sure you’re using the latest version of Docker. There have been numerous performance improvements and bug fixes in recent versions that could potentially resolve your issue.

        4. Antivirus or Firewall Interference: Sometimes security software can interfere with network performance. Try temporarily disabling any antivirus or firewall software to see if that improves your network speeds.

        5. Resource Allocation: Docker containers share system resources with the host OS, and if resources are not properly allocated, this can potentially affect network performance. If you’re running many containers concurrently, you might be reaching the limits of your system’s resources. You may need to adjust the limits or manage the allocation of resources more efficiently.

        I hope these suggestions help!

    3. This article provides a fantastic solution to a common problem that many Windows developers face when working with Docker. Slow Docker performance can be frustrating, and your step-by-step guide is a game-changer.

      By following these clear and straightforward instructions, developers can significantly improve their workflow and project progress. This method not only fixes the issue of slow Docker but also makes the development process smoother, making it more efficient and enjoyable.

      I want to express my appreciation for sharing this valuable solution, which is sure to help many developers who struggle with Docker performance on Windows. Your detailed explanations and helpful troubleshooting tips show a deep understanding of the problem, making it easier for readers to successfully implement this solution. Great job!

    Add comment

    Your email address will not be published. Required fields are marked *

    Popular news

    Digital marketing without third-party cookies – new rules
    • Technology
    • Trends

    Digital marketing without third-party cookies – new rules

    February 21, 2024 by createIT
    eCommerce healthcheck
    • Services
    • Trends

    eCommerce healthcheck

    January 24, 2024 by createIT
    Live Visitor Count in WooCommerce with SSE
    • Dev Tips and Tricks

    Live Visitor Count in WooCommerce with SSE

    December 12, 2023 by createIT
    Calculate shipping costs programmatically in WooCommerce
    • Dev Tips and Tricks

    Calculate shipping costs programmatically in WooCommerce

    December 11, 2023 by createIT
    Designing a cookie consent modal certified by TCF IAB
    • Dev Tips and Tricks

    Designing a cookie consent modal certified by TCF IAB

    December 7, 2023 by createIT
    Understanding the IAB’s Global Vendor List (GVL)
    • Dev Tips and Tricks

    Understanding the IAB’s Global Vendor List (GVL)

    December 6, 2023 by createIT
    Effortlessly transform JSON data into a clear schema
    • Uncategorized

    Effortlessly transform JSON data into a clear schema

    December 5, 2023 by createIT

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us