Dockers
Dockers
By: Lokeshkumar
1. What is Docker?
Docker is an open-source containerization platform that enables
developers to automate the deployment, scaling, and management of
applications.
It packages applications along with their dependencies into containers,
ensuring consistency across multiple environments.
6. What is a Dockerfile?
distinguish versions.
2. Use Container Names: Assign unique names using --name.
3. Isolate Services Using Networks: Create separate Docker networks.
4. Define Services in Compose: Define different services for each
version.
5. Testing & Rollback Strategy: Maintain a separate testing
environment before rollout.
users.
4. Use Docker Secrets & Environment Variables: Secure sensitive
information.
5. Network Segmentation: Restrict communication using Kubernetes
Network Policies.
6. Enable Logging & Auditing: Use centralized logging with the ELK
stack or Fluentd.
7. Regularly Update Dependencies: Keep images updated with security
patches.
ADVANCE LEVEL
1. What is Docker Content Trust (DCT) and why is it important?
Docker Content Trust (DCT) ensures the authenticity and integrity of
Docker images by allowing only signed images to be pulled and run. It uses
cryptographic signatures to prevent unauthorized modifications and
supply chain attacks.
2. What is the difference between Docker Swarm and Kubernetes?
By: Lokeshkumar
registry.
• docker rmi myimage - Remove an image.
3. Network Management
• docker network ls - List available networks.
• docker network create mynetwork - Create a new network.
• docker network connect mynetwork mycontainer - Connect a
container to a network.
4. Volume Management
• docker volume ls - List available volumes.
• docker volume create myvolume - Create a new volume.
• docker volume inspect myvolume - Inspect a volume.
• docker volume rm myvolume - Remove a volume.
5. Docker Compose
• docker-compose up -d - Start services defined in docker-
compose.yml.
• docker-compose down - Stop and remove containers defined in a
Compose file.
• docker-compose logs myservice - View logs of a specific service.
• docker-compose build - Build images defined in a Compose file.
6. Advanced Commands
• docker inspect mycontainer - Display detailed information about a
container.
• docker cp myfile.txt mycontainer:/path/to/destination - Copy files
to/from a container.
• docker stats - Show real-time resource usage of running containers.
• docker prune -a - Remove all unused containers, images, networks,
and volumes.
EXPOSE 3000
3. How do you optimize a Dockerfile to reduce image size?
• Use multi-stage builds to keep only necessary artifacts.
• Use Alpine Linux (node:16-alpine) as a lightweight base image.
• Minimize layers by combining commands (RUN apt-get update &&
apt-get install -y curl).
• Clean up unnecessary files after package installation (rm -rf
/var/lib/apt/lists/*).
4. How can you pass environment variables into a Dockerfile?
You can use the ENV instruction or pass them at runtime:
ENV APP_ENV=production
At runtime:
docker run -e APP_ENV=staging myapp
5. How do you handle secret management in a Dockerfile?
• Use Docker Secrets (Docker Swarm/Kubernetes)
• Use environment variables (-e SECRET_KEY=value)
• Mount external secret files (-v /secrets:/app/secrets)
6. How would you handle dependencies in a Python-based Dockerfile?
Example Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
7. What is the difference between COPY and ADD in a Dockerfile?
• COPY is used to copy files from the host to the container.
• ADD can also extract tar files and download remote URLs.
8. How do you cache dependencies efficiently in Docker?
Place COPY package.json . before copying the entire application to
leverage layer caching:
COPY package.json .
RUN npm install
COPY . .
9. How can you execute a script when a container starts?
By: Lokeshkumar