Top 20 Docker Commands with Examples and Outputs

Important Docker commands with their outputs: docker pull downloads an image from Docker registry user@docker:$ docker pull nginx:latest # download latest tag Using default tag: latest latest: Pulling from library/nginx a480a496ba95: Pull complete Digest: sha256:28402db69fec7c17e179ea87882667 Status: Downloaded newer image for nginx:latest docker.o/library/nginx:latest docker run runs a container from an image user@docker:$ docker run -d -p 8080:80 nginx:latest # -d => run in detach mode (background) # -p => publish port : 8f9f97dc08ffddb173ebe61eaa871b2944 docker ps lists all running containers user@docker:$ docker ps user@docker:$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS 8f9f97dc08ff nginx:latest "/docker-entrypoint" 2 minutes ago Up 2 minutes docker images lists all downloaded images user@docker:$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 3b25b682ea82 4 weeks ago 192MB docker rm removes a stopped container user@docker:$ docker container rm -f my_container 8f9 user@docker:$ docker image rm -f nginx # force to delete image Untagged: nginx:latest Untagged: nginx@sha256:28402db69fec7c17e179ea8788266 Deleted: sha256:3b25b682ea82b2db3cc4fd48db818be788ee Deleted: sha256:3e8a4396bcdb62aeb916ec1e4cf645000380 docker rmi removes a specific image user@docker:$ docker rmi nginx Untagged: nginx:latest Untagged: nginx@sha256:28402db69fec7c17e179ea8788266 Deleted: sha256:3b25b682ea82b2db3cc4fd48db818be788ee Deleted: sha256:3e8a4396bcdb62aeb916ec1e4cf645000380 docker exec runs a command inside a running container user@docker:$ docker exec -it my_container bash user@docker:$ docker exec -it my_container bin/sh # pwd / # read escape sequence # CTRL + P + Q => go to host user@docker:$ docker build builds an image from a Dockerfile user@docker:$ docker build -t myapp . # if Dockerfile in the same directory user@docker:$ docker build -t myapp -f /other-directory/Dockerfile . # if Dockerfile in the different directory path docker logs shows logs from a container user@docker:$ docker logs my_container 2024/11/01 21:20:08 [notice] 1#1: nginx/1.27.2 2024/11/01 21:20:08 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 2024/11/01 21:20:08 [notice] 1#1: OS: Linux 5.10.60.1 docker network ls lists all networks user@docker:$ docker network ls NETWORK ID NAME DRIVER SCOPE 19cb6e2b2cab bridge bridge local 8ab407104066 host host local 67944d3050a3 none null local docker volume ls lists all volumes user@docker:$ docker volume ls DRIVER VOLUME NAME local 3a243556537e4e11 local myvolume docker login login dockerhub or docker registry user@docker:$ docker login Login with your Docker ID to push and pull images from Docker Hub. Username: Password: docker tag creates a new tag for an image, useful for renaming or versioning user@docker:$ docker tag nginx:latest my_nginx:latest REPOSITORY TAG IMAGE ID CREATED SIZE my_nginx latest 3b25b682ea82 4 weeks ago 192MB nginx latest 3b25b682ea82 4 weeks ago 192MB docker push uploads an image to Docker Hub or another Docker registry user@docker:$ docker push /my_nginx The push refers to repository [docker.io//my_nginx] e4e9e9ad93c2: Mounted from library/nginx latest: digest: sha256:7ba542bde95e6523a4b126f610 size: 1778 docker save saves a Docker image as a .tar archive user@docker:$ docker save -o .tar user@docker:$ docker save -o my_nginx.tar my_nginx user@docker:$ ls my_nginx.tar docker load loads an image from a .tar archive user@docker:$ docker load -i my_image.tar user@docker:$ docker load -i my_nginx.tar Loaded image: my_nginx:latest docker stats displays real-time resource usage statistics for containers user@docker:$ docker stats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS docker-compose up starts and runs all services defined in a docker-compose.yml file user@docker:$ docker-compose up -d # -d => run detach mode, background docker-compose down stops and removes all services and networks created by docker-compose up user@docker:$ docker-compose down docker system prune cleans up unused containers, images, networks, and volumes user@docker:$ docker system prune user@docker:$ docker system prune -a WARNING! This will remove: - all stopp

Jan 15, 2025 - 09:47
Top 20 Docker Commands with Examples and Outputs

Important Docker commands with their outputs:

docker pull

  • downloads an image from Docker registry
user@docker:$ docker pull nginx:latest # download latest tag
Using default tag: latest
latest: Pulling from library/nginx
a480a496ba95: Pull complete
Digest: sha256:28402db69fec7c17e179ea87882667
Status: Downloaded newer image for nginx:latest
docker.o/library/nginx:latest

docker run

  • runs a container from an image
user@docker:$ docker run -d -p 8080:80 nginx:latest
# -d => run in detach mode (background)
# -p => publish port  :
8f9f97dc08ffddb173ebe61eaa871b2944

docker ps

  • lists all running containers
user@docker:$ docker ps
user@docker:$ docker ps -a
CONTAINER ID  IMAGE               COMMAND                     CREATED            STATUS                      
8f9f97dc08ff     nginx:latest     "/docker-entrypoint"   2 minutes ago   Up 2 minutes 

docker images

  • lists all downloaded images
user@docker:$ docker images
REPOSITORY      TAG        IMAGE ID            CREATED             SIZE
nginx                     latest     3b25b682ea82   4 weeks ago     192MB

docker rm

  • removes a stopped container
user@docker:$ docker container rm -f my_container
8f9
user@docker:$ docker image rm -f nginx            # force to delete image
Untagged: nginx:latest
Untagged: nginx@sha256:28402db69fec7c17e179ea8788266
Deleted: sha256:3b25b682ea82b2db3cc4fd48db818be788ee
Deleted: sha256:3e8a4396bcdb62aeb916ec1e4cf645000380

docker rmi

  • removes a specific image
user@docker:$ docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:28402db69fec7c17e179ea8788266
Deleted: sha256:3b25b682ea82b2db3cc4fd48db818be788ee
Deleted: sha256:3e8a4396bcdb62aeb916ec1e4cf645000380

docker exec

  • runs a command inside a running container
user@docker:$ docker exec -it my_container bash
user@docker:$ docker exec -it my_container bin/sh
# pwd
/
# read escape sequence      # CTRL + P + Q => go to host
user@docker:$

docker build

  • builds an image from a Dockerfile
user@docker:$ docker build -t myapp .    
# if Dockerfile in the same directory
user@docker:$ docker build -t myapp -f /other-directory/Dockerfile .
# if Dockerfile in the different directory path

docker logs

  • shows logs from a container
user@docker:$ docker logs my_container
2024/11/01 21:20:08 [notice] 1#1: nginx/1.27.2
2024/11/01 21:20:08 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/11/01 21:20:08 [notice] 1#1: OS: Linux 5.10.60.1

docker network ls

  • lists all networks
user@docker:$ docker network ls
NETWORK ID     NAME        DRIVER    SCOPE
19cb6e2b2cab   bridge       bridge    local
8ab407104066   host           host      local
67944d3050a3   none          null      local

docker volume ls

  • lists all volumes
user@docker:$ docker volume ls
DRIVER    VOLUME NAME
local          3a243556537e4e11
local          myvolume

docker login

  • login dockerhub or docker registry
user@docker:$ docker login
Login with your Docker ID to push and pull images from Docker Hub.
Username:
Password:

docker tag

  • creates a new tag for an image, useful for renaming or versioning
user@docker:$ docker tag nginx:latest my_nginx:latest
REPOSITORY    TAG            IMAGE ID                CREATED           SIZE
my_nginx           latest        3b25b682ea82      4 weeks ago     192MB
nginx                   latest        3b25b682ea82      4 weeks ago     192MB

docker push

  • uploads an image to Docker Hub or another Docker registry
user@docker:$ docker push /my_nginx
The push refers to repository [docker.io//my_nginx]
e4e9e9ad93c2: Mounted from library/nginx
latest: digest: sha256:7ba542bde95e6523a4b126f610 size: 1778

docker save

  • saves a Docker image as a .tar archive
user@docker:$ docker save -o .tar  
user@docker:$ docker save -o my_nginx.tar my_nginx
user@docker:$  ls
my_nginx.tar

docker load

  • loads an image from a .tar archive
user@docker:$ docker load -i my_image.tar
user@docker:$ docker load -i my_nginx.tar
Loaded image: my_nginx:latest

docker stats

  • displays real-time resource usage statistics for containers
user@docker:$ docker stats
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT   MEM %     NET I/O   BLOCK I/O   PIDS

docker-compose up

  • starts and runs all services defined in a docker-compose.yml file
user@docker:$ docker-compose up -d      
# -d => run detach mode, background

docker-compose down

  • stops and removes all services and networks created by docker-compose up
user@docker:$ docker-compose down

docker system prune

  • cleans up unused containers, images, networks, and volumes
user@docker:$ docker system prune
user@docker:$ docker system prune -a
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache
Are you sure you want to continue? [y/N] y
Deleted Containers: 94eabb9dd32f55061bc3d6