π’ 1. Basic Docker Commands
To check Docker version:
πΉ docker –version
Example: Docker version 24.0.5
To check system information:
πΉ docker info
Displays environment details.
π’ 2. Working with Containers
Run a container:
πΉ docker run -d –name my-nginx -p 8080:80 nginx
Runs an NGINX container in detached mode on port 8080 with name mynginx.
List running containers:
πΉ docker ps
Shows all active containers.
List all containers (including stopped ones):
πΉ docker ps -a
Stop a running container:
πΉ docker stop <container-id>
Restart a container:
πΉ docker restart <container-id>
Remove a container:
πΉ docker rm <container-id>
View container logs:
πΉ docker logs <container-id>
Execute a command in a running container:
πΉ docker exec -it <container-id> /bin/sh
Opens an interactive shell inside the container.
π’ 3. Working with Images
List all images:
πΉ docker images
Pull an image from Docker Hub:
πΉ docker pull <image-name>
Build an image from a Dockerfile:
πΉ docker build -t my-app .
Remove an image:
πΉ docker rmi <image-id>
Tag an image:
πΉ docker tag <image-id> myrepo/my-app:v1
Inspect an image:
πΉ docker inspect <image-id>
π’ 4. Docker Networking
List all networks:
πΉ docker network ls
Create a new network:
πΉ docker network create my-network
Connect a container to a network:
πΉ docker network connect my-network my-container
Disconnect a container from a network:
πΉ docker network disconnect my-network my-container
Inspect a network:
πΉ docker network inspect my-network
Remove a network:
πΉ docker network rm my-network
π’ 5. Docker Volumes (Persistent Storage)
Create a new Volume:
πΉ docker volume create my-volume
List all Volumes:
πΉ docker volume ls
Inspect a Volume:
πΉ docker volume inspect my-volume
Remove a Volume:
πΉ docker volume rm my-volume
Mount a Volume to a container:
πΉ docker run -d -v my-volume:/data my-app
Mounts “my-volume” to the “/data” directory inside the container.
π’ 6. Advanced Docker Usage
Run a Container with Environment Variables:
πΉ docker run -e APP_ENV=production my-app
Limit CPU and Memory Usage of a Container:
πΉ docker run –cpus=2 -m 512m nginx
Limits the container to 2 CPUs and 512MB RAM.
Export a Running Container to a Tar File:
πΉ docker export <container-id> > my-container.tar
Import a Tar File as an Image:
πΉ docker import my-container.tar my-new-image
Clean Up Unused Docker Objects:
πΉ docker system prune -a
Removes all stopped containers, networks, and dangling images.
π’ 7. Docker Compose (Multi-Container Applications)
Start Services Using docker-compose.yml:
πΉ docker-compose up -d
Stop Services:
πΉ docker-compose down
Rebuild and Restart Services:
πΉ docker-compose up –build
Check Logs for Services:
πΉ docker-compose logs -f
π’ 8. Docker Registry (Pushing and Pulling Images)
Login to Docker Hub:
πΉ docker login
Tag an Image for Pushing:
πΉ docker tag my-app:latest myrepo/my-app:v1
Push an Image to Docker Hub:
πΉ docker push myrepo/my-app:v1
Pull an Image from Docker Hub:
πΉ docker pull myrepo/my-app:v1
π’ 9. Docker in Production Use Cases
Deploy a Highly Available Dockerized App with Load Balancing:
πΉ docker run -d –name my-app -p 8080:80 –restart=always my-app
Ensures the container restarts automatically if it crashes.
Monitor Running Containers in Production:
πΉ docker stats
Displays real-time resource usage of running containers.
βDocker is all about hands-on practice. Build, break, fix, repeat β and watch your skills grow every day. π±β