DEV Community

muneeb aslam
muneeb aslam

Posted on

🚀 Docker Cheat sheet for Beginners

🐳 What is Docker?

Docker is a powerful platform for automating the deployment of applications within lightweight, portable containers, making it easier to manage and scale applications.


🔧 Basic Commands

Command Description
docker --version Check the installed Docker version
docker pull <image> Download an image from Docker Hub
docker images List all downloaded images
docker rmi <image> Remove an image
docker run <image> Create and start a container from an image
docker ps List all running containers
docker ps -a List all containers (running + stopped)
docker stop <container> Stop a running container
docker start <container> Start a stopped container
docker rm <container> Remove a stopped container

📦 Working with Containers

Command Description
docker exec -it <container> /bin/bash Access the terminal of a running container
docker logs <container> View logs from a container
docker commit <container> <new_image> Create a new image from a container's changes
docker network ls List all networks
docker volume ls List all volumes

🛠️ Dockerfile Basics

  • Dockerfile Structure:
# Base image
FROM ubuntu:latest

# Maintainer
LABEL maintainer="your_name@example.com"

# Install dependencies
RUN apt-get update && apt-get install -y python3

# Copy files
COPY . /app

# Set working directory
WORKDIR /app

# Command to run
CMD ["python3", "app.py"]
Enter fullscreen mode Exit fullscreen mode

🌐 Docker Compose

  • docker-compose.yml Example:
version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: example
Enter fullscreen mode Exit fullscreen mode

🔥 Common Commands

Command Description
docker-compose up Start services defined in docker-compose.yml
docker-compose down Stop and remove containers
docker-compose logs View logs for services

📘 Useful Tips

  • Container Management:

    • Use the -d flag with docker run to run containers in detached mode for background execution.
  • Clean Up:

    • Run docker system prune to remove unused data and free up disk space.
  • Help:

    • Use docker <command> --help for detailed command information and options.

🎨 Styling Tips

  • Terminal Color: Use terminal color codes for output to make commands visually appealing:
  echo -e "\033[1;32mYour command here\033[0m"
Enter fullscreen mode Exit fullscreen mode
  • Icons: Enhance your notes with emojis for a more engaging experience.

🔗 Resources


Top comments (0)