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