濤。

t.

Docker 常用命令

docker 常用命令

普通命令

[root@localhost docker]# docker version        # 显示docker的版本信息
[root@localhost docker]# docker info            # 显示docker的系统信息,包括镜像和容器的数量
[root@localhost docker]# docker 命令 --help     # 帮助命令
[root@localhost docker]# systemctl start docker # 启动 docker
[root@localhost docker]# systemctl stop docker  # 关闭 docker
[root@localhost docker]# systemctl status docker # 查看 docker 的状态
[root@localhost docker]# systemctl restart docker # 重启 docker

 

 

镜像命令

docker images #查看所有本地主机上的镜像

[root@localhost docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   6 months ago   13.3kB
​
#解释
REPOSITORY      # 镜像的仓库源
TAG             # 镜像的标签
IMAGE ID        # 镜像的id
CREATED         # 镜像的创建时间
SIZE            # 镜像的大小
​
#常用可选项
-a, --all       # 列出所有镜像
-q, --quiet     # 只显示镜像的id

docker search 搜索镜像

[root@localhost docker]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11444     [OK]   
mariadb                           MariaDB Server is a high performing open sou…   4343      [OK]  
​
#常用可选项,过滤
--filter=STARS=5000     # 搜索出来的镜像STARS大于5000

docker pull 下载镜像

#下载镜像   docker pull 镜像名[:tag]
​
[root@localhost docker]# docker pull mysql
Using default tag: latest           # 如果不写tag,默认选择latest最新版本
latest: Pulling from library/mysql
a330b6cecb98: Pull complete         # 分层下载,docker image的核心,联合文件系统
9c8f656c32b8: Pull complete
88e473c3f553: Pull complete
062463ea5d2f: Pull complete
daf7e3bdf4b6: Pull complete
1839c0b7aac9: Pull complete
cf0a0cfee6d0: Pull complete
1b42041bb11e: Pull complete
10459d86c7e6: Pull complete
b7199599d5f9: Pull complete
1d6f51e17d45: Pull complete
50e0789bacad: Pull complete
Digest: sha256:99e0989e7e3797cfbdb8d51a19d32c8d286dd8862794d01a547651a896bcf00c     # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest      # 真实地址
​
#等价于
docker pull mysql
docker pull docker.io/library/mysql:latest
​
#指定版本下载
[root@localhost docker]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a330b6cecb98: Already exists
9c8f656c32b8: Already exists
88e473c3f553: Already exists
062463ea5d2f: Already exists
daf7e3bdf4b6: Already exists
1839c0b7aac9: Already exists
cf0a0cfee6d0: Already exists
fae7a809788c: Pull complete
dae5a82a61f0: Pull complete
7063da9569eb: Pull complete
51a9a9b4ef36: Pull complete
Digest: sha256:d9b934cdf6826629f8d02ea01f28b2c4ddb1ae27c32664b14867324b3e5e1291
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi 删除镜像

[root@localhost docker]# docker rmi -f 镜像id              #删除指定的镜像
[root@localhost docker]# docker rmi -f 镜像id 镜像id 镜像id       #删除多个镜像
[root@localhost docker]# docker rmi -f $(docker images -qa)     #删除全部镜像

 

 

 

容器命令

# 说明:有了镜像之后才能创建容器(例如下载一个centos镜像)
[root@localhost docker]# docker pull centos

新建容器并启动

[root@localhost docker]# docker run [可选参数] image
# 参数说明
--name="Name"   # 容器名字  centos1  centos2  用来区分容器
-d              # 后台方式运行
-it             # 使用交互方式运行,进入容器查看内容
-p              # (小写英文p)指定容器的端口 -p 8080:8080
-P              # (大写英文P)ip:主机端口:容器端口
                    主机端口:容器端口(常用)
                    容器端口
                    随机指定端口
--restart=always # 使容器自动启动
--privileged=true # 开启特权模式(如遇到权限问题错误,可加此参数)
-v # 挂载宿主机目录和 docker容器中的目录,前面是宿主机目录,后面是容器内部目录

退出容器

exit            # 退出容器并停止运行
ctrl + P + Q    # 退出容器不停止运行

列出所有的运行的容器

[root@localhost docker]# docker ps      # 列出当前正在运行的容器
# 参数说明
-a      # 列出当前正在运行的容器 + 历史运行过的容器
-n=?    # 显示最近创建的容器
-q      # 只显示容器的编号

删除容器

docker rm 容器id          # 删除指定容器,不能删除正在运行的容器
docker rm -f $(docker ps -aq)       # 强制删除所有容器
docker ps -a -q|xargs docker rm     # 删除所有容器

启动和停止容器的操作

docker start 容器id       # 启动容器
docker restart 容器id     # 重启容器
docker stop 容器id       # 停止容器
docker kill 容器id        # 强制停止容器

 容器与主机之间的数据拷贝

docker cp      # 用于容器与主机之间的数据拷贝

 

posted on 2021-09-24 00:23  濤。  阅读(48)  评论(0编辑  收藏  举报

导航