1. What is Docker? β
Docker is a platform to develop, ship, and run applications inside lightweight containers. It ensures consistency across different environments.
2. Key Concepts β
- Image β A blueprint for containers (e.g.,
ubuntu,nginx). - Container β A running instance of an image.
- Dockerfile β A script to automate image creation.
- Registry β A storage for images (e.g., Docker Hub).
- Volume β Persistent storage for containers.
- Network β Communication between containers.
3. Essential Commands β
πΉ Check Docker version:
docker --versionπΉ List running containers:
docker psπΉ List all containers (including stopped):
docker ps -aπΉ Stop a container:
docker stop <container_id>πΉ Start a container:
docker start <container_id>πΉ Restart a container:
docker restart <container_id>πΉ Remove a container:
docker rm <container_id>πΉ Pull an image:
docker pull <image_name>πΉ Run a container:
docker run -d --name <name> -p <host_port>:<container_port> <image>πΉ Remove an image:
docker rmi <image_id>πΉ Check logs:
docker logs <container_id>πΉ Exec into a running container:
docker exec -it <container_id> bash4. Docker Compose β
- Used to manage multi-container applications using
docker-compose.yml. - Start services:bash
docker-compose up -d - Stop services:bash
docker-compose down
5. Dockerfile Example β
FROM python:3.9
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]6. Common Issues & Fixes β
β "Cannot connect to Docker daemon" β Start Docker:
sudo systemctl start dockerβ "Port already in use" β Use a different port or stop conflicting service. β
π§± 1. Image = Blueprint β
A Docker image is like a blueprint or recipe for creating containers.
π How to Create (or Pull) an Image: β
docker pull ubuntuβ¬οΈ This downloads the Ubuntu image from Docker Hub.
You can also create your own image using a Dockerfile (weβll come to that later).
π¦ 2. Container = Running App β
A container is like a running machine built from the image.
π How to Create and Run a Container: β
docker run -it ubuntuThis will:
- Use the
ubuntuimage - Start an interactive shell (
-it) - Put you inside the Ubuntu container
Now youβre inside the containerβs terminal! Type exit to come back out.
π Run a container in background: β
docker run -d ubuntu sleep 9999π List containers: β
docker ps -aπΈ 3. Create Your Own Image β
Use a Dockerfile to define an image:
π§ Example: Dockerfile β
# Use Ubuntu as base
FROM ubuntu
# Install curl
RUN apt update && apt install -y curl
# Default command
CMD ["bash"]π Build your image: β
docker build -t myubuntu .Now check your image:
docker imagesπΎ 4. Volume = Data Storage β
Volumes let you store data outside the container so it wonβt be lost when the container stops.
π Create a volume: β
docker volume create myvolumeπ Use volume in a container: β
docker run -it -v myvolume:/data ubuntuThis mounts the volume myvolume to /data inside the container.
Now any file saved in /data will persist even after the container is deleted.
Useful Commands β
docker ps -a # List all containers
docker images # List all images
docker volume ls # List all volumes
docker rm <container> # Remove container
docker rmi <image> # Remove image
docker volume rm <vol> # Remove volume