Basic Docker Commands: A Comprehensive Guide


Docker is a powerful platform for developing, shipping, and running applications inside containers. Mastering Docker commands is crucial for anyone working with this technology to efficiently manage containers and images. This guide provides an overview of the most essential Docker commands and their practical applications, complete with examples.

Key Docker Commands

1. Docker Run

The docker run command is used to create a container from a specified image and start the container.


docker run -d -p 80:80 --name webserver nginx

This command runs an Nginx server in detached mode, maps port 80 of the container to port 80 on the host, and names the container "webserver".

2. Docker PS

To list all currently running containers, use docker ps. Adding the -a option will show all containers, including stopped ones.


docker ps -a

3. Docker Stop

Stops one or more running containers. The command requires one or more container IDs or names.


docker stop webserver

This command will stop the container named "webserver".

4. Docker RM

Removes one or more containers. You need to stop the container before removing it, unless you use the -f option to force the removal.


docker rm webserver

This command removes the "webserver" container.

5. Docker Images

Lists all the Docker images stored locally.


docker images

6. Docker RMI

Removes one or more Docker images. Images must not be in use by any containers to be removed.


docker rmi nginx

This command removes the Nginx image from the local image store.

7. Docker Pull

Downloads an image from a registry.


docker pull nginx

This command pulls the latest Nginx image from Docker Hub.

8. Docker Build

Builds Docker images from a Dockerfile and a "context". The context is the set of files located in the specified PATH or URL.


docker build -t mynginx .

This command builds an image with the tag "mynginx" from the Dockerfile in the current directory.

9. Docker Logs

Fetches the logs of a container. Useful for debugging and monitoring the activity of containers.


docker logs webserver

This command displays logs from the "webserver" container.

10. Docker Exec

Runs a command in a running container.


docker exec -it webserver bash

This command opens a bash shell inside the "webserver" container.

Why Use Docker Commands?

Using Docker commands allows you to:

  • Quickly manage container lifecycles.

  • Handle image distribution and storage.

  • Debug and inspect container statuses and issues.


Docker commands form the backbone of container management and orchestration within Docker environments. Understanding these commands enhances your ability to deploy, manage, troubleshoot, and scale containerized applications effectively. For more detailed information, refer to Docker's official documentation.