Docker

Docker Basics Cheatsheet

A concise Docker cheat sheet for beginners, covering installation, common CLI commands, Dockerfiles, and core container workflows.

This Docker Basics Cheatsheet provides a quick reference for essential Docker commands and configuration patterns. Perfect for developers getting started with containerization.

🐳 Docker Basics

  • Install Docker for your OS.

  • Shows the client and server versions of Docker installed.

docker version
  • Displays system-wide information such as number of containers, images, storage driver, etc.
docker info
  • Lists all available Docker commands and options. Use docker <command> --help for detailed help on any specific command.
docker help

🛠️ Dockerfile Basics

A Dockerfile defines how to build a custom Docker image. Below is a simple example:

# Use an official Node.js image
FROM node:20-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of your application code
COPY . .

# Define the default command to run
ENTRYPOINT ["npm", "start"]

To build an image from this Dockerfile:

docker build -t my-node-app .

📦 Images

  • Downloads a Docker image from a remote registry (usually Docker Hub) to your local machine.
    • Example: docker pull ubuntu:20.04
docker pull <image-name>
  • Builds a Docker image from a Dockerfile in the current directory. The -t option tags the image with a name.
    • Example: docker build -t myapp .
docker build -t <image-name> .
  • Lists all images stored locally on your machine, including their IDs, sizes, and creation dates.
docker images
  • Removes a specific image from your local machine.
docker rmi <image-id or name>
  • 💡 Tip: Use docker rmi -f to force-remove an image even if it's in use.

📦 Containers

  • Creates and starts a new container from the specified image.
    • Example: docker run nginx
docker run <image-name>
  • Starts a container in interactive mode with a TTY, allowing shell access.
    • Example: docker run -it ubuntu bash
docker run -it <image> bash
  • Shows currently running containers with details like container ID, image, command, and uptime.
docker ps
  • Lists all containers, including those that have exited.
docker ps -a
  • Stops a running container gracefully (sends SIGTERM).
docker stop <container-id>
  • Starts a stopped container without creating a new one.
docker start <container-id>
  • Stops and then restarts a container.
docker restart <container-id>
  • Deletes a stopped container permanently.
docker rm <container-id>

🧰 Container Management

  • Runs a command inside a running container, often used to open an interactive shell.
    • Example: docker exec -it mycontainer bash
docker exec -it <container-id> <command>
  • Fetches and displays logs from a container’s stdout and stderr.
docker logs <container-id>
  • Outputs low-level JSON data about a container or image (network config, mount points, etc).
docker inspect <container-id>
  • Shows the active processes running inside a container.
docker top <container-id>
  • Copies files from container to local:
docker cp <container-id>:<path-in-container> <local-path>
  • Copies files from local to container:
docker cp <local-path> <container-id>:<path-in-container>
  • Displays live stream of container resource usage (CPU, memory, I/O).
docker stats

📁 Volumes

  • Creates a new Docker-managed volume.
docker volume create <volume-name>
  • Lists all Docker volumes on your system.
docker volume ls
  • Removes a specific volume.
docker volume rm <volume-name>

🌐 Networks

  • Lists all Docker networks (bridge, host, custom, etc.).
docker network ls
  • Creates a custom Docker network, useful for inter-container communication.
docker network create <network-name>
  • Deletes a custom Docker network.
docker network rm <network-name>
  • Shows configuration and connected containers for the specified network.
docker network inspect <network-name>

🧹 Clean-Up and Pruning

  • Cleans up all unused containers, networks, dangling images, and optionally volumes.
docker system prune
  • Removes unused images that are not associated with any container.
docker image prune
  • Removes all stopped containers.
docker container prune
  • Deletes unused volumes.
docker volume prune

That’s it for the Docker basics—keep this sheet handy as your quick reference. 🚀

© 2025 CodeFuzzify. All rights reserved.