Basic Docker CLI Commands
Your Baby Steps Into Docker
In this article we will discuss the most basic and important Docker CLI commands you need to be aware of. Docker is a famous tool designed to make it easier to create, deploy, and run applications by using containers. I explained that introduction part in my first docker article. Containers are very important in application development, some data science activities, and security.
Docker Pull
docker pull <IMAGE_NAME>
This command is used to pull images from the docker repository (hub.docker.com).
Docker Start
docker start <CONTAINER_NAME_OR_ID>
This will start an existing container.
Docker PS — Process Status
docker ps -a
This command is used to show all the running and other existing containers.
docker ps
This command is used to list the running containers.
docker ps -aq
This command will show all container IDs only.
Docker Run
docker run --name <CONTAINER_NAME_YOU_WANT> <DOCKER_IMAGE_ID>
Using the naming command you can assign a container name you prefer, in the below put the name for the ubuntu container. This docker run command is a combination of docker pull and docker start command (docker run = docker pull + docker start).
Docker RM — Remove
docker rm <CONTAINER_ID>
This command use deletes Specific container. To remove all containers that existing, can use the docker rm $(docker ps -aq) command also. I have checked the existing containers before and after removing the container below.
Note:- when you typing container id, no need to type the whole id. You can type the first 3 or 4 letters and those letters should be unique within your existing containers.
Docker Images
docker images
This will list all the information of downloaded images from the Docker hub.
docker images -q
This command will list image IDs only.
Docker RMI — Remove Image
docker rmi <IMAGE_NAME_OR_ID>
This command will remove the specific Docker image.
Docker Stop
docker stop <CONTAINER_ID>
This will stop the container that runs.
Docker Kill
docker kill <CONTAINER_ID>
This will kill the whole process of the container that runs. There is a difference between the stop and kill commands.
- docker stop - try to stop it politely. This will take some time to stop the container.
- docker kill - this will stop the main entry point of the process and stop it immediately.
Docker Detached Mode
- docker detached mode means that a Docker container runs in the background of your terminal.
- It does not receive input or display output. Using detached mode also allows you to close the opened terminal session without stopping the container.
docker run -d < IMAGE_NAME >
docker run -d <IMAGE_NAME> sleep 1000
This command will run the container in detached mode and stop the container after 1000 seconds.
Docker Logs
docker logs < CONTAINER_NAME_OR_ID >
This will show all the logs of the particular container.
Docker Stats
docker stats <CONTAINER_NAME_OR_ID>
This is showing how much resources are consuming each time. Every second refresh those values and get updated information.
Docker Inspect
docker inspect <CONTAINER_ID>
This will show more container properties like IP address, mac address of the container.
Wrap-Up
If you have some knowledge about Docker and are pretty much confident with the basic stuff and to expand your knowledge hopefully this overview has helped you better understand Docker.