Table of contents
Docker Cheatsheet
1. Docker Basics:
Docker Image: A lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and dependencies.
Docker Container: An instance of a Docker image that can be executed and run as an isolated process.
2. Docker Commands:
docker run: Create and start a new container based on an image.
docker build: Build a Docker image from a Dockerfile.
docker pull: Download a Docker image from a registry.
docker push: Upload a Docker image to a registry.
docker stop: Stop a running container.
docker start: Start a stopped container.
docker restart: Restart a running container.
docker exec: Run a command inside a running container.
3. Dockerfile:
A text file that contains instructions for building a Docker image.
Uses a simple syntax to specify the base image, dependencies, environment variables, and commands to run.
Example Dockerfile:
FROM ubuntu:latest RUN apt-get update && apt-get install -y python3 CMD ["python3", "app.py"]
4. Docker Registry:
A centralized location where Docker images are stored and can be shared.
Popular registries include Docker Hub, Amazon ECR, and Google Container Registry.
Allows you to upload and download Docker images.
5. Docker Compose:
A tool for defining and running multi-container Docker applications.
Uses a YAML file to configure the services, networks, and volumes for the application.
Simplifies the management of complex Docker setups.
Example docker-compose.yml:
version: '3' services: web: build: . ports: - "5000:5000" volumes: - .:/code
6. Docker Networking:
Docker provides networking capabilities to enable communication between containers and other networked entities.
Supports various network drivers such as bridge, overlay, host, and macvlan.
Containers can be connected to multiple networks for different use cases.
7. Docker Volumes:
A mechanism for persisting data generated by and used by Docker containers.
Allows data to be shared between containers and persisted beyond the lifecycle of a container.
Can be managed using Docker commands or Docker Compose.
8. Docker Security:
Docker provides several security features to protect containers and the host system.
Includes capabilities such as user namespaces, SELinux/AppArmor, container isolation, and image signing.
Regularly update Docker and base images to patch security vulnerabilities.
9. Docker Best Practices:
Keep Docker images lightweight by minimizing the number of layers and removing unnecessary dependencies.
Use multi-stage builds to optimize Dockerfile size and build times.
Avoid running containers as root whenever possible for improved security.
Monitor container resource usage and scale as needed to ensure performance and reliability.
Remember, Docker is a powerful tool for containerization, but it's essential to understand its concepts and best practices to use it effectively in your development and deployment workflows.
Unlock Your DevOps Potential Today! Explore our curated collection of tools, guides, and resources on our BuyMeACoffee page. Elevate your skills and streamline your workflows with just a sip of support. Check it out now!