This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

【Docker series 】 Docker learning 2, Docker common commands

Basic Help commands

#View basic version information of Docker
docker version

#View the docker system information, such as image and container number
docker info

#View the help of a commandDocker xx command --helpCopy the code

We can see the official documentation:

The mirror command

Docker images View images

Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]

View the mirror on the local server

# Docker images REPOSITORY TAG IMAGE ID CREATED SIZE Ubuntu Latest 1318b700e415 5 days ago 72.8MB Hello - World Latest D1165f221234 4 months ago 13.3kBCopy the code
The keyword explain
REPOSITORY Warehouse source
TAG Image label
CREATED Creation time
SIZE Image size

Optional parameters:

Options: -a, --all Displays all images. -q, --quiet Displays only the image IDCopy the code

Docker Search searches for images

Search for redis as an example

# Docker search Redis NAME DESCRIPTION STARS OFFICIAL AUTOMATED source Redis redis is an open source key-value store that... 9734 [OK] Sameersbn/Redis 83 [OK] Grokzen/Redis -cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.78 Rediscommander/Redisis-Commander Alpine Image for Redisis-commander-Redis man... 6.78 Rediscommander/Redisis-Commander Alpine Image for Redisis-commander-Redis man... 63 [OK]Copy the code

Combined with parameters

Filter images with STARS greater than 2000

# docker search redis -f STARS=2000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value Store that... 9734 [OK]Copy the code

We can also search for images directly on the page from DockerHub

The search result is the same as that obtained using the command

Docker pull Downloads images

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Example Download the Redis image

# Docker pull redis Using default tag: latest # docker pull redis Using default tag: latest 26a746039521: Pull complete 18d87DA94363: Pull complete 5e118A708802: Pull complete ECf0dbe7c357 Pull complete 46f280ba52da: Pull complete Digest: Sha256: cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59 # signature Status: Downloaded newer image for redis: latest docker. IO/library/redis: latest # redis real download addressCopy the code

So the above download operation docker pull redis and docker pull docker. IO/library/redis: latest agreement

The redis supported versions are available on DockerHub

We download a version 6 of Redis

# docker pull redis:6
6: Pulling from library/redis
Digest: sha256:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59
Status: Downloaded newer image for redis:6
Copy the code

You can see that when you download Redis version 6, there is no layer download, indicating that the layer download seen above is shared

View the image you just installed

docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
redis             6         aa4d65e670d6   8 days ago     105MB
redis             latest    aa4d65e670d6   8 days ago     105MB
hello-world       latest    d1165f221234   4 months ago   13.3kB
Copy the code

Docker RMI deleted the image

  • Deleting a Single Mirror
Docker rmi-f Indicates the ID of the containerCopy the code
  • Deleting multiple Mirrors
Docker rmi -f Container ID Container ID Container IDCopy the code
  • Deleting All Mirrors
docker rmi -f $(docker images -q)
Copy the code

Container order

The container is created based on the image, so let’s download an Ubuntu image

docker pull ubuntu
Copy the code

Docker run Creates and starts a container

Docker run [parameter] Image name [directive]

Common parameters are as follows:

--name=" XXX "; --name=" XXX "; --name=" XXX "; --name=" XXX "; --name=" XXX "; --name=" XXX "; --name=" XXX"Copy the code

Start ubuntu inside the container, and we can already see the host switch by the host name

root@iZuf66y3tuzn4wp3h02t7pZ:~# docker run -it ubuntu /bin/bash
Copy the code

Out of the container

  • typeexit Command, the container exits
  • Using the shortcut Ctrl + P + Q to return to the host, the container will not exit

Docker PS looks at the container

docker ps [OPTIONS]

# docker ps -a
CONTAINER ID   IMAGE          COMMAND           CREATED          STATUS                      PORTS                     NAMES
73f951b70438   ubuntu         "/bin/bash"       2 minutes ago    Up 2 minutes                                          vigorous_buck
87fb04e2a6e7   ubuntu         "/bin/bash"       7 minutes ago    Exited (0) 3 minutes ago                              flamboyant_tu
Copy the code

Optional parameters:

			#View the running containers-s # Check the size of the container. -q # Check the ID of the containerCopy the code

Docker RM deleted the container

Docker rm containers ID # delete not running docker rm -f container ID # mandatory delete a running container docker rm -f $(docker ps - aq) # delete all containers docker ps - aq | xargs Docker rm # Delete all containersCopy the code

Start, restart, stop, kill Start, restart, stop, forcibly stop the container

Docker Start container ID Docker restart container ID Docker Stop container ID Docker kill container IDCopy the code

