Docker Command Reference

Essential Docker commands with explanations and examples

docker run

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

Starts a new container from the nginx image. If the image isn't available locally, Docker will pull it from the internet. After run this command check on - http://localhost:8080

Explanation:

  • docker run → Start a new container
  • -d → Detached mode (runs in background)
  • -p 8080:80 → Maps host port 8080 to container port 80.
  • --name webserver → Names the container for easier management
  • nginx → The image to use

docker ps

docker ps

Shows a list of currently running containers along with their details.

Usage:

Use this command to check the status of your running containers, see their IDs, names, and which ports they're using.

docker images

docker images

Lists all Docker images stored locally on your system.

Usage:

This command helps you manage your local Docker images, see their sizes, and determine when they were created.

docker exec

docker container exec -it webserver bash

Executes a command inside a running container. This example opens a bash shell in the container.

Explanation:

  • docker container exec → Run command in running container
  • -it → Interactive mode with pseudo-TTY
  • webserver → Container name to connect to
  • bash → Command to run inside container

docker stop

docker stop webserver

Stops a running container. The container will remain available and can be restarted.

Note:

After stopping, the container will no longer appear in docker ps but will appear in docker ps -a which shows all containers including stopped ones.

docker rm

docker rm webserver

Removes a stopped container from your system, freeing up resources.

Note:

You must stop a container before removing it, or use the -f flag to force removal of a running container.

docker rmi

docker rmi nginx

Removes a Docker image from your local storage.

Note:

You cannot remove an image that is being used by a container (even stopped containers). Remove dependent containers first.