DEV Community

Huseyn
Huseyn

Posted on • Edited 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.

Command Description
docker build -t my-app . Build an image from Dockerfile in current directory.
docker images List all images on local machine.
docker rmi <image_id> Remove an image.
docker pull <image> Pull an image from Docker Hub.
docker push <image> Push your image to a registry.
Command Description
docker run -it --name my-container -p 3000:3000 my-app Run a container from an image interactively.
docker ps List running containers.
docker ps -a List all containers, running or stopped.
docker stop <container_id> Stop a running container.
docker rm <container_id> Remove a stopped container.
docker logs <container_id> View container logs.
docker exec -it <container_id> /bin/bash Open shell in running container.
docker container inspect container_name Inspect Container
Command Description
docker-compose up Start all services defined in docker-compose.yml.
docker-compose down Stop and remove all services.
docker-compose build Build/rebuild images for services.
docker-compose logs View logs of all services.
Command Description
docker network ls List all available Docker networks.
docker network inspect <network_name> Show detailed info about a specific network (subnet, containers, driver, etc.).
docker network create mynet Create a new custom network (default: bridge).
docker network create --driver bridge mynet Create a network explicitly using the bridge driver.
docker network create --driver host hostnet Create a network using the host driver (shares host network).
docker network create --driver overlay overnet Create an overlay network (multi-host, Docker Swarm).
docker network rm <network_name> Delete a specific network (only if no container is using it).
docker network prune Remove all unused networks.
docker run -d --name app --network=mynet myimage Run a container and attach it to mynet.
docker network connect mynet mycontainer Connect an existing container to mynet.
docker network disconnect mynet mycontainer Disconnect a container from mynet.
`docker inspect <container_id> grep -A 5 "Networks"` Show which networks a container is connected to.

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)