头图

[Docker series] docker learning two, Docker's common commands

Basic help commands

# 查看 docker 的基本版本信息
docker version

# 查看 docker 的系统信息,如镜像和容器数量
docker info

# 查看某个命令的帮助
docker xx命令 --help

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

Mirror command

docker images view image

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

View the mirror 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
Keywordexplain
REPOSITORYWarehouse source
TAGMirror label
IMAGE IDMirror ID
CREATEDCreation time
SIZEMirror size

Optional parameters:

Options:
  -a, --all             显示所有的镜像
  -q, --quiet           只显示镜像ID

docker search search mirror

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 mirrors directly on the page on dockerhub

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

docker pull download image

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

Download redis mirror as an example

# docker pull redis
Using default tag: latest            # 默认拉最新版本
latest: Pulling from library/redis    # redis 的库
33847f680f63: Pull complete            # 分层下载
26a746039521: Pull complete
18d87da94363: Pull complete
5e118a708802: Pull complete
ecf0dbe7c357: Pull complete
46f280ba52da: Pull complete
Digest: sha256:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59                                    # 签名
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest        # redis 真实下载地址

So 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

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 downloading version 6 of redis, there is no hierarchical download, indicating that the hierarchical downloads seen above are shared

View the mirror installed 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 mirror

  • Delete a single image
docker rmi -f 容器ID
  • Delete multiple mirrors
docker rmi -f 容器ID 容器ID 容器ID 
  • Delete all mirrors
docker rmi -f $(docker images -q)

Container command

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

docker pull ubuntu

docker run creates and starts the container

docker run [parameter] image name [command] [parameter]

Common parameter description:

--name="xxx"    # 运行容器的名字
-d                # 后台方式运行
-it                # 交互的方式运行
-p                # 指定容器的端口号 例如 -p 6379:6380
                常用的操作有 -p 主机端口:容器端口
-P                # 随机指定端口号

Start ubuntu in the container, through the host name, we can already see that the host has switched

root@iZuf66y3tuzn4wp3h02t7pZ:~# docker run -it ubuntu /bin/bash
root@87fb04e2a6e7:/#

Exit the container

  • Type the exit , the container will exit
  • Use the shortcut key Ctrl + P + Q to return to the host, the container will not exit

docker ps view 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

Optional parameters:

            # 查看正在运行的容器
-a             # 查看运行过的容器
-n=xx        # 查看运行过的前 xx 个容器
-s            # 查看容器运行的 大小
-q            # 查看容器 ID

docker rm delete container

docker rm 容器ID        # 删除未运行的容器
docker rm -f 容器ID    # 强制删除正在运行的容器
docker rm -f $(docker ps -aq)        # 删除所有容器
docker ps -aq | xargs docker rm     # 删除所有容器

start, restart, stop, kill start, restart, stop, force stop the container

docker start 容器ID
docker restart 容器ID
docker stop 容器ID
docker kill 容器ID

Commonly used other commands

docker run -d starts the container in the background

# 后台启动一个 ubuntu
docker run -d ubuntu

# 查看 运行的容器
docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES

Found that no containers are running

The reasons are as follows:

  • To start the service in the background of docker, there needs to be a foreground process, otherwise docker will stop the service if it finds that there is no application

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

# 临时加上一个前台进程
docker run -d ubuntu /bin/bash -c "while true;do echo xiaozhupeiqi;sleep 2;done"

# 查看正在运行的容器
docker ps
CONTAINER ID   IMAGE     COMMAND
10ba0e687434   ubuntu    "/bin/bash -c 'while…" 

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

docker logs view logs

docker logs [parameter] container ID

Options:
  -f        # 和输出保持一致
  -n        # 输出最近的几行
  -t        # 打印时间戳

View the logs 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 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 lot of information is omitted from the output

[
     {
        "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
root@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 -ef

docker attach enters the program being executed in the container

docker attach container ID

docker attach 10ba0e687434

exec and docker attach 1618280d5745e9

  • docker exec

After entering the container, a new terminal will be opened, which can be operated normally

  • docker attach

Enter the terminal where the container is executing, and 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

# 进入容器,在容器的/home 目录下 建一个文件 xiaomotong.go
 docker exec -it 10ba0e687434 /bin/bash
 cd /home/
 touch xiaomotong.go
# Ctrl + P + Q 退出容器

# 将容器内文件拷贝到主机内
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

Summarize

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

Reference materials:

docker docs

Welcome to like, follow, favorite

Friends, your support and encouragement are my motivation to keep sharing and improve quality

Okay, that's it for this time

Technology is open, and our mindset should be more open. Embrace the change, live toward the sun, and work hard to move forward.

I’m little magic boy , welcome to like and follow the collection, see you next time~


阿兵云原生
192 声望37 粉丝