Docker - For Beginner Part 3

Docker - For Beginner Part 3

Installation & Basic Commands Of Docker

Installation Of Docker On Ubuntu :

  1. Remove old installation if it is installed

     sudo apt-get remove docker docker-engine docker.io containerd runc
    
  2. Update the Ubuntu packages & install the packages

      sudo apt-get update
      sudo apt-get install ca-certificates curl gnupg
    
  3. Install Docker official GPG key

      sudo install -m 0755 -d /etc/apt/keyrings
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
      sudo chmod a+r /etc/apt/keyrings/docker.gpg
    
  4. Command to set up the repository:

     echo \
       "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
       "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
       sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    
  5. Install the latest Docker package

      sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    
  6. Verify the docker installation

      sudo docker --version
    

Basic Commands :

  1. Check version

     docker --version
    
  2. To check all downloaded / present docker images

     docker images
    
  3. Docker Login

     docker login
    
  4. To find the images in the docker hub

     #docker search <nameofimage>
     docker search jenkins
    
  5. Download any image

     #docker pull <nameofimage>
     docker pull jenkins
    
  6. To check the service

     service docker status
    
  7. Start Container

     #docker start <containername/containerid>
     docker start mynginx
    
  8. Stop Container

     #docker stop <containername/containerid>
     docker stop mynginx
    
  9. Kill Container

     #docker kill <containername/containerid>
     docker kill mynginx
    
  10. To see all running container

    docker ps
    
  11. To see all running and stopped container

    docker ps -a
    
  12. Remove the docker image

    #docker rmi <imagename>
    docker rmi jenkins
    
  13. Remove the container

    #docker rm <containername/containerid>
    docker rm jenkins
    
  14. Prune the container

    docker system prune
    
  15. Create the image from the docker file

    #docker build -t <tagname> <absoulte_path>
    docker build -t LatestNginx /home/docker/Dockerfile
    OR
    docker build -t LatestNginx .
    
  16. Create a container

    #docker run -it --name <nameofcontainer <imagename> /bin/bash
    docker run -it --name LatestUbuntu Ubuntu /bin/bash
    
  17. Connect to the container

    #docker attach <containername/containerid>
    docker attach LatestUbuntu
    
  18. Check the logs of a container

    docker container logs <containername/containerid>
    docker logs abebb534sff3