[docker series] docker learning II, common commands of docker

Time:2021-11-29

[docker series] docker learning II, common commands of docker

Basic help commands

#View the basic version information of docker
docker version

#View the system information of docker, such as the number of images and containers
docker info

#View help for a command
Docker XX command -- help

We can see the official help document:docs.docker.com/reference/

[docker series] docker learning II, common commands of docker

Mirror command

Docker images view images

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

View images on this machine

# docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
ubuntu            latest    1318b700e415   5 days ago     72.8MB
hello-world       latest    d1165f221234   4 months ago   13.3kB
keyword explain
REPOSITORY Warehouse source
TAG Mirror label
IMAGE ID Mirror ID
CREATED Creation time
SIZE Mirror size

Optional parameters:

Options:
  -a. -- all show all mirrors
  -q. -- quiet displays only the image ID

Docker search search image

Search redis as an example

# docker search redis
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis                            Redis is an open source key-value store that…   9734      [OK]
sameersbn/redis                                                                  83                   [OK]
grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2      78
rediscommander/redis-commander   Alpine image for redis-commander - Redis man…   63                   [OK]

Add 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]

We can also search for images directly on the dockerhub page

[docker series] docker learning II, common commands of docker

The search results are consistent with the results of the command search

[docker series] docker learning II, common commands of docker

Docker pull Download Image

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

Download the redis image as an example

# docker pull redis
Using default tag: latest # default version
Latest: pulling from library / redis # redis
33847f680f63: pull complete # layered Download
26a746039521: Pull complete
18d87da94363: Pull complete
5e118a708802: Pull complete
ecf0dbe7c357: Pull complete
46f280ba52da: Pull complete
Digest: sha256: cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322ddd5b474578515422d3ddb59 # signature
Status: Downloaded newer image for redis:latest
Docker.io/library/redis: latest # redis real download address

Therefore, the above download operation
Docker pull redis is consistent with docker pull docker.io/library/redis: latest

You can view the version supported by redis on dockerhub

[docker series] docker learning II, common commands of docker

We download a 6 version of redis

# docker pull redis:6
6: Pulling from library/redis
Digest: sha256:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59
Status: Downloaded newer image for redis:6
docker.io/library/redis:6

You can see that when you download version 6 redis, there is no layered download, which indicates that they share the layered download seen above

View the image of the installation just now

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

Docker RMI delete image

  • Delete a single mirror
Docker RMI - f container ID
  • Delete multiple mirrors
Docker RMI - f container ID container ID
  • Delete all mirrors
docker rmi -f $(docker images -q)

Container command

Containers are created based on images. Let’s download a Ubuntu image

docker pull ubuntu

Docker run creates and starts a container

Docker run [parameter] image name [instruction] [parameter]

Description of common parameters:

--Name = "XXX" # running container name
-D # background operation mode
-It # interaction
-P # specifies the port number of the container, for example - P 6379:6380
                Common operations include -p host port: container port
-P # randomly specify the port number

Start the Ubuntu in the container. We can see the host switching through the host name

[email protected]:~# docker run -it ubuntu /bin/bash
[email protected]:/#

Exit container

  • typingexitCommand, the container exits
  • Use the shortcut keys Ctrl + P + Q to return to the host, and the container will not exit

Docker PS viewing containers

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

Optional parameters:

#View running containers
-A # view containers that have been run
-N = XX # view the first XX containers that have been run
-S # view the size of the container run
-Q # view container ID

Docker RM delete container

Docker RM container ID # deletes a container that is not running
Docker RM - f container ID # forces the deletion of a running container
Docker RM - f $(docker PS - AQ) # delete all containers
Docker PS - AQ | xargs docker RM # delete all containers

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 ID

Common other commands

Docker run – D background start container

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

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

No containers found running

The reasons are as follows:

  • When docker starts the service in the background, there needs to be a foreground process. Otherwise, if docker finds no application, it will stop the service

We take the initiative to add a foreground process to see the effect

#Temporarily add a foreground process
docker run -d ubuntu /bin/bash -c "while true;do echo xiaozhupeiqi;sleep 2;done"

#View running containers
docker ps
CONTAINER ID   IMAGE     COMMAND
10ba0e687434   ubuntu    "/bin/bash -c 'while…"

It can be seen that the docker PS command can view the running container, OK

Docker logs view logs

Docker logs [parameter] container ID

Options:
  -F # is consistent with the output
  -N # output the last few lines
  -T # print timestamp

View the log of the above container

# docker logs -tf -n 5 10ba0e687434
2021-08-01T08:02:51.380512218Z xiaozhupeiqi
2021-08-01T08:02:53.381606198Z xiaozhupeiqi
2021-08-01T08:02:55.382780869Z xiaozhupeiqi
2021-08-01T08:02:57.383890580Z xiaozhupeiqi
2021-08-01T08:02:59.384977473Z xiaozhupeiqi
2021-08-01T08:03:01.386430484Z xiaozhupeiqi

Docker top view the process information in the container

Docker top container ID

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

Docker inspect view image metadata

Docker inspect container ID

A large amount of information is omitted from the output information

[
     {
        "Id": "10ba0e6874341b2e2f002c22613a71223ca981dc36df0d1ea4ed3bb5a7a6c58e",
        "Created": "2021-08-01T07:57:52.725305443Z",
        "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": {
            ...
                }
    }
]

Docker exec enters the currently running container

Docker exec [parameter] container ID instruction [parameter of instruction]

# docker exec -it 10ba0e687434 /bin/bash
[email protected]:/# 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 -ef

Docker attach enters the executing program in the container

Docker attach container ID

docker attach 10ba0e687434

The difference between docker exec and docker attach

  • docker exec

After entering the container, a new terminal will be opened and can operate normally

  • docker attach

Entering the terminal where the container is executing will 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 exit container

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

Docker stats view service memory status in docker

# docker stats
CONTAINER 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   5

summary

There are still a lot of docker commands. You can try to sort out the list of common commands according to the following figure for easy query. The following pictures are from the network and are only for learning

[docker series] docker learning II, common commands of docker

reference material:

docker docs

Welcome to like, follow and collect

My friends, your support and encouragement are the driving force for me to insist on sharing and improve quality

[docker series] docker learning II, common commands of docker

Well, that’s all for this time

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

I amLittle Devil boy Nezha, welcome to like and pay attention to the collection. See you next time~

This work adoptsCC agreement, reprint must indicate the author and the link to this article

WeChat official account: Little Devil boy Na Zha

Recommended Today

On the mutation mechanism of Clickhouse (with source code analysis)

Recently studied a bit of CH code.I found an interesting word, mutation.The word Google has the meaning of mutation, but more relevant articles translate this as “revision”. The previous article analyzed background_ pool_ Size parameter.This parameter is related to the background asynchronous worker pool merge.The asynchronous merge and mutation work in Clickhouse kernel is completed […]