DEV Community

Huseyn
Huseyn

Posted on

Docker Cheatsheet

What is Docker?
Docker is a platform for developing, shipping, and running applications inside containers. It allows you to package an application with its dependencies, ensuring consistency across different environments.

Key Concepts

Container : A lightweight, standalone, executable package that includes everything needed to run an application (code, runtime, libraries, and dependencies).
Image : A read-only template used to create containers. Images are built from a series of layers, each representing an instruction in the image’s Dockerfile.

Basic Docker Commands

Managing Containers

docker ps: List all running containers.
Example: docker ps
Use: Check currently active containers.

docker ps -a: List all containers (including stopped ones).
Example: docker ps -a
Use: View all containers, running or stopped.

docker run: Create and start a container from an image.
Example: docker run -d -p 8080:80 nginx
Options:
-d: Run container in detached mode (background).
-p :: Map host port to container port.
--name <name>: Assign a name to the container.
-e <environemnt_variable>: set env variables like db user, pass etc

Use: Start a new container from an image.

docker exec: Run a command inside a running container.
Example: docker exec -it <container_id> /bin/bash
Options:
-i: Interactive mode (keep STDIN open).
-t: Allocate a pseudo-TTY (terminal).

Use: Access a container’s shell or run commands inside it.

docker stop: Stop a running container.
Example: docker stop <container_id>
Use: Gracefully stop a container.

docker rm: Remove a stopped container.
Example: docker rm <container_id>
Use: Delete a container to free up resources.

Managing Images

docker images: List all images on the system.
Example: docker images
Use: View available images locally.

docker pull: Download an image from a registry (e.g., Docker Hub).
Example: docker pull ubuntu:latest
Use: Fetch an image to use locally.

docker rmi: Remove an image.
Example: docker rmi <image_id>
Use: Delete an unused image to free up space.

docker build: Build an image from a Dockerfile.
Example: docker build -t my-app:latest .
Options: -t <name:tag>: Name and tag the image.

Use: Create a custom image from a Dockerfile.

Logs and Monitoring

docker logs: View logs of a container.
Example: docker logs <container_id>
Options: -f: Follow log output (like tail -f).

Use: Inspect container output for debugging or monitoring.

docker inspect : Display detailed information about a container or image.
Example: docker inspect <container_id>
Use: Get metadata, network settings, or configuration details.

System Management

docker info: Display system-wide information about Docker.
Example: docker info
Use: Check Docker version, storage, and running containers/images.

docker system prune: Remove all unused containers, networks, and images.
Example: docker system prune -a
Options:
-a: Remove all unused images, not just dangling ones.

Use: Clean up unused Docker resources.

Networking

docker network ls: List all networks.
Example: docker network ls
Use: View available Docker networks.

docker network create: Create a new network.
Example: docker network create my-network
Use: Set up a custom network for containers to communicate.

Tips

  • Always specify a tag (e.g., nginx:latest) when pulling or running images to avoid unexpected updates.
  • Use docker ps -q to get only container IDs (useful for scripting).
  • Combine docker exec with -itfor interactive commands like shells.
  • Regularly clean up unused containers and images with docker systemprune to save disk space.

Top comments (0)