Useful Docker Commands 2020

    By: Manu
    3 years ago

    Here we have docker most used commands, This page is updated and we try to make it updated. If you want any more commands here you can add a comment and we will update this page. All commands are working with latest docker version available today



    Show all docker commands

    docker
    


    Check Docker version

    docker version
    


    Docker information

    Docker info
    


    List of all containers

    docker ps
    
    or
    
    docker container ls
    


    Create and run container in foreground


    docker container run -it -p 80:80 imageName
    
    //incase we are creating container from nginx image
    
    docker container run -it -p 80:80 nginx
    


    Create and run container in background


    docker container run -d -p 80:80 imageName
    
    //incase we are creating container from nginx image
    
    docker container run -d -p 80:80 nginx
    


    Give name to a container


    docker container run -d -p 80:80 --name anyNameYouWant nginx
    
    //In case setting name equals nginx_local
    
    docker container run -d -p 80:80 --name nginx_local nginx
    


    List all containers running + not running


    // To see list of all running containers
    
    docker container ls 
    
    // To see list of running and stopped containers
    
    docker container ls -a
    


    Stop a container


    docker container stop containerIdHere
    


    Stop all running containers


    docker stop $(docker ps -aq)
    


    Remove all containers


    docker rm $(docker ps -aq)
    
    //remove using force
    
    docker rm $(docker ps -aq) -f
    


    Remove container


    //remove single container
    
    docker container rm ContainerIDHere
    
    //remove using force
    
    docker container rm ContainerIDHere -f
    
    //removing multiple containers
    
    docker container rm ContainerIDHere AnotherContainerIDHere AnotherContainerIDHere 
    


    Get logs


    docker container logs ContainerName
    


    Inspect a container



    docker container inspect ContainerName
    


    Getting Into container


    docker exec -it ContainerId sh
    
    //incase creating a container and bash into it 
    
    docker container run -it ImageName bash
    







    *********************

    Dealing with Images

    *********************






    List of all images

    docker images
    


    Pull Image from docker hub


    docker pull imageNameHere
    
    //Incase of nginx image
    
    docker pull nginx
    


    Remove all images


    docker rmi $(docker images -a -q)
    
    //using force
    
    docker rmi $(docker images -a -q) -f
    


    Remove image


    docker image rm ImageIdHere
    





    *********************

    Docker Networks

    *********************


    List all available networks


    docker network ls
    


    Inspect a network

    docker network inspect networkNameHere
    


    Create network


    docker network create networkName
    


    Create container on a network


    //incase of image is nginx
    
    docker container run -d --network networkNameHere nginx
    


    Connect existing container to network


    docker network connect networkNameHere containerNameHere
    


    Disconnect container from network


    docker network disconnect networkNameHere containerNameHere
    


    Detach network


    docker network disconnect
    




    *********************

    Docker File

    *********************


    Building image from Docker File

    docker image build -t customImageName
    


    Running image


    docker container run -p 80:80 --rm customImageName
    




    *********************

    Bind Mount

    *********************


    Setting up Bind Mount


    docker container run  -p 80:80 -v $(pwd):/usr/share/nginx/html nginx