Docker Worksheet for Everyone

🟒 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. πŸŒ±β€

Leave a Comment