Day 20 - Docker CheatSheet

Day 20 - Docker CheatSheet

90 DaysOfDevOps

Docker is an open source software platform used to create, deploy and manage virtualized application containers on a common operating system (OS) with the aid of certain tools.

Installing Docker Engine

TerminologyMeaning
ImageA standalone, virtual and executable "package" that can run particular software configured inside it.
ContainerIt provides isolated environment in order to achieve the separation between the host system environment and the environment contained within the image.
EngineIt is an open source containerization technology for building and containerizing your applications. Acts as a client-server application with the server, api and the docker CLI(client).
HubIt iss a web-based storage platform for public/private access to docker images.

Commands

Running a container

  • Start a container from image

      docker run <image>
    
  • Start a container with custom name

      docker run --name <your_name> <image>
    
  • Start a container in detached mode

      docker run -d <image>
    
  • Start a container with Port mappings

      docker run -P [host_port]:[container_port] <image>
    
  • Start a container with a hostname

      docker run --hostname <your_name> <image>
    
  • Start container in interactive mode

      docker run -it <image>
    
  • Start container with a specific entrypoint

      docker run --entrypoint <executable> <image>
    
  • Start a container by mapping a local directory into the container

      docker run -v <host_dir>:<target_dir_in_container> image
    

Operations on images

  • Build an image from Dockerfile

      docker build <directory>/<url>
    
  • Download an image from DockerHub

      docker pull <image>:[TAG] # can also specify version/TAG
      #default is the 'latest' version
    
  • List all the images

      docker images
    
  • Remove all containers

      docker rmi $(docker images -q)
    
  • List dangling images

      docker images -f dangling=true
    
  • Remove all dangling images

      docker rmi $(docker images -f dangling=true -q)
    
  • Give tag to an image

      docker build tag <name>/<image_ID> <new_name>:[tag]
    
  • Save an image to a tar file

      docker save <image> > <name.tar>
    
  • Load an image from a tar file

      docker load -i <tar_file>
    
  • Stop or remove all the containers at once

      docker rm -f $(docker ps -aq)
      docker stop -f $(docker ps)
    
  • List all the running containers

      docker ps
      docker ps -a # list all active and stopped containers
    
  • Stop a container

      docker stop <container1> <container2> ...
    
  • Delete a container

      docker rm <container1> <container2> ...
      docker rm -f <container1> <container2> ... # force delete
    
  • Restart a running container

      docker restart <container1> <container2> ...
    
  • Start a stopped container

      docker start <container1> <container2> ...
    
  • Execute a command within a running container

      docker exec <container> <command>
      # for e.g.
      docker exec -d mycontainer touch /tmp/execWorks
    
  • Copy files from localhost to container and reverse

      docker cp <source_path> <container>:<dest_path> # localhost to container
      docker cp <container>:<source_path> <dest_path>
    
  • Rename a container

      docker rename <old_name> <new_name>
    
  • Create an image from a container

      docker container commit -a "author" -m "message" <container> <image_name>
    
  • Delete all the stopped containers

      docker prune $(docker ps -a)
    

Monitoring

  • Show overall stats of all containers

      docker stats <container>
    
  • Show logs of a container

      docker logs <container>
    
  • Show processes running in a container

      docker top <continer>
    
  • Show mapped ports of a container

      docker port <container>
    
  • Detailed info about any docker entity

      docker inspect <image>|<container>|<entity>
    
  • List files/directories added, edited or deleted from a container compared to the base image

      docker diff <container>
    

Happy Learning ;)