
10 months ago
Docker and Its Essential Commands: A Beginner’s Guide
14 Min Read
Introduction Docker has revolutionized the way developers build, ship, and run applications. It provides a lightweight, consistent environment that ensures applications run smoothly across different systems. Whether you’re a beginner or an experienced developer, understanding Docker and its essential commands is crucial for efficient container management. In this guide, we’ll cover what Docker is, its benefits, and some of the most commonly used Docker commands. What is Docker? Docker is an open-source platform that allows developers to automate the deployment of applications inside lightweight, portable containers. Unlike traditional virtual machines (VMs), Docker containers share the host OS kernel, making them more efficient and faster. Benefits of Docker: Portability: Docker containers work the same way across different environments (local, staging, production). Efficiency: Containers share resources and use less memory compared to VMs. Scalability: Easy to scale applications by spinning up multiple containers. Consistency: Eliminates the “works on my machine” problem. Installing Docker Before diving into commands, make sure Docker is installed on your system. You can download it from Docker’s official website. Essential Docker Commands Here are some fundamental Docker commands that you should know: 1. Checking Docker Installation docker --version This command verifies that Docker is installed and displays its version. 2. Pulling a Docker Image docker pull <image_name> Example: docker pull nginx This fetches the latest nginx image from Docker Hub. 3. Listing Downloaded Images docker images This displays all downloaded images on your system. 4. Running a Container docker run -d -p 8080:80 --name mycontainer nginx -d: Runs the container in detached mode (background). -p 8080:80: Maps port 8080 on the host to port 80 in the container. --name mycontainer: Assigns a custom name to the container. nginx: The image to use. 5. Listing Running Containers docker ps This lists all active containers. To see all containers (including stopped ones), use: docker ps -a 6. Stopping a Running Container docker stop <container_id_or_name> Example: docker stop mycontainer 7. Removing a Container docker rm <container_id_or_name> Make sure the container is stopped before removing it. 8. Removing an Image docker rmi <image_id_or_name> This deletes a Docker image from your system. 9. Building a Custom Image If you have a Dockerfile, use this command to build a custom image: docker build -t mycustomimage . -t mycustomimage: Tags the image with a name. .: Refers to the current directory where the Dockerfile is located. 10. Running a Shell Inside a Container To execute commands inside a running container: docker exec -it <container_id_or_name> /bin/bash Example: docker exec -it mycontainer /bin/bash 11. Viewing Container Logs docker logs <container_id_or_name> This helps in debugging by displaying the logs of a running container. 12. Removing All Stopped Containers docker container prune This removes all stopped containers to free up disk space. Conclusion Docker simplifies the development and deployment of applications by providing a consistent and portable environment. By mastering these essential commands, you can efficiently manage Docker containers and streamline your workflows.