πŸ’» Common Useful Docker Commands 🐳

Truong Phung - Oct 16 - - Dev Community

Here are container filesystem structure and some common Docker commands for managing containers, images, networks, and volumes:

Docker Container FileSystem Structure

The filesystem structure inside a Docker container is based on a minimal Linux root filesystem (Read More), designed to be lightweight and specific to the application it runs. Here’s a breakdown of common directories:

/
β”œβ”€β”€ bin/           # Essential binaries, executables, user commands (ls, cp, etc.)
β”œβ”€β”€ dev/           # Device files (terminals, USBs), usually for virtual devices
β”œβ”€β”€ etc/           # System and containerized application configuration files (network, users)
β”œβ”€β”€ home/          # Optional, user-specific files and directories, for non-root users
β”œβ”€β”€ lib/           # Shared libraries needed by bin/ and sbin/
β”œβ”€β”€ media/         # Mount points for removable media (e.g., CD, USB) (often empty)
β”œβ”€β”€ mnt/           # Temporary mount location for external filesystems
β”œβ”€β”€ proc/          # Virtual filesystem with system/process info, representing process and system info
β”œβ”€β”€ root/          # Home directory for the root user
β”œβ”€β”€ run/           # Runtime process information (e.g., PIDs, sockets), application state files
β”œβ”€β”€ sbin/          # System management binaries, commands, usually for root (e.g., reboot)
β”œβ”€β”€ srv/           # Service data provided by the system (e.g., web, FTP)
β”œβ”€β”€ tmp/           # Temporary files, often cleared on reboot, wiped on container stop/restart
β”œβ”€β”€ usr/           # User binaries, libraries, documentation, secondary hierarchy for system resources
β”‚   β”œβ”€β”€ bin/       # User binaries (e.g., git, gcc)
β”‚   β”œβ”€β”€ lib/       # Libraries for user binaries
β”‚   └── local/     # Locally installed software and files
└── var/           # Variable data files that change frequently
    β”œβ”€β”€ cache/     # Application cache data
    β”œβ”€β”€ log/       # System and application logs
    β”œβ”€β”€ mail/      # User mail storage
    └── tmp/       # Temporary storage for running applications
Enter fullscreen mode Exit fullscreen mode

Containers are built to isolate application dependencies, so only essential directories and files for the application are typically present, making it lightweight and minimal

1. Docker Container Commands:

  • Run a container:

     docker run -d --name <container-name> <image-name>
    
    • Runs a container in detached mode (-d) from the specified image.
    • Example: docker run -d --name my-nginx nginx
  • List running containers:

     docker ps
    
    • Shows all currently running containers.
    • Add -a to list all containers, including stopped ones.
  • Stop a container:

     docker stop <container-id|container-name>
    
    • Stops a running container.
  • Remove a container:

     docker rm <container-id|container-name>
    
    • Deletes a stopped container.
    • Use -f to force-remove a running container.

2. Docker Image Commands:

  • Build an image:

     docker build -t <image-name>:<tag> <path-to-dockerfile>
    
    • Builds an image from a Dockerfile.
    • Example: docker build -t my-app:latest .
  • List images:

     docker images
    
    • Lists all images available on the host.
  • Remove an image:

     docker rmi <image-id|image-name>
    
    • Deletes an image.
    • Use -f to force-remove an image if it's being used.
  • Pull an image from a repository:

     docker pull <image-name>:<tag>
    
    • Downloads an image from Docker Hub or other registries.
    • Example: docker pull ubuntu:latest

3. Docker Network Commands:

  • List networks:

     docker network ls
    
    • Shows all Docker networks.
  • Create a network:

     docker network create <network-name>
    
    • Creates a new Docker network.
  • Connect a container to a network:

     docker network connect <network-name> <container-name>
    
  • Inspect a network:

     docker network inspect <network-name>
    
    • Shows detailed information about a Docker network.

4. Docker Volume Commands:

  • Create a volume:

     docker volume create <volume-name>
    
    • Creates a new volume for persistent storage.
  • List volumes:

     docker volume ls
    
    • Lists all Docker volumes.
  • Remove a volume:

     docker volume rm <volume-name>
    
    • Deletes a volume.
  • Inspect a volume:

     docker volume inspect <volume-name>
    
    • Shows detailed information about a volume.

5. Miscellaneous Commands:

  • View container logs:

     docker logs <container-id|container-name>
    
    • Displays logs from a specific container.
    • Use -f to follow the logs in real-time.
  • Access a running container's shell:

     docker exec -it <container-id|container-name> /bin/bash
    
    • Starts an interactive shell inside a running container.
    • Example: docker exec -it my-nginx /bin/bash
  • Tag an image:

     docker tag <image-name> <repository>/<image-name>:<tag>
    
    • Tags an image to prepare it for pushing to a registry.
    • Example: docker tag my-app myrepo/my-app:latest
  • Push an image to a registry:

     docker push <repository>/<image-name>:<tag>
    
    • Uploads an image to a Docker registry.
  • Prune unused containers, images, networks, and volumes:

     docker system prune
    
    • Cleans up unused resources.
    • Add -a to remove all stopped containers and unused images.

These commands cover most of the typical operations when working with Docker, such as managing containers, images, and networking.

If you found this helpful, let me know by leaving a πŸ‘ or a comment!, or if you think this post could help someone, feel free to share it! Thank you very much! πŸ˜ƒ

. . . . . . . . . . . . . . . . . . . . . . . . . . .