{"id":3862,"date":"2020-02-20T14:06:43","date_gmt":"2020-02-20T05:06:43","guid":{"rendered":"https:\/\/learningbox.co.jp\/?p=3862"},"modified":"2020-02-20T14:06:43","modified_gmt":"2020-02-20T05:06:43","slug":"blog_wsl2-docker","status":"publish","type":"post","link":"https:\/\/learningbox.co.jp\/en\/2020\/02\/20\/blog_wsl2-docker\/","title":{"rendered":"How to build a Docker environment using WSL2"},"content":{"rendered":"<p>This is Kaneda from Development! In this post I'm going to try to create a Docker environment using WSL2!<br \/>\nAfter all this time? Some of you may be wondering.<br \/>\n<strong>WSL2.........\u2753<br \/>\nDocker.........\u2753<\/strong><br \/>\nSome of you may have a lot of \u2753\u2753 in your head. First of all, I will explain from the above two terms. I hope you will also enjoy this paper.<\/p>\n<p class=\"mokujimidasi\">Click here for table of contents<\/p>\n<ul class=\"mokuji\">\n<li>1. what is WSL?<\/li>\n<li>2. what is Docker?<\/li>\n<li>3. let's actually do it!<\/li>\n<\/ul>\n<h3 class=\"page-sub-title\">1. what is WSL?<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/02\/linux-1.png\" alt=\"linux\" width=\"691\" height=\"361\" class=\"alignnone size-full wp-image-4182\" srcset=\"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/02\/linux-1.png 691w, https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/02\/linux-1-300x157.png 300w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><br \/>\nWindows Subsystem for Linux, which has been available since Windows 10 (yes! It's this acronym that's WSL! ) refers to the mechanism for using Linux from Windows 10.<\/p>\n<p>I mean, it's windows, but I can use Linux!<br \/>\nThat's what I mean.<br \/>\nI'll skip the explanation about Linux this time (it's very long...).<\/p>\n<p>Let's have a quick look!<\/p>\n<h3 class=\"page-sub-title\">2. what is Docker?<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/02\/docker-1024x349.png\" alt=\"docker\" width=\"1024\" height=\"349\" class=\"alignnone size-large wp-image-4176\" srcset=\"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/02\/docker-1024x349.png 1024w, https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/02\/docker-300x102.png 300w, https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/02\/docker-768x262.png 768w, https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/02\/docker.png 1243w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p style=\"margin: 0\" class=\"well2\">Docker is an open source software that provides an OS-level virtualization (English version) environment called a container.<br \/>\nCompared to hypervisor-based products that are fully virtualized, such as VMware products, it has the advantage of using less disk space, creating and launching virtual environments (instances) faster, and having almost no performance degradation.<br \/>\nQuote.<a href=\"https:\/\/ja.wikipedia.org\/wiki\/Docker\" target=\"_blank\" rel=\"noopener noreferrer\">Wikipedia.<\/a><\/p>\n<p><\/p>\n<p>Hmmm... I need a more engaging expression!<\/p>\n<p style=\"margin: 0\" class=\"well2\">In other words, Docker makes it easy to build lightweight virtual machines, and the entire application, including infrastructure, can be enclosed in a box called a container. You can also distribute and version control containers.<br \/>\n(omitted)<br \/>\n<strong>[Feature]<br \/>\nThe virtual environment is called a container type, and has less overhead because it directly accesses the host OS.<br \/>\nEasy to build environment, no need for Virtual Box<br \/>\nContainers are highly portable, so as long as you have Docker installed, you can run your app in the exact same environment.<br \/>\nThe container is recognized as one process by the host OS.<\/strong><br \/>\nQuote.<a href=\"https:\/\/qiita.com\/wMETAw\/items\/b9bc643ded4b92bf6add\" target=\"_blank\" rel=\"noopener noreferrer\">Introduction to Docker #1 [What is Docker] - Qiita<\/a><\/p>\n<p><\/p>\n<p>I'm sure you are starting to understand it somewhat. Since it is a container type, it is highly portable and you can use the same development environment.<br \/>\nI think this is a big advantage.<br \/>\nThe same project may behave differently in different development environments. With Docker, you don't have to worry about that!<\/p>\n<h3 class=\"page-sub-title\">3. let's actually do it!<\/h3>\n<p>Now that you've got an idea of what WSL and Docker are, let's see how you can actually use them!<\/p>\n<p><strong>STEP1.<\/strong> Click \"Enable or Disable Windows Features\" in Control Panel.<br \/>\n<img decoding=\"async\" src=\"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/01\/1-1.png\" alt=\"\" \/><br \/>\nAfter that, confirm that the check box of \"Windows Subsystem for Linux\" is checked.<br \/>\n<img decoding=\"async\" src=\"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/01\/1-2.png\" alt=\"\" \/><\/p>\n<p><strong>STEP2.<\/strong> Search for the desired distribution from Microsoft Store and install it.<br \/>\n   Personally, I recommend debian, but for now (as of 2019-11-10) docker only works on Ubuntu installed from Store, so I'll create it on Ubuntu this time.<br \/>\n<img decoding=\"async\" src=\"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/01\/2-1.png\" alt=\"\" \/><\/p>\n<p>There's a lot of commands here, so we'll go through them all at once!<br \/>\n<strong>STEP3.<\/strong> Check if \"WSL2\" is enabled.<br \/>\n    After starting powershell in administrator mode.<\/p>\n<p class=\"well2\">wsl -l -v<\/p>\n<p>and execution<br \/>\n        If you get an error, do a Windows Update to get the latest build (Win10IP).<br \/>\n        When the number of the \"VERSION\" column is \"2\", it is OK, and when it is \"1\", it is OK.<\/p>\n<p class=\"well2\">wsl -set-version NAME 2<\/p>\n<p>and execute (NAME is the one in the \"NAME\" column with \"VERSION\" set to \"1\")<\/p>\n<p><strong>STEP4.<\/strong> When the initial setting is completed after the start, various settings are made from the terminal of wsl that was started again by the administrator authority.<br \/>\n    (You actually need administrator privileges for docker-related tasks, but this time we'll start with administrator privileges from the beginning.)<br \/>\n    4-1. change visudo editor from nano to vi(vim)<br \/>\n         If you like nano, skip this step!<\/p>\n<p class=\"well2\">sudo update-alternatives -set editor \/usr\/bin\/vim.tiny<\/p>\n<p>    4-2. setting to be able to \"sudo\" without a password<br \/>\n         I 'sudo' everything, including mounting filesystems, managing packages, and so on. That's fine, but it's annoying that as soon as there's a pause, it says, \"Put in your password! so I'm going to set it up so that I can do it without a password.<\/p>\n<p class=\"well2\">sudo visudo<\/p>\n<p>         and put it at the bottom of the line.<\/p>\n<p class=\"well2\">The user name you set ALL=NOPASSWD: ALL<\/p>\n<p>         and finish saving.<br \/>\n    4-3 Upgrading Installed Packages and Updating Package Information<\/p>\n<p class=\"well2\">sudo apt update;sudo apt -y upgrade<\/p>\n<p>    4-3. install openssh client (and other things you need) so that you can use SSH<\/p>\n<p class=\"well2\">sudo apt install -y git openssh-client wget curl<\/p>\n<p><strong>STEP5.<\/strong> Installing Docker<\/p>\n<p class=\"well2\">\n    sudo apt install -y docker.io docker-compose<br \/>\n    sudo cgroupfs-mount<br \/>\n    sudo usermod -aG docker $USER<br \/>\n    sudo service docker start<br \/>\n    # Configure docker to be available for normal users<br \/>\n    sudo chown configured user name: -R ~\/.docker\/*<br \/>\n    sudo gpasswd -a $USER docker\n<\/p>\n<p><strong>STEP6.<\/strong> Now quit the terminal, and start it again, this time in normal mode<br \/>\n<strong>STEP7.<\/strong> Create the docker-compose.yml file<\/p>\n<p class=\"well2\">Description of docker-compose.yml<br \/>\n    - create `htdocs` directory in the directory where docker-compose.yml is saved beforehand<br \/>\n    - Please prepare various conf files and ini files by yourself.<br \/>\n    - If you create a file in the `htdocs` directory, the same file will be created in the `\/var\/www\/html` directory in the web container (shared folder).<br \/>\n    - `docker-compose exec web -u ec2-user bash` to manipulate inside the web server\n<\/p>\n<p>(Example of a LAMP environment on AmazonLinux2)<\/p>\n<pre><code>version: '3\r\nservices:\r\n  mysql:\r\n    image: mysql:5.7\r\n    command:\r\n      --sql-mode=NO_ENGINE_SUBSTITUTION\r\n    volumes:\r\n      - db_data:\/var\/lib\/mysql\r\n    restart: always\r\n    environment:\r\n      MYSQL_ROOT_PASSWORD: PASSWORD\r\n  phpmyadmin:\r\n    depends_on:\r\n      - mysql\r\n    image: phpmyadmin\/phpmyadmin\r\n    environment:\r\n      PMA_HOST: mysql\r\n    restart: always\r\n    ports:\r\n      - \"8080:80\"\r\n  web:\r\n    image: shizuki\/amazonlinux2-php-apache\r\n    env_file: . \/files\/env_file.env\r\n    volumes:\r\n      - . \/system.conf:\/etc\/systemd\/system.conf\r\n      - . \/httpd.conf:\/etc\/httpd\/conf\/httpd.conf\r\n      - . \/php.ini:\/etc\/php.ini\r\n      - . \/10-opcache.ini \/etc\/php.d\/10-opcache.ini\r\n      - . \/xdebug.ini \/etc\/php.d\/xdebug.ini\r\n      - . \/50-zip.ini \/etc\/php.d\/50-zip.ini\r\n      - . \/htdocs:\/var\/www\/html\r\n    restart: always\r\n    ports:\r\n      - \"80:80\"\r\n      - \"443:443\"\r\n    depends_on:\r\n      - mysql\r\nvolumes:\r\n    db_data: {}\r\n<\/code><\/pre>\n<p><strong>STEP8.<\/strong> Go to the directory where you saved the docker-compose.yml and run<\/p>\n<p class=\"well2\">docker-compose up -d<\/p>\n<p>  and execute it.<br \/>\n  <a href=\"http:\/\/localhost\/\" rel=\"nofollow\">http:\/\/localhost\/<\/a> and the content created in the `\/var\/www\/html` directory can be seen by phpmyadmin at http:\/\/localhost:8080<br \/>\nSTEP9. Install the WSL Linkage Development Extension on VSCode, and everything else will be done inside the VSCode terminal! (Huzzah!)<\/p>","protected":false},"excerpt":{"rendered":"\u958b\u767a\u306e\u517c\u7530\u3067\u3059\uff01\u4eca\u56de\u306e\u8a18\u4e8b\u3067\u306fWSL2\u3092\u4f7f\u7528\u3057\u3066Docker\u74b0\u5883\u3092\u4f5c\u6210\u3057\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\uff01 \u3044\u307e\u3055\u3089\uff1f\u3068\u601d\u3046 [&hellip;]","protected":false},"author":38,"featured_media":4171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[13,6,7,40],"tags":[],"class_list":["post-3862","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pc-blog","category-blog","category-7","category-40"],"acf":[],"jetpack_featured_media_url":"https:\/\/learningbox.co.jp\/wp-content\/uploads\/2020\/02\/WSL2.png","jetpack_shortlink":"https:\/\/wp.me\/pgMrZ4-10i","_links":{"self":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/3862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/comments?post=3862"}],"version-history":[{"count":47,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/3862\/revisions"}],"predecessor-version":[{"id":4189,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/posts\/3862\/revisions\/4189"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media\/4171"}],"wp:attachment":[{"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/media?parent=3862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/categories?post=3862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learningbox.co.jp\/en\/wp-json\/wp\/v2\/tags?post=3862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}