Docker is not just a tool for beginners; it's a powerful platform for seasoned developers and DevOps engineers to streamline workflows. In this blog, we dive into advanced Docker commands and professional tips to enhance your productivity, troubleshoot effectively, and manage complex containerized environments.

I have posted some regularly used command lists together in another blog you can check out on my channel. Thankyou and Lets begin

1. View Container Resource Limits

Command:

docker inspect --format='{{.HostConfig.Memory}} {{.HostConfig.CpuShares}}' <container_id>

Description:

Extracts specific resource limits (memory and CPU shares) for a container. Useful for ensuring proper resource allocation.

Example:

docker inspect --format='{{.HostConfig.Memory}} {{.HostConfig.CpuShares}}' abc123

2. Limit CPU and Memory Usage for a Container

Command:

docker run --cpus="<value>" --memory="<value>" <image_name>

Description:

Restricts the CPU and memory resources a container can use, ensuring it doesn't overwhelm the host system.

Example:

docker run --cpus="1.5" --memory="512m" nginx

3. Running Containers in Read-Only Mode

Command:

docker run --read-only <image_name>

Description:

Starts a container with a read-only file system, enhancing security by preventing file system modifications.

Example:

docker run --read-only nginx

4. Multi-Stage Dockerfile Build

Command:

Use a multi-stage Dockerfile.

Description:

Optimizes image size by separating the build process into multiple stages. Use this for production-ready builds.

Example Dockerfile:

# Stage 1: Build
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o main .
# Stage 2: Runtime
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/main .
CMD ["./main"]

Build and Run:

docker build -t my-app:latest .
docker run my-app:latest

5. Monitor Real-Time Logs from Multiple Containers

Command:

docker-compose logs -f

Description:

Streams logs from all services defined in a docker-compose.yml file in real-time.

Example:

docker-compose logs -f

6. Troubleshooting with Debug Mode

Command:

docker run --entrypoint /bin/sh -it <image_name>

Description:

Overrides the default entrypoint to start a shell session, allowing for container debugging.

Example:

docker run --entrypoint /bin/sh -it nginx

7. Mounting Files for Local Development

Command:

docker run -v $(pwd):/app <image_name>

Description:

Mounts a local directory into the container for seamless development without rebuilding images.

Example:

docker run -v $(pwd):/app my-node-app

8. Docker Networks for Microservices

Command:

docker network create <network_name>

Description:

Creates a custom bridge network to enable communication between multiple containers. Ideal for microservice architectures.

Example:

docker network create my-network
docker run --network=my-network --name service1 nginx
docker run --network=my-network --name service2 alpine

9. Copy Files to and from a Container

Command:

docker cp <container_id>:<path_in_container> <local_path>
docker cp <local_path> <container_id>:<path_in_container>

Description:

Transfers files between your local machine and a container. Essential for quick debugging or backups.

Example:

docker cp abc123:/app/config.yml ./config.yml
docker cp ./new-config.yml abc123:/app/config.yml

10. Export and Import Containers

Command:

Export:

docker export <container_id> > container_backup.tar

Import:

cat container_backup.tar | docker import - <new_image_name>

Description:

Exports a container's filesystem as a tar archive and re-imports it as a new image. Useful for creating snapshots.

Example:

docker export abc123 > mycontainer.tar
cat mycontainer.tar | docker import - mynewimage

11. Docker Compose: Scale Services

Command:

docker-compose up --scale <service_name>=<count>

Description:

Scales a specific service to the desired number of replicas for load balancing and testing.

Example:

docker-compose up --scale web=3

12. Clean Dangling Images and Containers Automatically

Command:

docker system prune --volumes -f

Description:

Removes unused containers, images, and volumes in one go to free up disk space.

Example:

docker system prune --volumes -f

13. Running Containers as Non-Root User

Command:

Set USER in the Dockerfile.

Example Dockerfile:

FROM node:16
RUN addgroup appgroup && adduser -S appuser -G appgroup
USER appuser
WORKDIR /app
COPY . .
CMD ["node", "app.js"]

Description:

Improves security by ensuring the container doesn't run as root.

14. Inspect Running Processes in a Container

Command:

docker top <container_id>

Description:

Lists all running processes inside a container for debugging performance issues.

Example:

docker top abc123

15. Enable Auto-Restart for Containers

Command:

docker run --restart always <image_name>

Description:

Configures a container to automatically restart if it crashes or if the Docker daemon restarts.

Example:

docker run --restart always nginx

16. Export Docker Images to Remote Systems

Command:

docker save <image_name> | ssh user@remote 'docker load'

Description:

Transfers Docker images to a remote host without needing a registry.

Example:

docker save my-app:1.0 | ssh user@remote 'docker load'

17. Persistent Volumes for Data Storage

Command:

docker volume create <volume_name>
docker run -v <volume_name>:<path_in_container> <image_name>

Description:

Stores persistent data outside the container lifecycle.

Example:

docker volume create app-data
docker run -v app-data:/data my-app

18. Watch Container Events

Command:

docker events

Description:

Streams real-time events from the Docker daemon for monitoring activities.

Example:

docker events

Conclusion

Docker is a Swiss Army knife for containerized environments. By mastering these advanced commands and implementing these professional tips, you can optimize performance, improve security, and streamline workflows in production-grade applications.

What are your go-to Docker tricks? Share them in the comments below!

Happy Dockering! 🚀