Docker Top 50ย Must-Know Commands ๐Ÿšข๐Ÿณ

Devanshu Agarwal

Written by Devanshu Agarwal /

In todayโ€™s world, Docker is the go-to tool for containerizing applications. Whether youโ€™re a developer, sysadmin, or someone curious about how modern applications scale and deploy, understanding Docker commands can make a big difference. Weโ€™ve gathered the top 50 Docker commands you need to know, broken down into easy-to-understand chunks, so you can navigate Docker like a pro. ๐Ÿ’ช

Why Should You Care About Docker Commands? ๐Ÿค”

Docker helps simplify application development by packaging software into containersโ€”a lightweight alternative to virtual machines. With Docker, you can manage environments efficiently, ship faster, and make sure "it works on my machine" is never an excuse. Mastering Docker commands gives you control over your environment and makes you that person others turn to when they need an app running smoothly. ๐Ÿš€

Hereโ€™s a rundown of the 50 most essential Docker commands and what they do. Bookmark this guide as your go-to Docker reference. ๐Ÿ“Œ

1. Docker Basics: The Essentials ๐Ÿ“

Before diving deep, let's go through the foundational commands every Docker user needs:

  1. docker --version
    ๐Ÿ“Š Displays the current version of Docker installed. Useful for verifying compatibility.

  2. docker pull [image]
    โฌ‡๏ธ Fetches a Docker image from Docker Hub. For example, docker pull nginx gets the latest NGINX container.

  3. docker run [image]
    ๐Ÿƒโ€โ™‚๏ธ Creates and runs a container based on the specified image.

  4. docker ps
    ๐Ÿ“‹ Shows all currently running containers.

  5. docker ps -a
    ๐Ÿ“‹ Displays all containers, even those that are stoppedโ€”ideal for tracking down misbehaving services.

  6. docker stop [container_id]
    โœ‹ Stops a running container. Handy when you need to halt a misbehaving app.

  7. docker rm [container_id]
    ๐Ÿ—‘๏ธ Removes a stopped container to free up system resources.

  8. docker rmi [image_id]
    ๐Ÿ—‘๏ธ Deletes an image from your local machine. Useful for housekeeping when youโ€™ve accumulated too many unused images.

  9. docker images
    ๐Ÿ–ผ๏ธ Lists all Docker images available on your machine.

  10. docker exec -it [container_id] /bin/bash
    ๐Ÿ”ง Opens an interactive terminal in a running container. Great for troubleshooting.

2. Managing Images and Containers ๐Ÿ“ฆ

  1. docker build -t [name] .
    ๐Ÿ—๏ธ Builds an image from a Dockerfile located in the current directory.

  2. docker logs [container_id]
    ๐Ÿ“ Displays the logs of a running or stopped container. Essential for debugging.

  3. docker start [container_id]
    โ–ถ๏ธ Starts a stopped container without recreating it.

  4. docker restart [container_id]
    ๐Ÿ”„ Stops and then starts a container again. Useful for applying quick fixes.

  5. docker inspect [container_id]
    ๐Ÿ” Provides detailed information about a container, including its configuration and network settings.

  6. docker commit [container_id] [image_name]
    ๐Ÿ“ธ Creates a new image from an existing container. Think of it as saving the current state.

  7. docker rename [old_name] [new_name]
    โœ๏ธ Renames a container. Useful if your naming conventions change.

  8. docker top [container_id]
    ๐Ÿ“Š Shows running processes inside a container. Itโ€™s the container equivalent of the Unix top command.

  9. docker diff [container_id]
    ๐Ÿ“‹ Lists the changes made to a container's file system.

  10. docker cp [container_id]:[path] [host_path]
    ๐Ÿ“ Copies files from a container to the host or vice versa.

