Docker is a container-based application framework that allows deploying programs that are run as containers. Docker Containers are generally used in CI/CD (Continuous Integration/Continuous Deployment) platform. There are several docker commands you must know when working with Docker.
In this tutorial, we will help you to understand some basic Docker commands.
Search Docker Images
We can search the Docker images in Docker Hub. Let’s assume you want to find CentOS images.
# docker search centos
Download Docker Container Images
Once you find the image you can pull the image to the local system from Docker hub. Docker pull is almost the same as git pull.
# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8a29a15cefae: Pull complete
Status: Downloaded newer image for centos:latest
View all Downloaded Docker Container Images
Whenever we download the docker images in your local system using pull command and it saves the docker image locally at /var/lib/docker location. To list all the docker images pulled on the system use following command.
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 1d622ef86b13 12 days ago 73.9MB
nginx latest 602e111c06b6 12 days ago 127MB
httpd latest b2c2ab6dcf2e 12 days ago 166MB
centos latest 470671670cac 3 months ago 237MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB
docker/whalesay latest 6b362a9f73eb 4 years ago 247MB
Run a Docker Container
Use the following command to run the downloaded Docker image.
# docker run –it centos
-it: It will open a shell that can be used to interact with the container.
Run a Docker Container in Detach Mode
We can launch a container in detached mode using –d option using the following command.
# docker run –d centos
We can list all the running containers using the following command.
# docker ps
List Container including Shutdown State
To List all the containers including shutdown using the following command.
# docker ps -a
Getting Container Console
If Docker container is running in detach mode and need to take the console of the container using the following command.
# docker attach priceless_austin
Start, Stop, Restart and Kill Containers
We can start, stop and restart docker containers.
Start the container using mention command.
# docker start priceless_austin
Stop the container using mention command.
# docker stop priceless_austin
Restart the container using mention command.
# docker restart priceless_austin
Kill the container using mention command.
# docker kill priceless_austin
Remove the Docker containers using the container id or container name. Containers will only remove when docker is in a stopped state.
# docker rm 0fb1de712c0d
To remove a running container forcefully, use ‘-f’ option
# docker rm -f f529d03c4692
Delete/Remove Docker Containers Images
Not like containers you can remove the container images also. You can use image name and id to remove the images. Use the mentioned command to remove the container images.
# docker rmi nginx
Display History of Docker Image
History of Docker command will display the list of commands used while building docker images.
# docker history httpd
Containers resource usage statistics
Display CPU, memory, and network I/O usage of all the containers using the following command. Below command will live to show live streaming of resource usage.