Basic Useful Commands for Docker

Play this article

As DevOps continues to revolutionize the software development process, Docker has become an essential tool for developers looking to build and deploy their applications more efficiently.

What is Docker:

Docker is a powerful containerization platform that allows developers to package their applications and dependencies into a single, portable container.

With Docker, developers can easily move their applications between environments, eliminate compatibility issues, and simplify the deployment process.

However, to fully leverage Docker's capabilities, it's essential to understand some of the Docker components and most useful commands for managing and troubleshooting Docker containers.

In this article, we'll explore some of the most basic and essential Docker components and commands that every DevOps engineer should know.

Let's Dive into Docker Components:

Before we dive into Docker commands, let's briefly review some of the key components of Docker. A Complete Overview of the Essential Components of Docker, Including Engine, Images, Containers, Registry, and Compose.

Docker Engine:
Docker Engine is the core component of Docker that enables containerization on a host machine. It includes a daemon process (dockerd) and a command-line interface (CLI) client (docker) that communicates with the daemon to manage containers, images, networks, and volumes.

Docker Images:
A Docker image is a lightweight, standalone, and executable package that contains everything needed to run an application, including code, libraries, dependencies, and runtime. Images are built from a Dockerfile, which is a script that defines the steps to create the image.

Docker Containers:
A Docker container is a running instance of a Docker image. Containers are isolated environments that run on top of the host machine's operating system, with their own filesystem, network, and resources. Containers are lightweight, fast, and can be easily started, stopped, and restarted.

Docker Registry:
A Docker registry is a repository that stores Docker images. The most popular Docker registry is Docker Hub, a public registry that hosts thousands of images. However, organizations can also set up private registries to store and share their own images.

Docker Compose:
Docker Compose is a tool for defining and running multi-container Docker applications. Compose uses a YAML file to define the services, networks, and volumes required by the application, and can start, stop, and manage the containers as a single unit.

Basic Useful Commands for Docker:

Docker ps:

Check All the Running Containers in the machine.

docker ps

The docker ps command is used to list all the running containers on your local machine. By default, it only shows the container ID, image name, command, and status of the Running Containers Only.

However, you can use various options with this command to display additional information, such as container names, ports, and network information.

Example: To display Only the Name and ContainerI'd of the running containers, use the following command

docker ps --format "table {{.ID}}\t{{.Names}}"

Docker run:

to Run Any Images from the machine.

docker run command is used to create and run a new container from a Docker image. You can use various options with this command to customize the container, such as specifying the image name, container name, ports, and environment variables.

Example 1: Run a container from images and specify the container name you want to call it. Use -d Flag to Run this container in detached(background) mode:

docker run -d nginx

Example 2: Run a new container from the "nginx" image and map port 80 on the host to port 80 in the container, use the following command:

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

Example 3: Run a container with a mounted volume from the local machine to docker image:

docker run -d --name mynginx -p 80:80 -v /home/user/demo:/usr/share/nginx/html nginx

This will run a container from the nginx image and mount the /path/on/host directory on the host to the /path/in/container directory inside the container.

Docker stop:

to stop any Running Container in your local machine.

docker stop command is used to stop a running container. You can specify the container ID or container name as an argument to this command.

Example1: To stop a container with the ID "7d7a18ea75f", use the following command:

docker stop 7d7a18ea75f

Example 2: To stop all the Running containers in your machine.

docker stop $(docker ps -q)

docker rm:

the docker rm command is used to remove one or more containers from your local machine. You can specify the container ID or container name as an argument to this command.

you can not remove any running containers. so to remove the container you should first stop that container and then delete that container.

Example 1: Remove multiple stopped containers in a single command:

docker rm 7d7a18ea75f 7d7a16740fbn

Example 2: Remove All the stopped containers in a single command. The docker ps -a -q command is used to get the IDs of all containers, including stopped ones.

docker rm $(docker ps -a -q)

PRO Tip: By default, when you delete a container using the docker rm command, any volumes that were created for the container will not be deleted. This means that the data stored in those volumes will persist even after the container is deleted.

If you want to delete the volumes associated with a container when you remove the container, you can use the -v option with the docker rm command. For example:

docker rm -v <container_name_or_id>

docker images:

docker images command is used to list all the Docker images on your local machine. By default, it shows the image ID, repository, tag, and size.

Example 1: You can use the REPOSITORY argument to filter images by repository name. For example, to list all images in the nginx repository, run the following command:

docker images nginx

Example 2: To display only the repository and tag of the Docker images, use the following command:

docker images --format "table {{.Repository}}\t{{.Tag}}"

docker build:

The docker build command is used to create a Docker image from a Dockerfile. A Dockerfile is a text file that contains a set of instructions that are used to build a Docker image.

The docker build command reads the instructions from the Dockerfile and builds an image according to the specified instructions.

Example: Assuming we have a directory named myapp that contains the Dockerfile and all necessary files, you can build a Docker image using the following command:

docker build -t demo-image -f Dockerfile .

In this example, the -t option is used to specify the name and tag of the Docker image that will be created. The demo-image name is used as the name of the image, and the latest is used as the tag. The -f Flag Asks for Dockerfile and it checks all the necessary files for the Dockerfile.

FROM node:14-alpine
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]
  • FROM: Specifies the base image that will be used to build the new image. In this case, the Node.js 14 Alpine image is used.

  • WORKDIR: Sets the working directory for the following instructions.

  • COPY: Copies the package.json and package-lock.json files to the working directory.

  • RUN: Installs the dependencies specified in the package.json file using the npm install command.

  • COPY: Copies all the files in the current directory to the working directory.

  • EXPOSE: Exposes port 3000 to the outside world.

  • CMD: Specifies the command that will be run when the container is started.

By running the docker build command with the appropriate options, you can build a Docker image from this Dockerfile. Once the image is built, you can use the docker run command to start a new container from the image.


Docker is a powerful tool for containerizing and deploying applications and mastering Docker commands is essential for any DevOps engineer. By understanding

Did you find this article valuable?

Support Ravi Kyada by becoming a sponsor. Any amount is appreciated!