Docker基础命令

镜像

[root@CentOS ~]# docker search --help

Usage:	docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output

1、搜索image镜像

[root@CentOS7 ~]# docker search mysql --limit 3
NAME                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                MySQL is a widely used, open-source relation…   10075               [OK]
mysql/mysql-server   Optimized MySQL Server Docker images. Create…   736                                     [OK]
bitnami/mysql        Bitnami MySQL Docker Image                      45                                      [OK]

2、使用–format指定输出格式

[root@CentOS7 ~]# docker search --format "{{.Name}}\t{{.StarCount}}\t{{.Description}}" mysql --limit 3
mysql   10075   MySQL is a widely used, open-source relation…
mysql/mysql-server      736     Optimized MySQL Server Docker images. Create…
bitnami/mysql   45      Bitnami MySQL Docker Image

3、过滤搜索start大于等于1000

[root@CentOS ~]# docker search --format "{{.Name}}\t{{.StarCount}}\t{{.Description}}" --filter stars=10072  mysql
mysql	10072	MySQL is a widely used, open-source relation…

4、下载镜像

[root@CentOS ~]# docker pull --help

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

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform capable
  -q, --quiet                   Suppress verbose output

下载指定的镜像mysql:5.7

[root@CentOS7 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
bb79b6b2107f: Pull complete
49e22f6fb9f7: Pull complete
842b1255668c: Pull complete
9f48d1f43000: Pull complete
c693f0615bce: Pull complete
8a621b9dbed2: Pull complete
0807d32aef13: Pull complete
6d2fc69dfa35: Pull complete
56153548dd2c: Pull complete
3bb6ba940303: Pull complete
3e1888da91a7: Pull complete
Digest: sha256:b3dc8d10307ab7b9ca1a7981b1601a67e176408be618fc4216d137be37dae10b
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

用户可以访问hub.docker.com网站搜索需要下载的镜像,找到对应的版本,进行相关下载。
在这里插入图片描述

如果用户直接使用docker pull mysql则系统默认会下载最新版本的mysql的docker镜像文件

5、下载镜像,禁止输出下载过程。

[root@CentOS7 ~]# docker pull --quiet mysql
docker.io/library/mysql:latest

6、查看镜像

[root@CentOS ~]# docker images --help
Usage:	docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs
[root@CentOS ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 42cdba9f1b08        5 days ago          448MB
mysql               latest              8e85dd5c3255        5 days ago          544MB

7、过滤image镜像

[root@CentOS7 ~]# docker images --filter=reference=m*:5*
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 42cdba9f1b08        5 days ago          448MB

8、仅仅查看Image ID

[root@CentOS ~]# docker images -q
42cdba9f1b08
8e85dd5c3255
0d120b6ccaa8
bf756fb1ae65

9、删除镜像

[root@CentOS ~]# docker rmi --help

Usage:	docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents
[root@CentOS ~]# docker rmi mysql:latest
Untagged: mysql:latest
Untagged: mysql@sha256:86b7c83e24c824163927db1016d5ab153a9a04358951be8b236171286e3289a4
Deleted: sha256:8e85dd5c32558ea5c22cc4786cff512c1940270a50e7dbc21ad2df42f0637de4
Deleted: sha256:d34d6911b98a793cb85471491a0e7050b648fb6352ce0514da14dfee3e278437
Deleted: sha256:a8fb61bcfe3163415d7b912fa63adcd80faaa3c635dc03b1a78d63e61b6c9976
Deleted: sha256:f90e56318179ee9771109d224a84dc5acb6b659706219ba6dfefc8c955d9918b
Deleted: sha256:ca8b8d4e1e06d73cf2d30e23d7d0f0eeb532e2ed5f0d0e8b571e2195127c7733
Deleted: sha256:f1ca5278cba221a11962bb664de4128301d03d2c00ac9414fc8cf63f1ac26013

或者

[root@CentOS ~]# docker rmi 42cdba9f1b08
Untagged: mysql:5.7
Untagged: mysql@sha256:b3dc8d10307ab7b9ca1a7981b1601a67e176408be618fc4216d137be37dae10b
Deleted: sha256:42cdba9f1b0840cd63254898edeaf6def81a503a6a53d57301c3b38e69cd8f15
Deleted: sha256:d163436e5e1e8180feaf30af2ecbfd3b6a2e86a66b805dcd1e6e0e9ba26be92f
Deleted: sha256:eae434fad508aeec80daf1a0fc16ba89f65707f76a31db7f63883bebf208b743
Deleted: sha256:78b3fc624268ff3cf2ff6558e8c9d47babda61033966f4443e2430dd2607163b
Deleted: sha256:10a3c92754ac4860a385e2095dbe35a1486b4aa2109421a58f3c91f3ce6dbfce
Deleted: sha256:33134afe9e842a2898e36966f222fcddcdb2ab42e65dcdc581a4b38b2852c2e0
Deleted: sha256:dd053ec71039c3646324372061452778609b9ba42d1501f6a72c0272c94f8965
Deleted: sha256:2d4c647f875f6256f210459e5c401aad27ad223d0fa3bada7c4088a6040f8ba4
Deleted: sha256:4bded7e9aa769cb0560e60da97421e2314fa896c719179b926318640324d7932
Deleted: sha256:5fd9447ef700dfe5975c4cba51d3c9f2e757b34782fe145c1d7a12dfbee1da2f
Deleted: sha256:5ee7cbb203f3a7e90fe67330905c28e394e595ea2f4aa23b5d3a06534a960553
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c

容器

1、创建容器,但是不运行

[root@CentOS7 ~]# docker create --hostname mysql --name mysql01 -e MYSQL_ROOT_PASSWORD=root mysql:5.7
eb2ec0c1fdf0e201c41b26012a59ac8d014f8777342a154081f3e12b9bbaa14c
2、启动当前容器
```shell
[root@CentOS7 ~]# docker start eb2ec0c1fdf0
eb2ec0c1fdf0
[root@CentOS7 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                 NAMES
eb2ec0c1fdf0        mysql:5.7           "docker-entrypoint.s…"   About a minute ago   Up 4 seconds        3306/tcp, 33060/tcp   mysql01
[root@CentOS7 ~]#

3、创建并运行MySQL容器

[root@CentOS7 ~]# docker run --hostname mysql --name mysql02 -d  -e MYSQL_ROOT_PASSWORD=root mysql:5.7
a870a6bf2e45bb282c186fedc406d21f96440be7b53f39791d1a6be1b5daa7db

这里的-i 表示interactive -t表示分配一个虚拟的终端,然后在虚拟的终端上执行/bin/bash,用户可以使用Ctrl+q+p退出虚拟终端。

4、进入当前容器

[root@CentOS7 ~]# docker exec -i -t a870a6bf2e45 /bin/bash
root@mysql:/#
root@mysql:/# mysql -u root -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

5、查看目前正在运行的容器

[root@CentOS ~]# docker ps --help

Usage:	docker ps [OPTIONS]

List containers

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display numeric IDs
  -s, --size            Display total file sizes
[root@CentOS ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
a870a6bf2e45        mysql:5.7           "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        3306/tcp, 33060/tcp   mysql02
eb2ec0c1fdf0        mysql:5.7           "docker-entrypoint.s…"   7 minutes ago       Up 6 minutes        3306/tcp, 33060/tcp   mysql01
[root@CentOS ~]# docker ps --format "{{.ID}}\t{{.Image}}\t{{.Command}}"
a870a6bf2e45    mysql:5.7       "docker-entrypoint.s…"
eb2ec0c1fdf0    mysql:5.7       "docker-entrypoint.s…"

6、再次进入到该容器,打开新的回话

[root@CentOS ~]# docker exec -it eb2ec0c1fdf0 /bin/bash
[root@mysql /]# 
或者
[root@CentOS7 ~]# docker exec -i -t mysql01 /bin/bash
[root@mysql:/#

7、再次进入到该容器,使用上一次回话

[root@CentOS ~]#  docker ps --format "{{.ID}}\t{{.Image}}\t{{.Command}}"
eb2ec0c1fdf0   mysql:5.7   "/bin/bash"
[root@CentOS ~]#  docker attach eb2ec0c1fdf0 
[root@mysql/]# 

8、暂停容器

[root@CentOS ~]# docker pause f1aaa3122cef
f1aaa3122cef

9、恢复容器

[root@CentOS ~]# docker unpause f1aaa3122cef
f1aaa3122cef

10、停止容器

[root@CentOS ~]# docker stop f1aaa3122cef
f1aaa3122cef

11、启动容器

[root@CentOS ~]# docker start f1aaa3122cef
f1aaa3122cef

12、强制终止容器

[root@CentOS ~]# docker kill f1aaa3122cef
f1aaa3122cef

13、重启容器实例

[root@CentOS ~]# docker restart f1aaa3122cef
f1aaa3122cef

14、删除所有容器实例

[root@CentOS ~]# docker ps -aq|xargs docker rm -f
f1aaa3122cef
4dcf88274b48
7ed67bd4a7a2
2c6ec17e6661
fadd99766e4a
4901fe787252
65b3f17741ae

15、查看容器实例

[root@CentOS ~]# docker inspect 2168cc600a48

其他指令

1、文件拷贝

[root@CentOS ~]# docker cp HbaseCoProcessor-1.0-SNAPSHOT.jar 2168cc600a48:/root
[root@CentOS ~]# docker exec -it 2168cc600a48 /bin/bash
[root@2168cc600a48 /]# ls /root
HbaseCoProcessor-1.0-SNAPSHOT.jar  anaconda-ks.cfg  anaconda-post.log  original-ks.cfg

2、查看容器占用

[root@CentOS ~]# docker stats --no-stream
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
da086537529c        funny_galileo       0.00%               536KiB / 3.682GiB   0.01%               656B / 0B           0B / 0B             1

3、查看某个容器内部占用

[root@CentOS ~]# docker top da086537529c
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                14926               14910               0                   07:23               pts/0               00:00:00            /bin/bash

4、修改容器的名字

[root@CentOS ~]# docker rename da086537529c centos01
[root@CentOS ~]# docker ps --format "{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}"
da086537529c	centos01	centos	Up 17 minutes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值