Docker Basics
Key Concepts
- Container: Isolated, portable environments for running applications.
- Image: The blueprint for creating containers.
- Volumes: Persistent storage for container data.
- Networks: Connections between containers.
Start with Simple Commands
- Run a container:
docker run hello-world - List running containers:
docker ps - List all containers (including stopped ones):
docker ps -a - Stop a container:
docker stop <container_id> - Remove a container:
docker rm <container_id> - Remove all stopped containers:
docker container prune
Building and Running Containers
Build an Image
- Build from a
Dockerfile:docker build -t <image_name> .
Run a Container
- Run an image as a container:
docker run -d -p 8080:80 <image_name>-d: Run in detached mode (background).-p: Map container ports to host ports.
- Run with environment variables:
docker run -e ENV_VAR_NAME=value <image_name>
Interacting with Containers
Execute Commands Inside a Running Container
- Open a shell in a container:
docker exec -it <container_id> /bin/bash - Run a single command inside a container:
docker exec <container_id> <command>Example:docker exec <container_id> ls /app
Copy Files
- Copy a file from host to container:
docker cp <local_path> <container_id>:<container_path> - Copy a file from container to host
docker cp <container_id>:<container_path> <local_path>
Attach to a Container
- Attach to the standard input/output of a running container:
docker attach <container_id>(PressCtrl+P+Ctrl+Qto detach without stopping the container.)
Viewing Logs and Debugging
Logs
- View logs of a container:
docker logs <container_id> - Follow logs in real-time:
docker logs -f <container_id>
Stats
- Monitor resource usage of running containers:
docker stats
Inspect
- Inspect a container or image:
docker inspect <container_name_or_image> - Get specific details (e.g., IP address):
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name>
Managing Images
Image Commands
- List all images:
docker images - Remove an image:
docker rmi <image_id> - Remove dangling images:
docker image prune
Tagging and Pushing
- Tag an image for a registry:
docker tag <image_id> <repository>:<tag> - Push an image to Docker Hub:
docker push <repository>:<tag> - Pull an image:
docker pull <repository>:<tag>
Docker Volumes
Persistent Data
- Create a volume:
docker volume create <volume_name> - Mount a volume when running a container:
docker run -v <volume_name>:/path/in/container <image_name> - List all volumes:
docker volume ls - Remove unused volumes:
docker volume prune
Docker Networks
Network Commands
- List all networks:
docker network ls - Create a network:
docker network create <network_name> - Run a container in a specific network:
docker run --network <network_name> <image_name> - Connect a running container to a network:
docker network connect <network_name> <container_name>
Cleaning Up
Remove Unused Resources
- Remove stopped containers:
docker container prune - Remove unused images:
docker image prune - Remove unused volumes:
docker volume prune - Remove everything unused:
docker system prune --volumes
Docker Compose
Common Commands
- Start services defined in
docker-compose.yml:docker-compose up - Start services in detached mode:
docker-compose up -d - Stop running services:
docker-compose down - Restart services:
docker-compose restart
Example docker-compose.yml
Here’s an example for the Video Transcriber project:
version: '3.8'
services:
frontend:
build:
context: ./frontend
ports:
- "3000:3000"
backend:
build:
context: ./backend
ports:
- "8000:8000"
Advanced Docker Commands
Multi Stage Builds
Optimize image size with multi stage builds:
FROM python:3.9-slim as builder
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
FROM python:3.9-slim
WORKDIR /app
COPY --from=builder /app /app
CMD ["python", "app.py"]
Interactive Containers
- Run a container interactively for debugging:
docker run -it <image_name> /bin/bash
0 Comments