Docker Commands
Image
  • contains the container's filesystem, it must contain everything needed to run an application - all dependencies, configuration, scripts, binaries, etc.
  • Show Images
  • # show a list of all images on the local system
    docker images
    docker image ls
            
  • Remove Images
  • # remove an image
    docker image rm [imageName]
    docker rmi [imageName]
    
    # remove all images
    docker rmi $(docker images -a -q)
            
  • Pull Image
  • # fetch an Docker image from Docker Registry
    docker pull [imageName]
    docker image pull [imageName]
            
  • Create Image
  • # docker build -t [imageName:tagName] [sourceCodeDir]
    docker build -t [imageName] .
    docker image build -t [imageName] .
            
  • Push Image
  • docker push [imageName:tagname] # push the image with the tag to Docker Hub, default is latest
    docker image push [imageName:tagName]
            
  • Create a Virtual Image
  • docker tag [imageName:tag] [imageName:tag] # create a refernce to the same image
            
  • Save Image
  • # save an image as a tar file
    docker save -o name.tar [imageName]
    docker image save -o name.tar [imageName]
            
  • Load Image
  • # load an image from a tar file
    docker load -i name.tar
    docker image load -i name.tar
            
  • Inspect Image
  • # display detailed image information
    docker inspect [imageName]
    docker image inspect [imageName]
            
  • Import Image
  • docker image import [name.tar] [imageName]
    docker import [name.tar] [imageName]
            
    Container
  • Run an image
  • # Docker client finds the image, loads up in a new container, runs a command in that container
    # Default command is defined in Dockerfile
    docker run [imageName:tag] [command]
    
    # -it, attaches to an interactive tty in the container, can be used to explore the image system
    docker run -it [imageName] sh # access the Linux system of a container
    
    docker run busybox echo "hello from busybox" runs a command in container busybox
    docker run -it busybox sh
    
    # -d, detach the terminal, running on backend
    # -P, publish all exposed ports to random ports
    # -p [docker_host_port]:[container_port], map port number
    # --name, give the container a name
    # --network, configure network
    # -e, environment variables
    docker run -d -P --name [containerName] [imageName]
    docker run -p 80:8080 [imageName]
    
    docker container run [imageName]
            
  • Inspect Container
  • docker inspect [containerName]
    docker container inspect [containerName]
            
  • Show Containers
  • # show containers running
    docker container ls
    docker ps
    
    # show containers exit
    docker container ls -a
    docker ps -a
            
  • Copy
  • docker cp [containerID/containerName]:src_path local_dest_path # copy a container file to local
    
    docker cp local_src_path [containerID/containerName]:container_dest_path # copy a local file to a container
            
  • Access a Container
  • docker attach [containerName]
    
    or
    
    docker exec -it [containerName] sh
    docker container exec -it [containerName] sh
            
  • Convert Container to Image
  • docker commit [containerID/containerName] [imageName] # create a image from a container
            
  • Start Container
  • docker start -ia [containerName]
    docker container start [containerName]
            
  • Restart Container
  • docker restart [containerName]
    docker container restart [containerName]
            
  • Stop Container
  • docker stop [containerName] # stop a container running
    docker container stop [containerName]
    
    docker kill [contaierID] # kill a running container
            
  • Remove Container
  • docker rm [containerID/containerName] # remove a container that is not running
    docker container rm [containerName]
    docker rm $(docker ps -a -f status=exited -q), remove all exited containers
            
  • Display Container Log
  • docker container logs [containerName]
            
  • Export Container
  • docker container export -o name.tar [contianerName]
            
    docker create --name [containerName ]-it [imageName] [command] # create a container
    docker start -ia [containerName] # start the container
            
    Network
  • List networks
    docker network ls
            
  • Inspect network
  • docker network inspect [networkName]
            
  • Create network
  • docker network create -d [driverName] [networkName] # default driver is bridge
            
  • Connect network
  • docker run -itd --network=[networkName] [image] # start a container and connect it to a network
    
    # ip, specify the IP address a container on a network
    docker network connect --ip [ip] [networkName] [containerName] # connect a running container to a network
            
  • Disconnect network
  • docker network disconnect [networkName] [containerName] # disconnect a container from a network
            
  • Remove network
  • docker network rm [networkName], remove a network
    
    docker network prune, remove all networks
            
    Volume
  • Create Volume
    docker volume create [volumeName]
            
  • List Volumes
    docker volume ls
            
  • Inspect Volume
    docker volume inspect [volumeName]
            
  • Remove Volume
    docker volume rm [volumeName]
            
  • Docker Registry
  • Docker Hub
  • docker login # log in Docker Hub
            
    Others
  • docker [command] --help, get the help information of a command
  • Reference
  • Docker Tutorial
  • Docker CLI