๐ข 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. ๐ฑโ