Screenshot from the article

Introduction

Docker is an essential tool for modern DevOps, enabling seamless containerization of applications. While basic Docker commands are widely used, mastering advanced Docker commands can significantly enhance efficiency, security, and troubleshooting in containerized environments.

This guide covers advanced Docker commands, real-world use cases, and best practices to help you optimize your workflows.

1. Managing Containers Efficiently

1.1 Running Commands Inside a Running Container

  • Use docker exec to run a command inside a running container without stopping it:
  • docker exec -it <container_id> bash
Use case: Access a running container to debug an issue.

1.2 Committing Changes to a Running Container

  • Save the state of a container as a new image:
  • docker commit <container_id> my-custom-image
  • Use case: Create a reusable image after manual changes inside a container.

1.3 Viewing Changes in a Container’s Filesystem

  • Identify modified files inside a container:
  • docker diff <container_id>
  • Use case: Audit container modifications for security and debugging.

2. Networking in Docker

2.1 Creating a Custom Network

  • Set up an isolated network for inter-container communication:
  • docker network create my_network
  • Use case: Ensure secure and organized container communication.

2.2 Connecting a Running Container to a Network

  • Attach an existing container to a network dynamically:
docker network connect my_network <container_id>
  • Use case: Link a container to a service without restarting it.

2.3 Inspecting Network Configuration

  • View detailed network information:
  • docker network inspect my_network
  • Use case: Debug network connectivity issues.

3. Debugging and Monitoring Containers

3.1 Viewing Container Logs

  • Monitor application logs:
  • docker logs -f <container_id>
  • Use case: Debug application behavior in real-time.

3.2 Inspecting Container Metadata

  • View low-level details about a container:
  • docker inspect <container_id>
  • Use case: Fetch environment variables, mounts, and networking details.

3.3 Monitoring Container Resource Usage

  • Get real-time CPU and memory usage:
  • docker stats
  • Use case: Identify resource-hungry containers.

4. Advanced Volume Management

4.1 Listing Volumes

  • Display all Docker volumes:
  • docker volume ls
  • Use case: Track persistent storage in your containers.

4.2 Removing Unused Volumes

  • Free up storage by removing unused volumes:
  • docker volume prune
  • Use case: Avoid unnecessary disk usage.

5. Security Best Practices in Docker

5.1 Scanning Docker Images for Vulnerabilities

  • Identify security vulnerabilities in images:
  • docker scan my-image
  • Use case: Ensure compliance with security policies.

5.2 Managing Trusted Images

  • Enable content trust for Docker images:
  • export DOCKER_CONTENT_TRUST=1
  • Use case: Ensure images are signed and verified before running.

6. Real-World Use Cases

6.1 Optimizing Docker Builds for Production

  • Use multi-stage builds to reduce image size:
FROM node:16 as build
WORKDIR /app
COPY . .
RUN npm install && npm run build
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
  • Use case: Reduce attack surface and improve deployment speed.

6.2 Running a Multi-Container App with Networking

  • Start a containerized app using Docker Compose:
version: '3.8'
services:
  web:
    image: nginx
    networks:
      - my_network
  db:
    image: postgres
    networks:
      - my_network
networks:
  my_network:
  • Use case: Deploy interconnected services with ease.

FAQs

Q1: How can I find the container consuming the most resources?

Use docker stats to check resource usage per container.

Q2: How can I restart all running containers?

docker restart $(docker ps -q)

Q3: How do I clean up unused Docker resources?

docker system prune -a

Conclusion

Mastering advanced Docker commands enhances efficiency, security, and scalability in your containerized applications. Whether it’s optimizing builds, troubleshooting issues, or securing deployments, these commands are essential for every DevOps engineer.

What’s Next? Start applying these commands in your projects and explore Docker Swarm and Kubernetes for large-scale orchestration.

📢 Have questions or feedback? Drop a comment below or connect with me on Twitter/X@spysood!

Originally published on Medium.