Docker Cheat Sheet

This Docker cheat sheet contains all the basic commands, you use frequently while you are working with Docker. But, it is difficult to keep remember all these commands and you prepare stickies. But now, no more stickies. You can use this cheat sheet as your sticky notes. 

Build, Remove, List and tag an Image
Build an image from the Dockerfile in the current directory and tag the image
  • docker build -t <image-name:tag> .
    Example: docker build -t helloworld:1.0 .

  • docker build -t <image-name> .
    Example: docker build -t helloworld .

Note:
- Don't miss dot (.). This used to save image in current directory.
- tag can be a version for image.
- Tag is optional but image-name is Required.
List of Images
  • Show all Images available in local cache
    docker images
  • Show all Images for particular image with different version
    docker images <image-name>
  • Show for image particular image name and version
    docker images <image-name:tag>
Tag Images
  • Retag a local image with a new image name and tag
    docker tag myimage:1.0 myrepo/myimage:2.0
Delete the Image
  • Delete Image using image name and tag, if at build time you given tag name then you should use this command
    docker rmi <image-name:tag>
  • Delete Image using image name
    docker rmi <image-name>
  • Delete Image using image-id (run docker images command to get image-id)
    docker rmi <image-id>
Remove, List, Start, Stop one or more containers
Start Container
  • Start container using image-name
    docker start <image-name>
  • Start container using container-id (run docker ps command to get container-id)
    docker start <container-id>
Stop Running Container
  • Stop container using image-name
    docker stop <image-name>
  • Stop container using container-id (run docker ps command to get container-id)
    docker stop <container-id>
  • Stop container using container-id (run docker ps command to get container-id)
    docker kill <container-id>
  • Note:
    - Difference between docker stop and docker kill is docker stop gives the container to shutdown gracefully. But docker kill stop immediately.
List of running container/services
  • Shows only the list of running containers
    docker ps
  • Shows the list of both running and exited containers
    docker ps -a
Remove Container
  • Remove container using container name
    docker rm <container-name>
  • Remove container using container Id
    docker rm <container-Id>
  • Note:
    - Container/Image name and Container id you can get using docker ps command
Access running container
  • docker exec -it <container-name> /bin/bash ;
Show logs Login, Pull and Push image in docker hub registry
  • Show all logs since deploy
    docker logs <container-name>
  • Show logs since timestamp
    docker logs --since=5m <container-name>
  • Note:
    - 5m for 5 minutes
    - 5s for 5 seconds
    - 5h for 5 hours
  • Show number of lines from the end of the logs
    docker logs --tail=200 <container-name>
  • Note:
    - will show last 200 lines
  • Login to the docker hub registry
    docker login
  • Pull an image from a registry
    docker pull <container-name:tag>
  • Push an image to a registry
    docker push <container-name:tag>
Run service
  • Run a container and will show the logs
    docker run --name=<container-name>
  • Example: docker run --name=myImage

  • Run a container and Automatically remove the container when it exits
    docker run --rm --name=<container-name>
  • Example: docker run --rm --name=myImage

  • Run a container and will not show the logs
    docker run --d --name=<container-name>
  • Example: docker run --d --name=myImage

  • Run a container with expose port 5000 externally, mapped to port 4000 inside the container
    docker run --p <host:external-port:internal-port> --name=<container-name>
  • Example: docker run -p 0.0.0.0:5000:4000 --name=myImage

Comments