Dock 1
Dock 1
1.DOCKER
Docker is an open-source platform that automates the deployment, scaling, and management of applications
using containerization. Containers are lightweight, standalone packages that include everything needed to
run an application: code, runtime, libraries, and system tools. They ensure consistency across different
environments
3. DOCKER VOLUME
Docker volumes are persistent storage areas managed by Docker, used to store data outside the container's
writable layer. They help persist data across container restarts or removal.
Named Volumes:
o Created and managed by Docker.
o Stored at /var/lib/docker/volumes/ on Linux hosts.
o Use: docker volume create my_volume
Anonymous Volumes:
o Created when you use -v /path without naming a volume.
o Docker manages these but they are harder to track.
Bind Mounts:
o Directly mount a host directory or file inside a container.
o Paths can be anywhere on the host.
o Example: -v /host/path:/container/path
Related Commands:
Definition:
Docker networking allows containers to communicate with each other, the host, and external networks.
Related Commands:
5. MULTISTAGE BUILD
Definition:
A Dockerfile technique to reduce image size by using multiple FROM statements, copying only necessary
artifacts from earlier stages to the final image.
Dockerfile
CopyEdit
# Stage 1: Build stage
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
Explanation:
What happens?
Stage 1 (builder):
Full Go environment compiles the app. This stage contains all build tools and source code.
Stage 2 (runtime):
Only the compiled binary from the builder stage is copied into a minimal Alpine image for runtime.
6. DOCKER COMPOSE
Definition:
Docker Compose is a tool to define and run multi-container Docker applications using a YAML file
(docker-compose.yml).
Use Cases:
Components included:
Example:
yaml
CopyEdit
version: "3"
services:
web:
image: nginx
ports:
- "80:80"
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
Commands:
Command Description
docker run Run a container
docker ps List running containers
docker ps -a List all containers (including stopped)
docker stop <container> Stop a running container
docker rm <container> Remove a container
docker rmi <image> Remove an image
docker images List images
docker build -t <tag> . Build an image from Dockerfile
docker pull <image> Pull an image from a registry
docker push <image> Push an image to a registry
docker logs <container> Show logs of a container
docker exec -it <container> /bin/bash Open a shell inside a running container
docker volume ls List volumes
docker network ls List networks
docker-compose up Start multi-container app (docker-compose)
docker-compose down Stop and remove multi-container app
dockerfile
CopyEdit
RUN useradd -m appuser
USER appuser
Or at runtime:
export DOCKER_CONTENT_TRUST=1
docker pull <image>
ls -l /var/run/docker.sock
sudo chmod 660 /var/run/docker.sock
sudo chown root:docker /var/run/docker.sock
# Add only trusted users to docker group
sudo usermod -aG docker <username>
dockerfile
CopyEdit
# Use official minimal base image
FROM python:3.11-slim
# Expose port
EXPOSE 5000
yaml
CopyEdit
version: "3.8"
services:
web:
build: .
ports:
- "5000:5000"
environment:
- ENV=production
volumes:
- web-data:/app/data # Persist writable data
user: "1000:1000" # Run as non-root user
read_only: true # Read-only root filesystem
cap_drop:
- ALL # Drop all Linux capabilities
volumes:
web-data:
1. Volume Issues
Error:
Permission denied when container tries to write to a mounted volume.
Reason:
Host directory permissions don’t allow container user to write.
Resolution:
Adjust permissions on the host folder:
Error:
Data not persisting after container removal.
Reason:
Using anonymous volumes or no volume specified, so data is lost with container.
Resolution:
Use named volumes or bind mounts in docker run or docker-compose.yml:
2. Network Issues
Error:
Container cannot reach another container by service name.
Reason:
Containers not on the same user-defined network.
Resolution:
Create and attach containers to the same network:
Error:
Port already in use on host.
Reason:
Host port is busy with another process.
Resolution:
Check process using port and change port mapping:
3. Image Issues
Error:
manifest for image:tag not found
Reason:
Image or tag does not exist in Docker Hub or registry.
Resolution:
Verify image name and tag; pull correct image or build locally.
Error:
Build fails with COPY failed: file not found
Reason:
File/directory missing in build context.
Resolution:
Ensure file exists relative to Dockerfile and build context root.
4. Container Issues
Error:
Container exits immediately after start.
Reason:
Main process (CMD/ENTRYPOINT) finishes or crashes immediately.
Resolution:
Check container logs for errors:
Error:
no such file or directory on ENTRYPOINT or CMD.
Reason:
Script or executable missing or wrong path.
Resolution:
Verify executable exists inside image and paths are correct.
Error:
docker: Error response from daemon: Conflict. The container name "/name"
is already in use.
Reason:
Container with same name exists.
Resolution:
Remove or rename existing container:
docker rm <container>
1. Volume Issues
List volumes:
docker volume ls
Inspect volume:
Error: Volume data is not updating inside container after host file changes
Reason: Volume mount uses a cached or read-only mode, or container app caches data internally.
Resolution:
Error: Docker volume is mounted but files inside container are empty or missing
Reason: Wrong volume path or volume not mounted correctly.
Resolution:
2. Network Issues
Check networks:
docker network ls
docker network inspect <network_name>
Error: DNS resolution fails inside container (e.g., ping google.com fails)
Reason: Docker DNS settings misconfigured or host DNS issues.
Resolution:
docker ps
3. Image Issues
Image not found
ls -l ./relative_path_to_file
Error: Build cache causes outdated layers to be used, changes not reflected**
Reason: Docker reuses cached image layers to speed up build.
Resolution:
Error: Build fails with failed to fetch anonymous token or authentication error pulling image
Reason: Docker Hub rate limits or authentication failure.
Resolution:
docker login
4. Container Issues
Check logs:
docker stats
df -h
Error: Container logs show cannot connect to database or service dependency failure
Reason: Container startup order or network issue between containers.
Resolution:
bash
CopyEdit
docker network inspect <network>
docker exec -it <container> ping <other_container>
docker ps -a
docker logs <container>
bash
CopyEdit
sudo systemctl restart docker
bash
CopyEdit
sudo journalctl -u docker
6.Security Issues
Error/Issue
Reason
Step-by-step Debug/Resolution
Commands you can run
1. Volume Issues
Error: Files inside the container are missing or do not update after host file changes.
Reason: Volume path mismatch, read-only mount, or app caching.
Fix:
ls -l /host/path
Error: Container cannot write to volume mounted from host, shows permission denied.
Reason: Host directory permissions don’t allow container user to write.
Fix:
ls -ld /host/path
3. Rerun container.
2. Network Issues
docker network ls
docker ps
Error: Containers cannot reach network or each other after Docker restart.
Reason: Default bridge network resets; user-defined networks persist but containers disconnected.
Fix:
3. Image Issues
ls -l ./path/to/file
Multistage example:
dockerfile
CopyEdit
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o app
FROM alpine
WORKDIR /app
COPY --from=builder /app/app .
CMD ["./app"]
docker login
1. Check logs:
docker ps -a
docker logs <container>
docker stats
2. Set limits:
df -h
1. Restart Docker:
2. Check logs:
6. Security Issues
1. Volume Issues
ls -l /host/path
ls -ld /host/path
o Change ownership/permissions:
sudo chown -R $(id -u):$(id -g) /host/path
o Rerun container.
2. Network Issues
docker network ls
docker ps
3. Image Issues
ls -l ./file
dockerfile
CopyEdit
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o app
FROM alpine
WORKDIR /app
COPY --from=builder /app/app .
CMD ["./app"]
docker login
4. Container Issues
o Run interactively:
docker stats
o Set limits:
df -h
o Cleanup:
o Check logs:
o Reinstall/update Docker.
6. Security Issues
bash
CopyEdit
docker ps -a
docker logs <container>
docker inspect <container>
docker stats
docker volume ls
docker volume inspect <volume>
docker volume rm <volume>
docker network ls
docker network inspect <network>
docker system prune -a --volumes
sudo systemctl restart docker
sudo journalctl -u docker
docker exec -it <container> /bin/sh
docker run -it --entrypoint /bin/sh myimage
docker build --no-cache -t myimage .
docker run --memory=512m --cpus=1 myimage
#!/bin/bash