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
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 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>
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..]
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 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 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 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}
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
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 commands
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
FAQs on Docker Commands
Q.1: What is Docker basics?
Docker is an open-source platform where you can containeriz your application.
Q.2: What is the Docker file with all commands?
Dockerfile is set of instruction to build the Docker image