Open In App

Docker Commands

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is an open-source project that automates the deployment of applications as movable, independent containers that can run locally or in the cloud. You can divide your applications from your infrastructure with the help of Docker, allowing for quick software delivery and it also allows you to manage your infrastructure in the same ways that you manage your applications.

 The number of commands found in docker is very huge in number, but we will be looking at the top commands in docker. To know more about Docker commands refer to Docker Cheat Sheet – Most Important Docker Commands.

Docker Commands

Docker Run command

This command is used to run a container from an image. The docker run command is a combination of the docker create and docker start commands. It creates a new container from the image specified and starts that container. if the docker image is not present, then the docker run pulls that.

$ docker run <image_name>
To give name of container
$ docker 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 Stop

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

$ docker stop <container_ID>



docker stop image

 

Docker Start

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

$ docker start <container_ID>



Docker rm

To delete a container. By default when a container is created, it gets an ID as well as an imaginary name such as confident_boyd, heuristic_villani, etc. You can either mention the container name or its ID.

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.

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 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 Basic Command

Following are the some of the docker basic commands

  1. docker images: Docker images will list all the images which are pulled or build in that docker host.
  2. docker pull: Docker pull will the docker images from the dockerhub.
  3. docker run: Docker run will run the docker image as an container.
  4. docker ps: Docker run will list all the containers which are running in the docker host.
  5. docker stop: Docker stop will stop the docker container which are already running.
  6. docker rm: Docker rm command will remove the containers which are in the stop condition.

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,

FAQs on Docker Command

1. What is Docker basics?

Docker is an open-source platform where you can containeriz your application.

2. Docker Command Not Found

Docker command not found error will get if the docker cli was not installed in your system.

3. Docker Command To list the All Containers

To list all the containers in the docker you can use the docker pa -a.

4. Docker Command To Build An Image

To build the docker image you can use the following command.

docker build -t <image name>:<tag> <path to Dockerfile>

5. Docker Command To Remove An Image

Docker Command to remove the docker images was

docker rmi <image name>:<tag>

6. Docker Command Cheat Sheet

To read the docker cheat sheet refer to Docker Cheat Sheet – Most Important Docker Commands.

7. Docker Command to Command to check the Running Containers

docker ps is the command to check the running containers.

8. What is the Docker file with all commands?

Dockerfile is set of instruction to build the Docker image



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads