docker Commands

Container Management

docker run

Run a command in a new container.

docker run

Examples:
Run container in background
docker run -d --name my-app nginx
Run with port mapping
docker run -d -p 8080:80 --name web nginx
Run with volume mount
docker run -d -v /host/path:/container/path --name app my-image
docker ps

List containers.

docker ps

Examples:
List running containers
docker ps
List all containers
docker ps -a
List container IDs only
docker ps -q
docker stop

Stop one or more running containers.

docker stop

Examples:
Stop container by name
docker stop my-container
Stop container by ID
docker stop abc123def456
docker start

Start one or more stopped containers.

docker start

Examples:
Start container by name
docker start my-container
Start container by ID
docker start abc123def456
docker restart

Restart one or more containers.

docker restart

Examples:
Restart container
docker restart my-container
docker rm

Remove one or more containers.

docker rm

Examples:
Remove stopped container
docker rm my-container
Force remove running container
docker rm -f my-container
docker exec

Run a command in a running container.

docker exec

Examples:
Execute command in container
docker exec my-container ls /app
Open interactive shell
docker exec -it my-container /bin/bash

Monitoring

docker logs

Fetch the logs of a container.

docker logs

Examples:
View container logs
docker logs my-container
Follow logs in real-time
docker logs -f my-container
Show last 100 lines
docker logs --tail 100 my-container

Image Management

docker build

Build an image from a Dockerfile.

docker build

Examples:
Build image with tag
docker build -t my-app:latest .
Build without cache
docker build --no-cache -t my-app:latest .
docker images

List images.

docker images

Examples:
List all images
docker images
List image IDs only
docker images -q
docker rmi

Remove one or more images.

docker rmi

Examples:
Remove image
docker rmi my-app:latest
Force remove image
docker rmi -f my-app:latest
docker pull

Pull an image or a repository from a registry.

docker pull

Examples:
Pull image from Docker Hub
docker pull nginx:latest
Pull from private registry
docker pull registry.example.com/my-app:latest
docker push

Push an image or a repository to a registry.

docker push

Examples:
Push image to Docker Hub
docker push my-app:latest
Push to private registry
docker push registry.example.com/my-app:latest

Networking

docker network

Manage Docker networks.

docker network

Examples:
List networks
docker network ls
Create network
docker network create my-network
Connect container to network
docker network connect my-network my-container

Storage

docker volume

Manage Docker volumes.

docker volume

Examples:
List volumes
docker volume ls
Create volume
docker volume create my-volume
Remove volume
docker volume rm my-volume