You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -458,18 +525,25 @@ docker run --security-opt no-new-privileges nginx
458
525
459
526
| Command | Description |
460
527
|---------|-------------|
461
-
|`docker run`| Create and start container |
528
+
|`docker run`| Create and start a container |
462
529
|`docker ps`| List running containers |
463
530
|`docker images`| List images |
464
-
|`docker build`| Build image from Dockerfile |
465
-
|`docker pull`| Download image |
466
-
|`docker push`| Upload image |
467
-
|`docker exec`| Execute command in container |
468
-
|`docker logs`| View container logs |
469
-
|`docker stop`| Stop container |
470
-
|`docker rm`| Remove container |
471
-
|`docker rmi`| Remove image |
472
-
531
+
|`docker build`| Build an image from a Dockerfile |
532
+
|`docker pull`| Download an image from a registry |
533
+
|`docker push`| Upload an image to a registry |
534
+
|`docker exec`| Execute a command inside a running container |
535
+
|`docker logs`| View a container's output logs |
536
+
|`docker stop`| Stop a running container |
537
+
|`docker restart`| Restart a container |
538
+
|`docker rm`| Remove a container |
539
+
|`docker rmi`| Remove an image |
540
+
|`docker inspect`| Show detailed info on a Docker object |
541
+
|`docker stats`| Show live resource usage statistics |
542
+
|`docker-compose up`| Start a multi-container application |
543
+
|`docker network ls`| List all Docker networks |
544
+
|`docker volume ls`| List all Docker volumes |
545
+
|`docker system prune`| Clean up unused images, containers, and networks |
546
+
473
547
### Quick Cleanup
474
548
475
549
```bash
@@ -491,6 +565,62 @@ docker volume prune
491
565
492
566
---
493
567
568
+
## Docker Security: Hardening Your Environment
569
+
570
+
Security is a critical aspect of containerization. Docker provides several built-in mechanisms to secure your applications and infrastructure.
571
+
572
+
### 1. Secrets Management
573
+
Secrets allow you to store sensitive data (like passwords, API keys, or certificates) outside of your images or source code.
574
+
```bash
575
+
# Create a secret from a file
576
+
docker secret create db_password ./password.txt
577
+
578
+
# List secrets
579
+
docker secret ls
580
+
581
+
# Inspect a secret
582
+
docker secret inspect db_password
583
+
584
+
# Use a secret in a service
585
+
docker service create --name db --secret db_password mariadb
586
+
```
587
+
588
+
### 2. Docker Content Trust (DCT)
589
+
DCT allows you to use digital signatures for data sent to and received from remote Docker registries. These signatures allow client-side verification of the integrity and publisher of specific image tags.
590
+
```bash
591
+
# Enable Content Trust (shell session)
592
+
export DOCKER_CONTENT_TRUST=1
593
+
594
+
# Pull only signed images
595
+
docker pull nginx:latest
596
+
```
597
+
598
+
### 3. Vulnerability Scanning
599
+
Regularly scan your images for known vulnerabilities to ensure your software supply chain is secure.
600
+
```bash
601
+
# Scan an image for vulnerabilities
602
+
docker scan my-image:latest
603
+
```
604
+
605
+
### 4. User Namespaces & Rootless Docker
606
+
Running Docker in "Rootless Mode" or using User Namespaces adds a layer of security by ensuring that even if a container is compromised, the attacker does not have root access to the host.
607
+
```bash
608
+
# Check if rootless mode is supported
609
+
docker system info | grep "Rootless"
610
+
611
+
# Run a container with a specific user namespace
612
+
docker run --userns-remap=default -it alpine sh
613
+
```
614
+
615
+
### 5. Resource Isolation
616
+
Prevent Denial of Service (DoS) attacks by strictly limiting the resources a container can consume.
617
+
```bash
618
+
# Limit memory, CPU, and pids (process limit)
619
+
docker run -m 512m --cpus="0.5" --pids-limit 100 my-app
620
+
```
621
+
622
+
---
623
+
494
624
## Best Practices
495
625
496
626
### Command Tips
@@ -502,21 +632,15 @@ docker volume prune
502
632
5.**Use health checks** - Monitor container health
503
633
6.**Clean up regularly** - Remove unused objects
504
634
505
-
### Security Tips
635
+
### Security Best Practices
506
636
507
-
```bash
508
-
# Don't run as root
509
-
docker run -u 1000:1000 my-app
637
+
1.**Don't run as root** - Use the `USER` instruction in Dockerfile or `-u` flag.
638
+
2.**Use read-only filesystem** - Prevents attackers from writing to the container disk.
639
+
3.**Scan images regularly** - Use `docker scan` to find vulnerabilities.
640
+
4.**Use Secrets** - Never bake passwords or keys into your images.
641
+
5.**Limit Resources** - Always set memory and CPU limits to prevent host exhaustion.
510
642
511
-
# Use read-only filesystem when possible
512
-
docker run --read-only my-app
513
-
514
-
# Drop unnecessary capabilities
515
-
docker run --cap-drop ALL --cap-add NET_BIND_SERVICE my-app
516
-
517
-
# Use security profiles
518
-
docker run --security-opt apparmor:my-profile my-app
519
-
```
643
+
Refer to the **Docker Security** section above for more detailed commands and implementation details.
0 commit comments