Secret Docker Commands you don't know!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is Christian and here are some of the most useful and advanced Doo commands that I have collected over the last months when I was working on my HB projects and before you're getting the wrong assumption here no we are not covering a simple Docker run or Docker ps-- all in here no we are really focusing on some Advanced and practical commands that you might not find in each and every documentation online is so anything that I found to be super useful when I had to troubleshoot issues with containers or commands that I'm using to manage workloads across multiple machines how I'm dealing with fights in containers or commands that I used to avoid running into problems I had such as running out of disk space which was really annoying so I'm pretty sure even though you might be somewhat experienced with Docker you will definitely find the one or another useful command in this list you probably didn't know before I know that's a pretty bold statement so hope I can deliver on that but before we start with Docker I also quickly want to show you an application I came across recently and this is called type AI which is a chat GPT powered iOS keyboard I'm using on my iPhone and on my iPad and I know everyone these days seems to be jumping on this AI hype train but hear me out honestly I found this application to be super useful to me because I often have to write text in English but as you might know and you probably will also hear it English is not my mother language and sometimes I just need to use an AI assistant for grammar and spellchecking or finding out how to correctly express myself of course it also works with any other language like German to but type AI is really amazing for translation as well as wording recommendations or autoc completing the text that you're writing you can even chat with the AI and change the tone of your text all those cool and amazing features you might be familiar with from chat GPT but the cool thing about type AI is that it seemlessly integrates into any application you're using on your phone because it is a keyboard application so you don't have to copy and paste text around different applications anymore this is really really great and so do me a favor check it out click the link in the description box below for a free trial of type AI premium version and find out whether it's going to be useful to you too and now let's go back to topic and let's go through some of the amazing doer commands that I found okay so let's first start with the First Command that I found and this is the docker system command so I know this is kind of a simple command one or another person will know but let's start simple and then accelerate throughout the video so the docko system commands are command collections that you can use for managing disk space or managing Docker resources these are super useful to avoid some problems you might run into so what you can do to check the current utilization of disk space is the docker system DF command so this will show you all the images the containers the local volumes in Bild cache and how much uh disk space they consume so what you can do to avoid this is uh frequently running a Docker system prune command so this will remove unused data Note this will remove all stopped containers all the networks that aren't used by at least one container all dangling images and build cach so I'm not sure if you want to use that command here so the command that I'm always using to clean up all my older images that I'm not using anymore is the docker image prune command so as I can see this will remove all the dangling images so what means dangling images as a quick explanation if you have downloaded darker images and you're pulling down the latest version of an image it might happen that the latest TCH was updated so the older version becomes unreferenced I can simply demonstrate this by just pulling down the latest uh latest full tag of the castra image I know this was updated in the last three months so when I pull down the latest version you can see there is a new version of that latest full tag and it's currently pulling down this version now once this is complete and we execute the docker image LS command again you can see that there is a new image updated free weeks ago that has the latest full tag again but there is still the older version of our image that is not referenced anymore but it's still there on our hard drive consuming the disk space this is what you can remove with the docker image prune command so this will remove all the dangling images and you avoid running into problems however the only problem that you still might run into is that when you're not using the latest tag but you're using pined taged version so for example here on the engine X images I'm always using a specific version to not run into any migration problems and so on so these older versions that you're not using anymore are not removed by the docker image prune command so this you can remove with the docker image prune D- all command so this will not just remove the dangling images that are not referenced anymore it will just remove all images that are not attached to a current running project I'm always doing this from time to time on my workstations and servers to just reclaim that consumed or that used disk space okay next I want to show you the docker context command and this is something that is really useful to manage remote workloads so when you're are running a couple of virtual Linux machines where you have installed different doco hosts you don't always want to connect with SSH to that remote machine and then execute the docker command you can all do it from your local workstation with a Docker context so with a Docker context you can Define remote environments or different local environments you connect to the docker socket to manage containers with the docker context list you can see what different types of Docker context you have created as you can see those two context simply connect to the local Docker socket that is running in dock or desktop but I also have created another context that is using the SSH protocol to connect to my remote machine where Docker is running as well so if you want to do that you simply can type Docker context create then give it a name for example server demo with a description uh Docker on server demo 1-- Docker and then you can define a host with an SSH protocol so this Docker context connects first to the remote machine over SSH to execute the darker commands so let's create this and to use this you simply can type in dock context use and then switch to a different Docker context so now when you execute the docker ps-- all for example you can see this is not running on my local machine anymore this is running on my server demo 1 and these are some of the docker projects that I'm running here and now you can just type in any Docker command it will automatically be executed on the select the docker socket and you don't always need to manually uh open an SSH session you can just use your local workspace to work with remote Doo containers so that's pretty useful in my opinion but it's also really nice and I'm using this to copy files in and out of the container's file system and by the way this command can be used in combination with the docker context too so for example when I want to copy a file from my local Mac to one of my doer containers that are deployed on remote server I can connect to the docker context and then use the docker CP command for copy very simple just like on a Linux shell for example let's copy a default HTML web page that I've created onto the engine X server on my remote machine now I can simply refresh the web server and it just updated the web page without having to open an SSH connection somehow Mount the volume inside the container this is really great for any testing on and of course you can use it to copy and take backups or restore things in and out of the containers file system this is really really useful okay so let's find out how to use Docker commands to troubleshoot issues you might have with containers and I know if you're running Docker you most likely will run into problems and most of you guys will be familiar with the docker locks command this is what you can use to collect the last locks that are created by the container but there is a lot more you can do with this command and you can use it in combination with other Linux tools to quickly and effectively search what you're looking for for example one thing that I often do is I often just use the dock locks command with a-f parameter so this will follow the lock output and will not just show you the last lock lines it will all also print something new when a new lock is created for example if you have this lock here for the web server and you are creating another request you can directly see this in a lock file and this is very useful for troubleshooting things now of course some containers or some Services might produce a lot of lock output for example traffic is such a case where there are a lot of locks created especially in debug mode for and sometimes you have a hard time finding exactly what you're looking for so this is why I'm most of the time using the doco loocks command and pipe this to a searching tool like grab for example so one thing you need to be aware of is when you're using the docker loocks command is that you need to redirect the standard error output to the standard out in Linux 2 so this syntax makes sure that it's both redirected to the same standard out and this is what you can then pipe into a grab searching tool for example if you only want to see the lines that contain an arror you can use that and that to filter the locks uh to match what you're looking for now graph can do many many things and it can be used in so many complicated ways one thing that I want to show you here is to enable the regular expression mode with a the dash capital E parameter and then you can do some really cool things for example if you you don't want to see all the error lock lines you only want to see the aror lock lines of the middleware for example so then you can use a regular expression to use logical operators and so on so this for example will only show the fre lock lines here where it finds an error and the word middleware of course you can also do many other cool things basically everything that you can do in your regular expression you can also search for specific dat or specific times this is really really amazing to filter and troubleshoot lock files in containers now what you might also know from others of my videos is how you execute a shell or you attach a shell to a running container for troubleshooting and looking into the Container file system like for example if you execute an interactive terminal on the traffic container and run a bash command for example then you can open a bash shells so some Docker containers don't have the bash shells mostly they only use the default shell then you open a shell inside the containers file system and then you can look into any of the files that are in here you can also check any of the configuration files for example the traffic. yml to see if everything is correctly set up here and of course you can also do certain tests like an IPA to get the local IP address of this container and see what is the current route uh when you want to Ping an external host and you can check certain things like pinging the Gateway and maybe you also want to do some Advanced Network troubleshooting like executing a kill command so okay so that's not existing in here and this is an issue you will have most of the time when you're trying to troubleshooting networking conditions and container that most of the containers that you're running won't have all the troubleshooting tools that you need so what I found to be super useful is a container image image that already comes pre-installed with all of the network utilities and troubleshooting tools you might need and this is called the netsh shoot container so I'm using a simple Docker run command and I remove it afterwards it is called netsh shoot this will run the net shoot container image published by Nikola open this with an interactive terminal of course and just open the bash for example so now this will pull down the very very small inefficient container image and attaches a shell to it and as I said this comes with all of the common Linux utilities for troubleshooting Network conditions like Cur command for example if you want to get the output of a specific website you want to troubleshoot the IP addresses the redirection links whatever you want to see can also ping of course and what is also pretty cool you can even run Advanced commands in here for example the open SSL command to open a secured connection to a specific host that you want to troubleshoot here you can inspect certificates for example when you pipe this command to the open SSL and inspect the certificate you can look up the certificate chain and all of the troubleshooting or network troubleshooting things that you might want to do in containerized environments of course you can run this container attach it to a specific Network and can test if you can connect to a database or whatever so for example if you just run the same container but you want to attach it to the network front end for example so now I can see if I can for example ping the traffic container for example this is how you can troubleshoot that you can connect within the same Docker Network to other containers using name resolution so I'm using this so many times whenever I have any problems with networking in containerized environments of course you can also spawn up this container in kubernetes environments also pretty useful and trouble shoot and test any of those things by the way because I'm just talking about troubleshooting and lcks and so on I want to give you one bonus tip yeah let let's do one bonus tip and this is in Docker desktop so make sure if you're using the docker desktop application that you need to switch back the docker context to use the default one so otherwise you will see errors in here but now as you might know the docker desktop is really great to work with containers in a local environment now I recently came across those Docker extensions and these extension are are sometimes really nice for example I just found the locks Explorer extension so this is really great to troubleshoot those locks on Docker environments that you have on your local workstation you don't need to use a grab or CI tool can always type in something for example type in a certain keyword and this is displayed in a very very nice and clean way can also filter locks for standard out standard error output so just look for some of those extensions you will find some really great stuff in here okay so I hope you could learn the one or another cool command that you didn't know before and if you knew all of them well congratulations yeah you can now tell me in the comments which one I missed by the way you will also find all of these commands in my dock cheat sheets repository you can find on my GitHub so make sure to check it out I'll leave you a link in the description of this video down below now that's it for today happy home labbing every one and I catch you in the next video take care bye-bye
Info
Channel: Christian Lempa
Views: 21,939
Rating: undefined out of 5
Keywords:
Id: tNBwddCczK8
Channel Id: undefined
Length: 15min 32sec (932 seconds)
Published: Wed Feb 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.