Docker Basic Commands

The table lists the most important commands related to the Docker:

Docker Command Syntax Explanation Example
docker --version Verify the Docker version  
docker info View system-wide information for Docker. For example Docker's root directory, Version, RAM, etc  
docker run <image_name> Run the docker image mentioned in the command docker run hello-world
docker run -p <port_no> --name <image_name> Run the docker image on a given port number and a given name docker run -p 9090:8080 --name hello-world some-name
docker start <container_name_or_container_id> Starts one or more stopped containers docker start hello-world
docker stop <container_name_or_container_id> Stops one or more running containers docker stop hello-world
docker restart <container_name_or_container_id> Restarts a Docker container docker restart 03ca6feb6efa
docker build -t <directory_of_dockerfile> Builds the image from the Dockerfile and tags the image docker build -t hello-world:web-app1 .
docker pull <image_name> Pulls the latest version of an image from the Docker Hub. Though developers can specify the particulars of an image docker pull hello-world
docker push <docker_username/image_name> Pushes the image to the Docker Hub docker push my_username/new-hello-world
docker exec -it <container_name_or_container_id> Access the docker container and run commands in a run-time container docker exec -it mongo
docker search <image_name> Searches the Docker Hub for images docker search centos
docker images List all the downloaded Docker images  
docker ps List all the running Docker containers  
docker ps -a List all the running and stopped Docker containers  
docker rm <container_name> Remove or delete a stopped Docker container. To remove a running container developer can use the -f option in the command docker rm mongo
docker rmi <image_name_or_image_id> Remove or delete the Docker images docker rmi hello-world
docker logs <container_name_or_container_id> To fetch the container logs. Developers can use the -f option in the command to fetch the live logs docker logs -f bead9d5fba90
docker login To log in with your Docker Id to push and pull images from Docker Hub  
docker logout To log out from Docker Hub  
docker volume create <volume_name> To create a volume that Docker container will use to store the data docker volume create postgres-data
docker volume ls To check if the volume got created or not