1. Container Management
- List running containers:
docker ps
- List all containers (including stopped):
docker ps -a
- Run a container:
docker run -d --name container_name image_name
- Run a container with a specific network and port mapping:
docker run -d --name container_name --network network_name -p host_port:container_port image_name
- Stop a container:
docker stop container_name
- Restart a container:
docker restart container_name
- Remove a stopped container:
docker rm container_name
- Remove all stopped containers:
docker container prune
- Exec into a running container:
docker exec -it container_name /bin/bash
- View container logs:
docker logs container_name
- View real-time logs:
docker logs -f container_name
- Scale services:
docker-compose up -d --scale service_name=num_instances
2. Image Management
- List all images:
docker images
- Build an image from a Dockerfile:
docker build -t image_name .
- Tag an image:
docker tag image_name repository/image_name:tag
- Push an image to a registry:
docker push repository/image_name:tag
- Pull an image from a registry:
docker pull repository/image_name:tag
- Remove an image:
docker rmi image_name
- Remove unused images:
docker image prune
- Export an image:
docker save -o image_name.tar image_name:tag
- Import an image:
docker load -i image_name.tar
3. Docker Volumes & Storage
- Create a volume:
docker volume create volume_name
- List volumes:
docker volume ls
- Inspect volume details:
docker volume inspect volume_name
- Remove a volume:
docker volume rm volume_name
- Remove unused volumes:
docker volume prune
- Run a container with a volume:
docker run -d --name container_name -v volume_name:/container_path image_name
4. Networking
- List networks:
docker network ls
- Create a network:
docker network create network_name
- Inspect network details:
docker network inspect network_name
- Connect a container to a network:
docker network connect network_name container_name
- Disconnect a container from a network:
docker network disconnect network_name container_name
- Remove a network:
docker network rm network_name
5. Docker Compose
- Start services:
docker-compose up -d
- Stop services:
docker-compose down
- Recreate services:
docker-compose up -d --force-recreate
- Scale a service:
docker-compose up -d --scale service_name=num_instances
- View logs for services:
docker-compose logs
- View real-time logs for services:
docker-compose logs -f
- Execute a command in a service container:
docker-compose exec service_name command
6. Monitoring & Logs
- View logs of a container:
docker logs container_name
- Stream container logs in real-time:
docker logs -f container_name
- Inspect resource usage:
docker stats
- View events:
docker events
- Inspect container details:
docker inspect container_name
7. System Diagnostics
- Show disk usage by Docker:
docker system df
- Clean up unused Docker data:
docker system prune
- Inspect Docker daemon configuration:
docker info
- Configure resource limits:
docker run -d --name container_name --memory="1g" --cpus="0.5" image_name
8. Security & User Management
- Run container as non-root user:
docker run -d --user user_id:user_group image_name
- Limit container access to resources:
docker run -d --name container_name --cap-drop ALL --cap-add NET_ADMIN image_name
- View security profile for a container:
docker inspect --format='{{json .HostConfig.SecurityOpt}}' container_name
9. Swarm and Orchestration
- Initialize a Docker Swarm:
docker swarm init
- Add a worker to the Swarm:
docker swarm join --token SWMTKN-1-xyz
- Deploy a stack:
docker stack deploy -c docker-compose.yml stack_name
- List services in a stack:
docker stack services stack_name
- Scale a service in a stack:
docker service scale service_name=replicas
- Remove a stack:
docker stack rm stack_name