Open In App

Docker Commands

Last Updated : 28 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Docker originally used straightforward, individual commands for each task. Over time, as more features were added, this approach became harder to manage. To make the interface clearer and more structured, Docker introduced an object‑based command system, grouping commands by the type of resource they control.

This makes the CLI more organized and easier to understand, and while the old commands remain for compatibility, the newer format is recommended for ongoing use.

Old CommandNew (Recommended) CommandPurpose
docker psdocker container lsList containers
docker rundocker container runRun a command in a new container
docker rmdocker container rmRemove one or more containers
docker imagesdocker image lsList images
docker rmidocker image rmRemove one or more images

Docker Container Run command

This is the most fundamental command. It creates a new container from a specified image and then starts it. If the image is not found locally, Docker will automatically try to pull it first.

$ docker container run <image_name>
To give name of container
$ docker container run --name <container_name> <image_name>
docker run image
 

Docker Pull

This command allows you to pull any image which is present in the official registry of docker, Docker hub. By default, it pulls the latest image, but you can also mention the version of the image.

$ docker pull <image_name>
Redis image latest version
 

Docker PS

This command (by default) shows us a list of all the running containers. We can use various flags with it.

  • -a flag:  shows us all the containers, stopped or running.
  • -l flag: shows us the latest container.
  • -q flag: shows only the Id of the containers. 
$ docker ps [options..]
Running Containers
 

Docker Container Stop

This command allows you to stop a container if it has crashed or you want to switch to another one.

$ docker container stop <container_ID>
docker stop image

Docker Container Start

Suppose you want to start the stopped container again, you can do it with the help of this command.

$ docker container start <container_ID>

Docker rm

Removes one or more stopped containers. You cannot remove a container that is still running; you must stop it first. You can use the docker stop <container_name or ID> command to stop the container.

Some important flags:

  • -f flag: remove the container forcefully.
  • -v flag: remove the volumes.
  • -l flag: remove the specific link mentioned.
$ docker rm {options} <container_name or ID>
docker remove an image

Docker RMI

To delete the image in docker. You can delete the images which are useless from the docker local storage so you can free up the space

docker rmi <image ID/ image name>

Docker Images

Lists all the pulled images which are present in our system.

$ docker images
docker images metadata

Docker exec

This command allows us to run new commands in a running container. This command only works until the container is running, after the container restarts, this command does not restart.

Some important flags:

  • -d flag: for running the commands in the background.
  • -i flag: it will keep STDIN open even when not attached.
  • -e flag: sets the environment variables 
$ docker exec {options}
ubuntu containerexec command

Docker Ports (Port Mapping)

In order to access the docker container from the outside world, we have to map the port on our host( Our laptop for example), to the port on the container. This is where port mapping comes into play.

$ docker run -d -p <port_on_host> 
<port_on_container> Container_name
port 8080 on the host is mapped to container

So these were the 9 most basic docker commands that every beginner must know. Containerization is a very vast topic but you can start from the very basic commands and by practicing them daily you can master them.

Docker Login

The Docker login command will help you to authenticate with the Docker hub by which you can push and pull your images.

docker login 

It will ask you to enter the username and password after that you will authenticate with DockerHub and you can perform the tasks.

Docker Push

Once you build your own customized image by using Dockerfile you need to store the image in the remote registry which is DockerHub for that you need to push your image by using the following command. To know more about How to Push a Container Image to a Docker Repository?

docker push <Image name/Image ID> 

Docker Build

The docker build command is used to build the docker images with the help of Dockerfile.

docker build -t image_name:tag .

In the place of image_name use the name of the image you build with and give the tag number and . "dot" represents the current directory.

Prune Command

This is a powerful command that removes all unused Docker objects in one go. By default, it removes:

  • All stopped containers.
  • All Build Cache.
  • All unused networks.
  • All dangling images (images that are not tagged and not used by any container).

docker system prune


Docker Stop

You can stop and start the docker containers where you can do the maintenance for containers. To stop and start specific containers you can use the following commands.

docker stop container_name_or_id

Stop Multiple Containers

Instead of stopping a single container. You can stop multiple containers at a time by using the following commands.

docker stop container1 container2 container3

Docker Restart

While running the containers in Docker you may face some errors and containers fails to start. You can restart the containers to resolve the containers by using the following commands.

docker restart container_name_or_id

Docker Inspection

Docker containers will run into some errors in real time to debug the container's errors you can use the following commands.

docker inspect container_name_or_id

Docker Container Top Command

Displays the running processes inside a container. It's like running the top command on Linux, but targeted at a specific container.

docker container top <container_id_or_name>

Docker Commit command

After running the containers by using the current image you can make the updates to the containers by interacting with the containers from that containers you can create an image by using the following commands.

docker commit container_name_or_id new_image_name:tag

Docker Commands List

Following are the docker commands which listed form build and Docker image to running it an Docker container and attaching the docker volumes to it.

Docker Image Command

  1. docker build command: It will build Docker images by using the Dockerfile.
  2. docker pull command: Docker pull command will pull the Docker image whcih is avalible in the dockerhub.
  3. docker images command: It will list all the images which are pulled and build in the docker host.
  4. docker inspect command: It will helps to debug the docker image if any errors occurred while building an image or pulling the image.
  5. docker push command: Docker command will push the docker image into the Dockerhub.
  6. docker save command: It will save the docker image in the form of dockerfile.
  7. docker rmi command: It will remove the docker image.

Docker Container Command

  1. docker attach command: Connecting to an Existing Container.
  2. docker ps command: To list the running containers.
  3. docker container inspect infinite Command: To Inspect the Docker containers.
  4. docker exec command: To execute the commands in the running containers.
  5. docker cp command: To copy the file from docker host to the docker containers,
Suggested Quiz
6 Questions

What is the primary function of Docker?

  • A

    Containerizing applications

  • B

    Hosting web servers

  • C

    Providing a virtualization platform

  • D

    Managing databases

Explanation:


What command is used to pull an image from the Docker registry?

  • A

    docker build

  • B

    docker run

  • C

    docker pull

  • D

    docker exec

Explanation:


Which command is used to list all the running containers?

  • A

    docker ps

  • B

    docker rm

  • C

    docker images

  • D

    docker start

Explanation:


What command is used to stop a running container?

  • A

    docker start

  • B

    docker stop

  • C

    docker rm

  • D

    docker exec

Explanation:


Which command is used to create a new Docker image from a running container?

  • A

    docker commit

  • B

    docker build

  • C

    docker pull

  • D

    docker run

Explanation:


Which of the following commands can be used to list all Docker containers?

  • A

    docker container ls

  • B

    docker ps

  • C

    docker container ls -a

  • D

    docker ps -a

Explanation:
  • docker container ls:
    This command lists all running containers. It will not show stopped or exited containers.
  • docker ps:
    This is an older command (still widely used), equivalent to docker container ls. It also lists only the running containers, not stopped ones.
  • docker container ls -a:
    Adding the -a flag displays all containers, including running, stopped, and exited ones.
  • docker ps -a:
    This is equivalent to docker container ls -a and displays all containers.
Quiz Completed Successfully
Your Score :   2/6
Accuracy :  0%
Login to View Explanation
1/6 1/6 < Previous Next >

Article Tags :

Explore