Install Docker in WSL 2 without Docker Desktop

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there nectar attack is here in this video we're going to go over how to install Docker directly inside of your wsl2 distro without needing to install Docker desktop for example if I hide my camera and take a look here at my sister notice that I do not have the docker icon here because I am not running Docker desktop however I can run a Docker info here and we do successfully connect to the docker server which is running the Daemon here within this specific wsl2 distro yes we do get a couple of warnings here but these warnings are not going to affect anything in your day-to-day for example there's no limitations on what you can and can't run here so I've got an example dockerize flask app here I'm just running a Docker compose up that is using Docker compose V2 this is connecting to Docker that's running within wsl2 everything is up and running if I go to localhost 48 000 here which is the port that this application runs on notice that everything operates smoothly here this is the application running on localhost I didn't have to do anything fancy to get all this up and running in fact yeah let's go over all the steps here in a second but I do want to just briefly talk about maybe the pros and cons of why you might want to choose not to use Docker desktop on Windows and you know this is not because I have some bad blood with Docker my relationship with them is still very well I'm part of the Dr Captain's program I really like using them been using Docker since 2014 and I plan to continue on using them for uh indefinitely right the applications and the things that I build completely work I will use it but um I did recently try to update to the latest version of Docker desktop 4 and for the very longest time I was running a Docker desktop version 3 like ancient version of Docker but things worked but you know if you've watched some of my videos in the past maybe from a month or two ago I did start using Docker compose V2 and that feature is really only available with modern versions of Docker desktop such as you know some version of four and I don't know I think it might just be my machine because my workstation is starting to get a little bit crusty we're talking like almost eight years old but uh you know it's got an SSD it's got a modern CPU it's got 16 gigs of memory nothing runs slow it runs like a champ basically when I first built it however Docker desktop 4 I just had some micro stutters um just appearing on machine on my machine all the time so every time I did anything related to my input devices like move my mouse or started typing in my keyboard then I would get these stutters where it was like my machine was just coming unresponsive for I don't know 500 milliseconds and it was happening basically a couple times per minute it was just like a complete stutter machine would lock up and then it would come back to normal and you know I didn't completely debug the problem down to like the exact version of Docker desktop that was the issue or you know what component of Docker desktop is messing things up but when I was running the newest version of Docker desktop these micro stutters would happen when I stopped Docker desktop they would go away I've also never experienced these micro stutters on this whole machine in the entire eight years of using it so uh that just led me down a path of wondering like can I actually get Docker installed running on wsl2 without needing to use Docker desktop and I found some Solutions on the internet tweaked them a little bit and here we are and by the way it's going to be pretty easy to get this going like you basically just follow Docker steps for installing Docker on Linux for whatever distro you have in wsl2 and then you drop in like a five line shell script in your profile file and you're good to go you won't need to tweak or mess with anything crazy with like system D or anything like that and then we're going to get to those details here but I just wanted to give you a quick warning or not a warning but maybe a pros and cons on why you might want to consider still using Docker desktop on Windows or maybe you're like me and you take a more pragmatic approach where yeah if things can work without it then maybe you're okay without it and by the way like you know all the clients that I work for some pretty big organizations like yeah we definitely pay for Docker desktop licenses you know we got folks running on Mac OS Windows etc etc you know so this has nothing to do with Dockers pricing schemes or changes around needing to pay for Docker desktop and you know well I forget the exact thing from their fact but like you know if your company is generating 10 million plus Revenue yeah paying them seven dollars a developer for a Docker desktop license plays absolutely zero role in my decision not to use Dr desktop I want to be very clear in that one you know I fully support Docker and I think their pricing scheme is completely reasonable for companies who qualify for needing to pay for Docker desktop so I wouldn't just use this as like not to pay the seven bucks or whatever but uh yeah pros and cons wise you know Docker desktop does offer a UI for looking at your container so if you really like that feature then maybe you want to stick with Docker desktop because just using the command line like this you're not going to get any UI features you know everything is command line driven also a Docker desktop has this idea of doctor extensions now where you as a developer like you and me we can contribute these Docker extensions that run within Docker desktop and then you can you know interface with the docker server and you can build some pretty nifty things like for example you know I don't know of any offhand and I can't even show you because I don't have Docker desktop but like you know if you run something like a Docker stats this will list all the containers that are running on your machine here and just give you a live update of the CPU memory usage you know disk and net IO etc etc for all the containers running and that's pretty cool but uh you know you could technically make something like a Docker extension that could chart all this out to some type of graph and you can like visually see that almost like uh you know like a little dashboard or something that could be a cool extension idea and you're just going to not be able to do those things without having a Docker desktop for doing extensions unless like you built your own like completely separate thing you know but yeah those are things that you might want to weigh the pros and cons against two just on you know whether or not you want to do this and another another big one is like for example and this doesn't really affect me is I tend to stick with one distro of wsl2 we know right now I'm running 2004 Hero buntu at some point I will update to 2204 but when I do and I make sure that I moved all my files over to the new version I'm going to completely ditch this old wsl2 instance right I have no reason really to keep it around so that does mean that um if you are not using Docker desktop like you need to install a doctor inside of every single wsl2 distro that you plan to use so if you are someone who are you know spinning up a lot of different distros then it could be handy to keep Docker desktop around because you don't need to install Docker desktop inside of a specific distro like you literally just like check a box on the options to enable it for distro and that's it whereas with this approach that we're going to do here you'd have to go through the motions of installing Docker in each distribute manually again not a big deal for me because I typically only operate in one but if you're spinning up distros all the time maybe that's something to think about now there is one last thing to consider here and it comes down to running kubernetes so Docker desktop makes it really easy to run a single node kubernetes cluster through Docker desktop you basically just enable kubernetes apply and restart and you're good to go but if you're not using Docker desktop then you will need to come up with alternative means to run kubernetes if that's something you want to do now I've done videos in the past about using kind and I really do like the solution because you basically just run kubernetes within Docker so you will not need Docker desktop for this to work you can basically just install this kind tool directly inside of wso2 and you're good to go honestly even if you're using Docker desktop you could use kind I've actually done that in the past before I moved to installing Docker directly inside of wsl2 but I just wanted to include this in a video to let you know that if you do plan to work with a multi-node cluster kind is a really good tool to do that it's really lightweight gets you up and going and I just wanted to let you know that there is a solution forward to run kubernetes within Docker inside of wsl2 without Docker desktop if that was something that you wanted to do but okay let's say that you are ready to move away from Docker desktop and windows and just run Docker natively what do you do step one would be uninstalling Docker desktop because you know like when over before in my system tray here Docker desktop is not installed it is not running entirely and the next step would be to install Docker directly inside of your ws02 instance now I've already got that installed here and I'm going to leave all the links here that we're about to look at in the description these are all the installation steps for running Docker on Native Linux and we can see Ubuntu Fedora Debian centas etc etc the nice thing is they have this little helper script like a convenience script get docker.sh and it will determine what distro you have and just install Docker correctly depending on yeah if it attacks that you're running Ubuntu or a sentos or something like that so you just curl down that shell script here officially from docker.com you know I'm not trying to trick you into downloading something different feel free to Google for these links if you'd like on your own and then you just run that shell script at sudo because you do need to install a service running inside of wsl2 here with root access for installing Docker itself and you know if you weren't using Ubuntu maybe you're using Debian or centas or something let's choose sentas here because it's completely different distro you know if you scroll down in here you will find the same exact convenience script here listed with the same exact steps so again like you know we're not going to check all them here but chances are there's one little command here and then running it is going to get you up and running for installing Docker itself now if you are familiar with working with Docker desktop Docker desktop will install Docker and it's going to install Docker compose for you but in our case we need to install Docker compose separately and there is also an additional Linux step that you'll want to do and that will be adding your group to the or sorry adding your user to the docker group and if I go to my terminal here and just cancel this one out you know if I run the ID command here we can see that I am logged in to wsl2 here as the Nic user and down here there is a group called Docker and my user belongs to that group so what you'll want to do is you'll want to make a new group called Docker if it's not already there after installing Docker then you'll want to add your user to that Docker group there and then they say you need to log out and log back in here I don't know 100 here what will happen within wsl2 I am pretty sure that if you just completely shut your entire terminal and open it up again then that will give a very similar effect if not then you'll want to either reboot your windows box I don't think that's going to be necessary or you can open up a Powershell window here if you're using the Microsoft terminal open up that in admin mode and then you can just run wsl2 shutdown or sorry WSL shutdown within that Powershell window and that is going to completely shut down wsl2 so that when you open up a new terminal is going to spawn a new wsl2 process like whatever Windows does behind the scenes and it'll effectively give you the same type of thing but yeah I don't think you're gonna have to go that far I'm pretty sure just closing your terminal we'll be good to go and then the last step there is we want to install Docker compose and you know I would suggest installing Docker compose V2 at this point in time it's very good I've done videos about that one in the past I'll link those in the card and we can see here we have different installation instructions for different distros here this will be a little bit different depending on what this drill that you have but let's say that you're running Ubuntu right all you need to do is run the sudo app get update and then you can just get install Docker compose Dash plugin here and that's it you're going to have Docker compose available to you and also as a sanity track you can just run Docker compose version here so with no spaces and no no double dashes here for the version because it's a go Application I've also done videos about Docker Capo's V2 as well but yeah at this point in time now you have Docker installed now you have Docker compose installed you can also run Docker without needing to use sudo that was that second step that we did here the post installation steps when you added your user to the docker group now you officially have dagger and Docker compose installed in wsl2 but there's still one situation here so normally if you were to install Docker on Linux you'd have a systemd set up on your Linux box and the docker service would be all set up for systemd so that you know when you boot the Box up like everything will be good to go like it's going to start up but we're not going to have that luxury in WSL to at least not right now I don't know if Microsoft is working on adding system d2w SL2 they might be like I don't run like preview versions or anything like that I run the stable version of wsl2 on Windows 10. but there's ways to get around that in not hacky ways at all like this completely still works but you know this is like the custom part of getting things to work in wsl2 but it's not really bad at all so uh let me open up my Z profile file here I am running as usual also made videos about that in the past if you're running bash then things are completely the same you're not gonna have to change anything here but uh this if condition here on the bottom is the thing that you'll want to add to either your bash underscore profile file if you're using bash or your Z profile file if you're using g-shell and the basic tldr here around this and we'll get into more details of how this works in a second is you know this rep condition here will make sure that this code inside of here will only execute if you are running wsl2 this is important because my DOT files up are up available on GitHub no number of people use it I use it on a Macbook for work for some stuff that accompany yours you to laptop I run it on a native Linux laptop for my own self I write on my main workstation workstation that's running wsl2 so I want this to work in all sorts of different scenarios here and you know we don't rent this stuff to run if you're running Mac OS or like native Linux because it doesn't have to so yeah we just make sure that we're running wsl2 and then what we do is we just see if Docker the actual service you know not using system D this is using like a knit D or whatever we just want to see if Docker is currently running so we can actually copy this command here and then let me open up below it's just going to be a little bit easier to see both things at the same time if I run this command here we can actually see that the docker service is running so even if I close my terminal and open it again no it doesn't matter that I'm using tmux you're not going to have to pay that penalty every time you open internal terminal so you can see here you know if I open up a new terminal tab it's basically instant right uh 100 milliseconds whatever happens to be if they open up another TMX pane here we can see very quick there's like no delay at all and the idea here though yeah that's part of what the zip condition is doing here so it's checking to see if Docker is running and if it's not running then it will execute one more command here which is a WSL command and it's going to operate within your distro that you're currently in it's going to run this command as root and what is it going to do it is going to run a service Docker start and basically we're doing you know all these redirect here to death null because you know if you are not running wso2 you don't want to see any error messages from grep and likewise you know whatever if you don't have Docker installed even because again my DOT files are pretty generic you don't you can use them and you don't have to have Docker installed right I just don't want to Output any messages around like hey this doctor's service isn't there because you know if I try to run a command like this here how to sudo in front but and also Docker is available so if I just like put in some nonsense here you know we're just going to get like an unrecognized service measures and you know we don't want to see that stuff every single time we open a new terminal because on wsl2 this profile file actually gets executed every single time that you open up a terminal so if you know if I do something like Echo cool or something like this with a couple O's because why not you know if I open this up then you can see that every time I open a terminal it is executing this you know that's unlike the behavior on Native Linux where this profile file typically only gets executed once when you log into the machine and then your um you know Z RC file or bash RC file that one would execute every time you open a terminal but for wsl2 Works a little bit differently so this isn't going to execute every time you open a terminal but as we saw before opens up very fast you can't even makes no difference with this hero there you know you saw those things if I comment this out and open up another terminal thing like it doesn't make a difference like it's opening in the same exact speed so let me bring that back done cool so yeah that's how basically all of this works and and the idea there is again first time you open your terminal it's going to lag for a little bit while the actual Docker service starts and then that's it you're good until you reboot Windows or maybe like I said before you know if you end up having to shut down wsl2 using um wsl2 or WSL shutdown or whatever the heck that command is like you'd run from Powershell then that would also reset things to where this would execute again for the first time and you know if you run a PR side script on Docker we can see that the docker service is running over here there's a PID for it it's all running it's up and good we can connect to it as we saw we're able to run containers and and a lot and you're good to go that's it by the time you watch this video maybe I'll update these links to point to the blog post that it will make after I make this video that will have some condensed version of you know quickly installing things on Ubuntu because it's really just like you know execute these commands here do the group stuff and then install Docker compose plugin and you're good so that's it on that note yeah let me know in the comments below if you are going to be using this method or maybe you have a different or better way to do this on wsl2 you you know keep in mind when I'm making this video systemd support isn't in the version of wsl2 that I have when we do get support for that maybe this stuff will change a little bit potentially we won't even need it because then systemd will just manage everything that's what I'm hoping for in the future but for now a couple lines of shell scripts uh not a problem at all so if you have any questions about this let me know in the comments below as well thanks a lot for watching and if you enjoyed the video please give it a thumbs up before you go and I will see you in the next video
Info
Channel: Nick Janetakis
Views: 41,727
Rating: undefined out of 5
Keywords: docker, wsl 2, docker desktop
Id: SDk3pqFXgs8
Channel Id: undefined
Length: 16min 14sec (974 seconds)
Published: Tue Nov 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.