Mastering Docker | Essential Commands & Cheatsheet for Developers | @Javatechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Javi as a developers we often get to experiment with Docker but remembering all those Docker commands can be a top job isn't it that's why I am making this Docker command cheat sheet video to make it easier for you so in this tutorial I will show you the essential Docker commands with practical examples this will help you get better at managing the containers and familiar with all the ER commands okay all right so without any further delay let's get [Music] started before I start this tutorial I am assuming that you have some basic understanding of Docker if not don't worry you can go to my YouTube channel and check for the playlist called Docker there you can understand the basic of Docker what is Docker why do we need Docker in real time and their commands Okay so I'll share this link in video description for your reference now let's Circle back to the context so I have created one simple springboard application where I have defined two rest end point one will return the string messages and other one will return the list of user object okay so what we want to do we want to run this application user service in Docker container okay so for that the first step you need to build Docker image of your application so to build a Docker image of your application the first thing you need a Docker file okay so I have created a simple Docker file here if you check the command I have defined or the instruction I have defined in this Docker file I'm using the open jdk7 as a dependency to run my user service application then I have just created one working directory called app then whatever the jar of my application I'm copying from the target to this particular directory working directory what I created here then I want my application to run on Port 9191 okay my application will run in 9191 Port in container and then I'm giving the command to execute my jar okay this is the simple thing you need to Define in your Docker file now once you have the docker file the first step you need to do build Docker image this is the first step we need to perform okay so to build a Docker image make sure first you need to start your Docker desktop on your machine it must need to up and running before you execute any Docker specific command Okay so once it is up and running then you can go to the terminal I'll go to the terminal here then you need to trigger the command to build a Docker image or to dockerize your spring boot app so the command is very simple you just need to trigger Docker build then what is the target what is the name of your image you want to keep let's say I want to keep the image name as user app okay and what is the what is the tag you want to assign to your image I want to Define 2.0 then provide the root directory just enter it see the command is very simple right build the image and this is what the image name I want and this is what the tab now if you see it will execute all the steps you have defined on your Docker file see Here If You observe carefully it created the working directory then it copy the jar of your app to the Target all the steps is getting executed okay next to validate whether the image is created in Docker or not you can trigger the command Docker images it will list down all the available image on your Docker container okay just enter it can you see the image we created user app with the tag 2.0 and this what your image ID okay and better if you'll go to the dock desktop here also you can visualize your image name of your image ID of ID of your image what is the tag when it created and what is the size of your image whether it is being used by any container or not everything you can visualize from here from the command prompt okay now so let's do one thing let's note down these two command to build a Do image this are the command let me note it down now another command we execute check available Docker images and the command is now what is the third step once you build the docker image you want to run that particular image in Docker container okay so run image in container so go to the terminal to run the image in a container you need to Simply run Ducker run I want to run it in a detou mode then I want to define the port let's say I want to Define 9090 colum the port where you exposed your app If You observe in your Docker container I'm exposing my app to run on put 9191 inside the container so just Define here 9191 so this 9090 is the host Port I mean as a user I cannot directly access the 9191 port which is running on container then how can I access it by doing the port forwarding I'm telling to the docker hey if I'll send the request to 9090 then please delegate that request or forward that Port request to 9191 where my container is running okay then what is the image name so for that you can Define your image name user app with the tag 2.0 if you want you can also Define the image ID if you don't want to specify the name and tag and if you want to start the container with your image ID you can copy this you can also paste it here not this one let me check the image ID let's run it now to validate whether it start the container or not you can run the command Ducker PS we can see the container is started with the container ID this this is the image and this is the command to execute your jar and it started the container and you can see the port forwarding here now I need to access to this particular Port 9090 then the request will delegate to the container with the port 9191 because that is what I'm exposing here okay simple thing let's copy this command and paste in our note book you can let's let's define here image ID okay then to validate available Docker containers or to just check the command is Docker PS okay so we validate that our image is successfully run on the docker container with this particular Port now if you visualize the same in the docker desktop if will go and check the containers you can find here right but if You observe it is having some different name but if you want to specify your own name of your container that also you can do with a single change let's say I'll just Define here I will just Define the name here okay just Define here here my user app container I need to change the port here again because already 9090 Port is being used so I'll just change it to something some different 70 70 now if I'll run it it started another container if I'll just f figure it out Docker dot Docker PS I must see there will be two container can you see here with the name which I Define and with the anonymous one now if you validate here you can see the container with your name now any of the container you can access so let's say if I want to access this gretings Endo this one I'll go to the browser and I'll just type slash greetings can I see here we are seeing the string messages now let me trigger another endpoint which is users can I see all the four hardcoded user object now I am able to access using 9090 also I can able to access using 70 0 70 can you see here but if I try accessing using 91 91 it's not possible because 9191 is the port is being used by inside the container to access the application from container you need to go through the host Port what you have defined while drawing your container 7070 9090 okay this is pretty clear next if you want to stop any running container then you can do that just copy the container ID you can simply type Docker stop give the container ID and if you want to start it again you can do Docker start then container ID okay it will again start so let's stop one of the container now let me note down these commands fine okay now let's move to the next one let's say I want to check the log of my container so what I can do check logs of container you can simply do go to the terminal just do the let me clear this run docker PS only one container is running if I want to check the log of this particular container I can check Docker logs then give the container ID just enter it you can see this is our spring board app we can see the startup log okay if you have any log statement added to the application if you trigger that then you can find those logs inside the container this is straight forward so let's note it down Fine these are the simple commands we learn so far now let's go one step ahead and deug the container so to debug the container I need to open the terminal or what I need to do I need to execute a B cell inside the running Docker container so execute BAS sell inside a container so I need to log to the container and check what all things are there same thing you can verify from the this particular page itself I mean this Docker dtop itself so if you go here and which is in use this one right if you open this container link you can find the logs you can also find all the available files okay can you see here it created the working directory called app and user hyen service. jar is inside the working directory that is what we have defined in our Docker file here right so if you want to validate the same thing from the container itself we can go using the command Docker execute okay integrated terminal then give your container ID so if I'll go here if I'll copy this this is what my container ID then I can simply paste the container ID then I just want to b/b okay now if I do PWD I'm inside app directory this is what the directory we created through the docker file now if I'll do LS I can see the jar file okay so let's copy this command for reference fine now let's check the another interesting command which is Docker commit okay so why we need this Docker commit simple thing so if You observe this particular container is up and running this one if you go here this particular container is up and running so I got some task to update some library or do some modification in the container but I don't want it to be impact rather I want to create another image from this running container do the changes but that changes will not impact your existing container running container rather you want to create a separate Docker image from the container itself how you can do that that is where this Ducker commit came into the picture for example let's say this is the running container right so what I'll do let me create some folder and instead that I'm adding something like some file index. HTML also I'm adding some text now if I'll do LS I can see home index some text user service. jar so I will go inside the home there also I'll add some context okay so touch some different file touch data. SH now I'll do LS inside the home folder I have two I have added two different file and in the app directory I have added two different file let's assume you did some library upgradation or you did some some realtime implementation here okay but to just demonstrate I just created two different file now this file I created on the running container but if I will stop this container these things will not be available okay but I want these things to be part of a new image rather than changing in the same container it might impact right this container is being used by some user I don't want them to be stay in the conflict rather I want to achieve the same thing but with the different images so how you can do that very simple step just terminate this terminal just do Docker PS this is what my container where I have done the changes so to validate that whether the changes is there or not go to The Container go to the files inside the app what happened modified yeah we can see the icon modified but it is not allowing me to check from here but somehow you can see the HTML file right that's fine so what I want to do I want to create another image the changes what I made in this container okay so for that what you can do dock commit what is the container ID from this container ID I want to create a image let's say my app of version B2 okay something like that from this container with the changes I want to create this particular image with this particular tab just enter it it created the container now to validate that just do Ducker images can you see here it created another image my app B2 and if you'll check Ducker running container it didn't start the container right we need to start it manually so to start that what I can do to how we can start the container simple right we already covered that Ducker run then in detached mode then Define the port where you want to run run let's say 7171 and the port what we are exposing is 9191 the right part of the port must be the same what you have defined in your Docker file okay left part is uise because that is your host put you are giving to forward your request to this 9191 then just what do you need to do just give the container ID also it's good to specify name of your container right so name give any name May app P2 container okay so my bad I'm just giving here container ID which is wrong we want to run the image then it will start a container right so let me clear this I'll just do here Ducker images then I'll run the same command but I need the image ID to be run here not container okay it started the container now if you'll check Docker PS you can find the new container if you check here it created the container okay now if we go here container and if you'll check the files if you'll go inside app can you find the folder we created index.html sum. txt inside home you can find data Dosh home.html whatever we created is available with our new image now validate Same by log to The Container or just open the B terminal in the container okay so for that what I can do Ducker execute integrated terminal then what is the container ID this one right slash bin SLB now if you'll do PWD you inside app if you'll do LS you can find these files same what we just observe in the doer desktop fine but if you observe your old containers if you'll go here this one still you can see the modified with the value but once this particular container will be crashed and it will be restarted then this information will be automatically removed from the container okay but this particular containers always you'll find the script or whatever the things we added because that is what we copied from this particular container with modifying something using Docker commit fine now let me exit from terminal let me keep a note Ducker commit then container ID then your new image name new image with tag fine now successfully we validated all the command in our local about the docker creating the image build it run it and do the docker commit start the container stop the container lot of things we have learned now these images what I created is available on my machine it cannot be accessible by others right so it means I need to store these images to some registry some global registry or some public registry so that others can access it so for that what you can do the next step you need to push your image to Docker Hub Docker Hub is nothing a registry to store your images but to push your images to Docker hub first what you need to do there are few point you need to remember you need to tag your images the reason to tag your images let's say I'm developing something called user app okay I'm not the only one who created the user app and pushed to the docker Hub there can be millions of user who created the app with the same name user SL app then how can I identify which is I created so I need to tag something like this right now I can understand okay this particular us user app is being this particular user okay so this is nothing your dockerhub ID unique ID okay based on that you need to tag your images once you tag your images then you can push it so first step tag your images then push that images before that since you want to connect your local to the docker hub you just need to do the docker login First you need to log to the docker hub from your terminal then tag your images then push that images okay simple thing but make sure to perform this push image to the hub you must have a account in Ducker Hub so I have already the account just type here Docker Hub then just open the first link do the sign in it won take much time it is 2 minute job to create the dockerhub account okay so if You observe here all my images what I have pushed to the dockerhub is tagged with the my Docker id dockerhub id Java okay so see here simple let's consider this pring Agro CD app okay I'm not the only single person who created this spring Agro CD app and push to the docker Hub so there can be 100 user who created the app with the same name but to uniquely identify the image belongs to which Community or which user it it's good practice to tag your images okay so we are good now what steps you want to perform to push the image to Docker Hub you need to do the docker login First go to the terminal just do Docker login so already I login before so it is taking my old credential so what I do I will just do Docker log out now if I'll do Docker login again it will ask me to give the username and password my username is Java password I don't want to share I will type it yeah can you see here once you provide the valid username and password you'll see the statement login succeeded okay once you login then just let me clear this just check your images available images my app and user app there are two app okay so what I want to do I want to tag my image first so what is the image let's say I I'll just Define Docker tag which image you want to tag let's say I want want to tag this particular my app okay my app of tag B2 or the version whatever you can consider two this is my user okay this is see Simple Thing Docker tag this is the current image which I want to push to the docker Hub by appending this ID okay just enter it so we seeing the error because my app so there is a hyphen in between just add it just enter okay now if I'll check Ducker images can I see my image with the tag it create another image with the tag okay so it means I successfully tagged my image with my dockerhub ID now once you tag the images next what you can do you can push your image to the hub so to push your image to the hub the simple command is let me type Docker images then you can simply do which image you want to push I want to push Docker P what is the image name this one of tag B2 okay just enter it it will take couple of second to push the images to the hub and I have done some example before so it is using their configuration so you can see the statement here right it push the images to validate that what you can do go to the docker Hub and refresh this and you can see these images last post less than a minute ago now if you open this I have the single tag called B2 now if I open the public view this is completely public so anyone can pull the images by using this particular command see to pull the images I'll just keep a note here you can run Docker pull then what image you want to pull similarly to push the image to the docker Hub what command we have typed here push image the command is same right only you need to change to push okay so push image successfully and if you want to use my images you can do the Ducker pull and this is my image name with the tag and this is are the version so just give a try just p pull this images on your machine and run it and see the result okay so similarly if I want to use some other software for example let's say I want to use my SQL on my machine I will search in the hub about the my SQL and you can find there are supported version of my SQL what I can do I'll will I will copy this particular command Docker pool my SQL any images you need to run on your docker container come to the hub and filter it and then go and run here I mean you can run from the intelligent terminal as well I'll just run Docker pull my SQL it will take couple of second because it will pull the images from Hub and will be stor in your Docker container I mean your Docker desktop Docker engine whatever you can pronouns you can see here it is suceeded now to validate whether it really created the image or not what I'll do I'll just check Docker images can you see here my SQL with the latest tag so you can push the images to the hub if you need some software to run on your container you can also pull that images from the Hub and here is the command Okay so these are the essential s Ducker commands which as a user or as a developer or as a administrator you must need to idea about these commands and there are few hidden commands of Docker which I will cover in my next tutorial so do let me know in a comment section if you find this tutorial helpful okay and if you have any doubt then just raise the concern in the comment itself that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 6,899
Rating: undefined out of 5
Keywords: javatechie, docker, docker commands, javatechie docker
Id: LimCWMFduC8
Channel Id: undefined
Length: 32min 2sec (1922 seconds)
Published: Fri Apr 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.