3. Networking and Volumes ๐ŸŒ๐Ÿ’พ

  1. docker network ls
    ๐ŸŒ Lists all Docker networks. Networks manage how containers communicate.

  2. docker network create [network_name]
    ๐Ÿ› ๏ธ Creates a new Docker networkโ€”useful for separating app environments.

  3. docker network connect [network] [container_id]
    ๐Ÿ”— Connects a container to a specific network.

  4. docker volume create [volume_name]
    ๐Ÿ’พ Creates a volume for persistent storage, which is crucial for databases.

  5. docker volume ls
    ๐Ÿ“‹ Lists all volumes on your system.

  6. docker volume rm [volume_name]
    ๐Ÿ—‘๏ธ Deletes a volume, freeing up disk space.

  7. docker network disconnect [network] [container_id]
    ๐Ÿ”Œ Disconnects a container from a network.

  8. docker port [container_id]
    ๐Ÿ“ Shows the public-facing port of a container and the port it is mapped to internally.

4. Docker Compose Commands ๐Ÿ› ๏ธ

  1. docker-compose up
    ๐Ÿš€ Starts services defined in a docker-compose.yml file. Think of it as a fast-track to launch multi-container apps.

  2. docker-compose down
    โฌ‡๏ธ Stops and removes services, networks, and volumes defined by docker-compose.yml.

  3. docker-compose build
    ๐Ÿ—๏ธ Builds or rebuilds services defined in the compose file.

  4. docker-compose logs
    ๐Ÿ“ Displays logs from all services defined in the Docker Compose setup.

  5. docker-compose ps
    ๐Ÿ“‹ Shows the status of all services in your docker-compose.yml.

5. Advanced Docker Commands ๐Ÿ”ง

  1. docker tag [source_image] [target_image:tag]
    ๐Ÿท๏ธ Tags an image for easy identification.

  2. docker save [image] > [filename.tar]
    ๐Ÿ’พ Saves an image to a tar file, which is handy for sharing offline.

  3. docker load < [filename.tar]
    ๐Ÿ“ฅ Loads an image from a tar archive.

  4. docker stats
    ๐Ÿ“Š Displays a live stream of container resource usageโ€”memory, CPU, etc.

  5. docker prune
    ๐Ÿ—‘๏ธ Removes unused containers, images, networks, and volumes in one sweep to free up space.

  6. docker system df
    ๐Ÿ“Š Shows disk usage for Docker. It's like df, but Docker-specific.

  7. docker update [container_id] --memory [limit]
    โš™๏ธ Updates the configuration of a container, such as limiting its memory usage.

6. Security and Best Practices ๐Ÿ”’

  1. docker scan [image]
    ๐Ÿ” Scans the image for vulnerabilities. Always be aware of security issues.

  2. docker login
    ๐Ÿ”‘ Authenticates your Docker client to the Docker Hub.

  3. docker logout
    ๐Ÿšช Logs out of Docker Hub from the Docker CLI.

  4. docker secret create [secret_name] [file]
    ๐Ÿ” Stores sensitive information, like API keys, securely in Docker Swarm.

  5. docker secret ls
    ๐Ÿ“‹ Lists all secrets currently managed by Docker.

7. Cleanup and Troubleshooting ๐Ÿงน๐Ÿ”

  1. docker container prune
    ๐Ÿ—‘๏ธ Removes all stopped containers.

  2. docker image prune
    ๐Ÿ—‘๏ธ Deletes dangling imagesโ€”those no longer associated with a running container.

  3. docker network prune
    ๐Ÿ—‘๏ธ Cleans up unused networks to reduce clutter.

  4. docker volume prune
    ๐Ÿ—‘๏ธ Cleans up unused volumes.

  5. docker system prune -a
    โš ๏ธ Wipes out all unused data: containers, images, volumes, and networks. Be cautiousโ€”this command is thorough.

Conclusion ๐ŸŽ‰

Understanding Docker commands is crucial for managing your containers effectively. With these top 50 commands, youโ€™re ready to tackle the most common Docker challenges, from starting containers to managing storage and networking. Docker becomes much easier to work with when you know what tools are at your disposal. ๐Ÿ› ๏ธ

Need More? ๐Ÿ”

This list provides a solid foundation, but Docker is constantly evolving. Stay curious and keep experimenting! Donโ€™t forget to bookmark this guide and revisit it whenever you feel lost in the sea of commands. ๐Ÿ“Œ