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> bash
4. 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 ubuntu
This will:
- Use the
ubuntu
image - 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 ubuntu
This 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