Other Common Commands

Docker run -d Starts the container in the background

#Start an Ubuntu in the background
docker run -d ubuntu

#View the running container
docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES

Copy the code

Found no containers running

Here’s why:

  • Docker background starts the service, there needs to be a foreground process, otherwise Docker will stop the service if it finds no application

Let’s take the initiative to add a foreground process and see what happens

#Temporarily add a foreground processdocker run -d ubuntu /bin/bash -c "while true; do echo xiaozhupeiqi; sleep 2; done"
#View the running containersDocker ps CONTAINER ID IMAGE COMMAND 10ba0e687434 ubuntu "/bin/bash -c 'while..."Copy the code

The docker ps command can now view the running container. OK

Docker logs View logs

Docker logs [Parameter] Container ID

Options: -f # to be consistent with the output -n # to print the last few lines -t # to print the timestampCopy the code

View the logs of the above containers

# docker logs -tf -n 5 10ba0e687434Xiaozhupeiqi 2021-08-01 T08:02:51.380512218z XiaozhupeiQi 2021-08-01 T08:02:53.381606198z XiaozhupeiQi 2021-08-01 T08:02:55.382780869z Xiaozhupeiqi 2021-08-01T08:02:57.383890580z XiaozhupeiQi 2021-08-01T08:02:59.384977473z Xiaozhupeiqi The 2021-08-01 T08:03:01. 386430484 z xiaozhupeiqiCopy the code

Docker top View process information in the container

Docker Top container ID

# docker top 10ba0e68743UID PID PPID CMD root 11101 11073 bin/bash -c while true; do echo xiaozhupeiqi; sleep 2; done root 11504 11101Copy the code

Docker Inspect Checks the image metadata

Docker INSPECT Container ID

A large amount of information is omitted in the output

[{"Id": "10ba0e6874341b2e2f002c22613a71223ca981dc36df0d1ea4ed3bb5a7a6c58e"."Created": "The 2021-08-01 T07:57:52. 725305443 z"."Path": "/bin/bash"."Args": [
            "-c"."while true; do echo xiaozhupeiqi; sleep 2; done"]."State": {... },..."GraphDriver": {... },"Mounts": []."Config": {
            "Hostname": "10ba0e687434"."Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]."Cmd": [
                "/bin/bash"."-c"."while true; do echo xiaozhupeiqi; sleep 2; done"]."Image": "ubuntu". },"NetworkSettings": {... }}]Copy the code

Docker exec enters the currently running container

Docker exec [parameter] container ID

# docker exec -it 10ba0e687434 /bin/bashroot@10ba0e687434:/# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 08:04 ? 00:00:00 /bin/bash -c while true; do echo xiaozhupeiqi; sleep 2; done root 922 0 0 08:34 pts/0 00:00:00 /bin/bash root 963 0 0 08:35 pts/1 00:00:00 /bin/bash root 972 1 0 08:35 ? 00:00:00 sleep 2 root 973 963 0 08:35 pts/1 00:00:00 ps -efCopy the code

Docker Attach accesses the program that is executing in the container

Docker ATTACH container ID

docker attach 10ba0e687434
Copy the code

Docker Exec and Docker Attach

  • docker exec

Enter the container, will open a new terminal, can operate normally

  • docker attach

Entering a terminal where the container is executing does not start a new terminal

Docker cp copies the files in the container to the host

Docker cp Container ID: container file path Host path

#Enter the container and create a file xiaomotong. Go in the /home directory of the container
 docker exec -it 10ba0e687434 /bin/bash
 cd /home/
 touch xiaomotong.go
#Ctrl + P + Q to exit the container

#Copy files from the container to the host
docker cp 10ba0e687434:/home/xiaomotong.go ./
Copy the code

Docker STATS Checks the service memory status in Docker

# docker statsCONTAINER ID NAME CPU % MEM USAGE/LIMIT MEM % NET I/O BLOCK I/O PIDS 2772A4050157 nginX1 0.00% 3.02MiB / 1.946GiB 0.15% 25.8kB / 33.1kB 0B / 8.19kB 3 10ba0e687434 loving_bohr 0.00% 4.07MiB / 1.946GiB 0.20% 810B / 0B 1.95MB / 0B 5Copy the code


There are still a lot of commands about Docker. You can try to comb the list of commonly used commands according to the figure below, which is easy to query. The following pictures are from the network and only for learning


docker docs

Welcome to like, follow and collect

Dear friends, your support and encouragement are the motivation for me to keep sharing and improve the quality

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am nezha, welcome to like the collection, see you next time ~