Mastering Docker: Your Essential CLI Cheat Sheet with Examples

Docker has revolutionized the way we develop and deploy applications. This cheat sheet provides a clear overview of essential Docker commands, examples, and installation guidance for different operating systems. Get ready to unlock the power of containerization!

Building Images:

  • Build an Image: docker build -t <image_name> . – Builds an image from your current directory’s Dockerfile.
  • Cacheless Build: docker build -t <image_name> . --no-cache – Ignores cache and rebuilds from scratch.

Image Management:

  • List Images: docker images – Displays all local images.
  • Delete Image: docker rmi <image_name> – Removes the specified image.
  • Clean Up: docker image prune – Removes unused images locally.
  • Login to Docker Hub: docker login -u <username> – Provides access to public/private repositories.
  • Publish Image: docker push <username>/<image_name> – Uploads your image to Docker Hub.
  • Search Docker Hub: docker search <image_name> – Finds images publicly available.
  • Pull Image: docker pull <image_name> – Downloads an image from Docker Hub to your local system.

Container Operations:

  • Run a Container: docker run --name <container_name> <image_name> – Creates and runs a container with a custom name.
  • Port Mapping: docker run -p <host_port>:<container_port> <image_name> – Exposes a container port to the host machine.
  • Background Mode: docker run -d <image_name> – Runs the container in the background.
  • Start/Stop: docker start|stop <container_name> – Manages existing containers.
  • Remove: docker rm <container_name> – Deletes a stopped container.
  • Shell Access: docker exec -it <container_name> sh – Opens a terminal inside a running container.
  • View Logs: docker logs -f <container_name> – Shows real-time log output of a container.
  • Inspect: docker inspect <container_name> – Details information about a running container.
  • List Containers: docker ps – Displays currently running containers.
  • All Containers: docker ps --all – Shows running and stopped containers.
  • Resource Usage: docker container stats – Monitors resource usage of running containers.

Examples:

1. Running a simple web server:

docker run -p 80:80 nginx:latest

This starts an Nginx web server container, mapped to host port 80, serving content from the default Nginx document root.

2. Building a custom Node.js app:

  • Create a Dockerfile with instructions.
  • Use docker build to build an image from the Dockerfile.
  • Run the image with docker run to start your application.

Docker Installation:

– Windows: Download and install Docker Desktop from https://docs.docker.com/desktop/install/windows-install/

– macOS: Install Docker Desktop from https://docs.docker.com/desktop/install/mac-install/

– Linux: Use your distribution’s package manager or follow instructions at https://docs.docker.com/engine/install/

Further Exploration:

Remember, this cheat sheet provides a starting point. Experiment with Docker commands and explore its vast potential!

1 thought on “Mastering Docker: Your Essential CLI Cheat Sheet with Examples

Comments are closed.