In this section, we will cover the fundamental Docker commands that you need to get started with Docker. These commands will help you manage Docker images, containers, and other Docker resources. By the end of this section, you should be comfortable with the basic operations in Docker.

Key Concepts

  1. Docker CLI: The Docker Command Line Interface (CLI) is the primary way to interact with Docker. It allows you to manage images, containers, networks, and volumes.
  2. Images: Read-only templates used to create containers.
  3. Containers: Instances of Docker images that run applications.
  4. Docker Daemon: The background service that manages Docker objects.

Basic Commands Overview

Here is a list of basic Docker commands that we will cover:

  1. docker --version
  2. docker info
  3. docker pull
  4. docker images
  5. docker run
  6. docker ps
  7. docker stop
  8. docker rm
  9. docker rmi

Detailed Explanation and Examples

  1. docker --version

This command displays the installed Docker version.

docker --version

Output:

Docker version 20.10.7, build f0df350

  1. docker info

This command provides detailed information about the Docker installation, including the number of containers, images, and the storage driver.

docker info

Output:

Containers: 2
 Running: 1
 Paused: 0
 Stopped: 1
Images: 5
...

  1. docker pull

This command downloads a Docker image from a repository (usually Docker Hub).

docker pull hello-world

Output:

Using default tag: latest
latest: Pulling from library/hello-world
...
Status: Downloaded newer image for hello-world:latest

  1. docker images

This command lists all the Docker images available on your local machine.

docker images

Output:

REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   2 months ago   13.3kB

  1. docker run

This command creates and starts a new container from a specified image.

docker run hello-world

Output:

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

  1. docker ps

This command lists all running containers.

docker ps

Output:

CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS         PORTS     NAMES

To list all containers (running and stopped), use:

docker ps -a

Output:

CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
d9b100f2f636   hello-world   "/hello"   5 minutes ago   Exited (0) 5 minutes ago             wonderful_morse

  1. docker stop

This command stops a running container.

docker stop <container_id>

Example:

docker stop d9b100f2f636

  1. docker rm

This command removes a stopped container.

docker rm <container_id>

Example:

docker rm d9b100f2f636

  1. docker rmi

This command removes a Docker image.

docker rmi <image_id>

Example:

docker rmi d1165f221234

Practical Exercise

Exercise 1: Basic Docker Commands

  1. Check Docker Version: Run the command to check your Docker version.
  2. Pull an Image: Pull the nginx image from Docker Hub.
  3. List Images: List all Docker images on your local machine.
  4. Run a Container: Run a container using the nginx image.
  5. List Running Containers: List all running containers.
  6. Stop the Container: Stop the running nginx container.
  7. Remove the Container: Remove the stopped nginx container.
  8. Remove the Image: Remove the nginx image from your local machine.

Solutions

  1. Check Docker Version:

    docker --version
    
  2. Pull an Image:

    docker pull nginx
    
  3. List Images:

    docker images
    
  4. Run a Container:

    docker run -d -p 80:80 nginx
    
  5. List Running Containers:

    docker ps
    
  6. Stop the Container:

    docker stop <container_id>
    
  7. Remove the Container:

    docker rm <container_id>
    
  8. Remove the Image:

    docker rmi nginx
    

Common Mistakes and Tips

  • Forgetting to Stop Containers: Always stop a container before removing it.
  • Using Incorrect IDs: Ensure you use the correct container or image ID.
  • Network Ports: When running a container, ensure the ports are not already in use.

Conclusion

In this section, we covered the basic Docker commands that are essential for managing Docker images and containers. These commands form the foundation of working with Docker, and mastering them will make it easier to understand more advanced concepts. In the next section, we will dive deeper into understanding Docker images.

© Copyright 2024. All rights reserved