Essential Docker Commands

Image Commands:

docker pull: Pull an image from a registry.

Example: docker pull nginx:latest

docker build: Build an image from a Dockerfile.

Example: docker build -t myapp:1.0 .

docker images: List all images on the local machine.

Example: docker images

docker rmi: Remove an image.

Example: docker rmi image_id

Container Commands:

docker run: Run a container from an image.

Example: docker run -d -p 8080:80 myapp:1.0

docker ps: List running containers.

Example: docker ps

docker stop: Stop a running container.

Example: docker stop container_id

docker rm: Remove a stopped container.

Example: docker rm container_id

docker exec: Execute a command in a running container.

Example: docker exec -it container_id bash

docker logs: View logs of a container.

Example: docker logs container_id

Volume Commands:

docker volume ls: List all volumes.

Example: docker volume ls

docker volume create: Create a new volume.

Example: docker volume create myvolume

Network Commands:

docker network ls: List all networks.

Example: docker network ls

docker network create: Create a new network.

Example: docker network create mynetwork

Docker Compose Commands:

docker-compose up: Start services defined in a docker-compose.yml file.

Example: docker-compose up -d

docker-compose down: Stop and remove services defined in a docker-compose.yml file.

Example: docker-compose down

docker-compose ps: List services and their status.

Example: docker-compose ps

Swarm Commands (for orchestration):

docker swarm init: Initialize a Docker swarm.

Example: docker swarm init

docker service: Manage Docker services in a swarm.

Example: docker service ls

System Information:

docker version: Display Docker version information.

Example: docker version

docker info: Display system-wide information about Docker.

Example: docker info

Image Commands:

Tag an Image:

Example: docker tag myapp:1.0 myregistry/myapp:1.0

Explanation: Tags an image with a different name, providing a way to reference the same image under different names.

Remove Unused Images:

Example: docker image prune

Explanation: Removes all dangling (unused) images.

Remove All Images:

Example: docker rmi $(docker images -q)

Explanation: Removes all images on the local machine.

Container Commands:

Remove Stopped Containers:

Example: docker container prune

Explanation: Removes all stopped containers.

Remove All Containers:

Example: docker rm -f $(docker ps -aq)

Explanation: Removes all containers, including running ones.

Volume Commands:

Remove Unused Volumes:

Example: docker volume prune

Explanation: Removes all unused volumes.

Network Commands:

Remove Unused Networks:

Example: docker network prune

Explanation: Removes all unused networks.

System Cleanup:

Remove All (Containers, Images, Volumes, Networks):

Example: docker system prune -a

Explanation: Removes all stopped containers, unused networks, dangling images, and unused volumes.

Remember to exercise caution when using commands that remove resources, as they can result in data loss. Always double-check the resources you are about to remove.