adnenre
#Docker

Advanced Docker CLI: Networking, Debugging, and Container Optimization

Master advanced Docker command line techniques including networking, debugging containers, monitoring resources, and optimizing container workflows

Docker provides powerful command line tools that allow developers and DevOps engineers to monitor, debug, network, and optimize containers efficiently.

While basic Docker commands help you run containers, advanced commands allow you to build scalable production systems, debug issues, and manage container infrastructure.

This guide explores advanced Docker CLI commands that are frequently used in real-world development environments.

#Inspecting Containers and Images

One of the most useful debugging tools in Docker is the inspect command. It provides detailed metadata about containers, images, networks, and volumes.

docker inspect container_id

Example:

docker inspect my-nginx

Output includes:

  • container configuration
  • environment variables
  • mounted volumes
  • network settings
  • resource limits

Filter specific information using --format.

Example: Get container IP address.

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id

#Monitoring Container Resources

Docker allows you to monitor CPU, memory, and network usage.

docker stats

Example output:

    CONTAINER ID   NAME        CPU %   MEM USAGE / LIMIT
    1a2b3c4d       my-nginx    0.12%   35MiB / 1GiB

Monitor specific containers:

docker stats container_id

Stop monitoring with CTRL + C.

#Viewing Container Processes

Check running processes inside a container.

docker top container_id

Example:

docker top my-nginx

This command works similarly to the Linux ps command but inside the container.

#Accessing Container Shell

Sometimes you need to interact with the container directly.

docker exec -it container_id bash

Example:

docker exec -it my-nginx bash

If bash is not available:

docker exec -it container_id sh

Common debugging commands inside container:

ls
ps aux
top
cat /etc/os-release

#Viewing Container Logs

Logs are essential for debugging container behavior.

docker logs container_id

Follow logs in real time:

docker logs -f container_id

Show last 100 lines:

docker logs --tail 100 container_id

Include timestamps:

docker logs -t container_id

#Docker Networking

Docker containers communicate using networks.

List networks:

docker network ls

Example output:

NETWORK ID     NAME      DRIVER
123abc         bridge    bridge
456def         host      host

#Creating Custom Network

Create a new network:

docker network create my-network

Run containers inside that network:

docker run -d --name app1 --network my-network nginx

Run another container:

docker run -d --name app2 --network my-network redis

Containers can communicate using their container names as hostnames.

Example:

app1 connects to redis using hostname "app2"

#Connecting Container to Network

docker network connect my-network container_id

Disconnect container:

docker network disconnect my-network container_id

#Docker Volume Management

Volumes allow containers to store persistent data.

List volumes:

docker volume ls

Inspect volume:

docker volume inspect volume_name

Create volume:

docker volume create app-data

Run container with volume:

docker run -d -v app-data:/app/data nginx

Remove unused volumes:

docker volume prune

#Docker Image History

View the layers of an image.

docker history image_name

Example:

docker history node:20

This helps understand image size and layer structure.

#Saving and Loading Images

Export Docker image to file:

docker save -o nginx-image.tar nginx

Load image from file:

docker load -i nginx-image.tar

This is useful for transferring images between servers.

#Tagging and Pushing Images

Tag an image:

docker tag my-app username/my-app:1.0

Push to registry:

docker push username/my-app:1.0

Login to Docker registry:

docker login

#Docker System Cleanup

Check Docker disk usage:

docker system df

Example output:

TYPE            TOTAL     ACTIVE
Images          10        3
Containers      8         2
Volumes         6

Remove stopped containers:

docker container prune

Remove unused images:

docker image prune

Remove unused networks:

docker network prune

Remove everything unused:

docker system prune

Aggressive cleanup:

docker system prune -a

#Limiting Container Resources

Limit container memory:

docker run -m 512m nginx

Limit CPU usage:

docker run --cpus="1.5" nginx

Example combined:

docker run -m 512m --cpus="1" nginx

#Restart Policies

Restart containers automatically:

docker run -d --restart always nginx

Restart policy options:

  • no
  • always
  • on-failure
  • unless-stopped

Example:

docker run -d --restart unless-stopped nginx

#Docker Best Practices

Use specific versions instead of latest.

Example:

node:20

Avoid:

node:latest

Run containers as non-root user when possible.

Minimize Dockerfile layers.

Use .dockerignore file:

node_modules
.git
.env

#Example Developer Workflow

Typical Docker workflow:

Build image:

docker build -t my-api .

Run container:

docker run -d -p 3000:3000 my-api

Check running containers:

docker ps

Check logs:

docker logs -f my-api

Debug container:

docker exec -it my-api bash

Stop container:

docker stop my-api

Remove container:

docker rm my-api

#Conclusion

Mastering Docker CLI commands allows developers to manage containerized applications efficiently. By understanding commands for containers, images, networking, and debugging, developers can build reliable and scalable systems using Docker.

Share this post