docker 基本命令

帮助命令

docker version   #显示docker的版本信息
docker info        #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help   #万能命令

镜像命令

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

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB

解释
REPOSITORY  镜像的仓库源
TAG      镜像的标签
IMAGE ID   镜像的id
CREATED    镜像的创建时间
SIZE      镜像的大小

-a, --all    #列出所有镜像
-q, --quiet #只显示镜像的id

docker search  搜索镜像

mysql                             MySQL is a widely used, open-source relation…   9746                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3558                [OK] 

#可选项
--filter=STARS=3000  #过滤stars等于3000的

docker pull  下载镜像

#下载镜像 docker  pull  镜像名【:tag】

[root@localhost ~]# docker pull mysql:5.7  #如果不写tag, 默认就是latest
5.7: Pulling from library/mysql
8559a31e96f4: Already exists   #分层下载 docker image的核心 联合文件系统
d51ce1c2e575: Already exists
c2344adc4858: Already exists
fcf3ceff18fc: Already exists
16da0c38dc5b: Already exists
b905d1797e97: Already exists
4b50d1c6b05c: Already exists
0a52a5c57cd9: Pull complete
3b816a39d367: Pull complete
13ee22d6b3bb: Pull complete
e517c3d2ba35: Pull complete
Digest: sha256:ea560da3b6f2f3ad79fd76652cb9031407c5112246a6fb5724ea895e95d74032    #签名
Status: Downloaded newer image for mysql:5.7   #真实地址
docker.io/library/mysql:5.7

#等价于

docker  pull  mysql:5.7

docker  pull  docker.io/library/mysql:5.7

docker  rmi  删除镜像

docker  rmi  -f  镜像id    #删除指定的镜像
docker  rmi  -f  镜像id  镜像id  镜像id    #删除多个镜像
docker  rmi  -f  $(docker images -aq)     #删除所有镜像

容器命令

 新建容器并启动

docker  run  【可选参数】 image
#参数说明
--name="Name"  容器名字,用来区分容器
-d         后台方式运行
-it        使用交互方式运行
-p         指定容器的端口
  -p ip:主机端口:容器端口
  -p 主机端口:容器端口
  -p 容器端口
  容器端口
-P         (大写)随机指定端口

#启动并进入容器

[root@localhost ~]# docker run -it centos
[root@a890870c09fe /]# ls    #查看容器内的centos,基础版本,很多命令不完善
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr

#从容器中退回主机

[root@a890870c09fe /]# exit
exit
[root@localhost ~]#

docker  ps   列出所有运行中的容器

-a  #列出当前正在运行的容器+历史运行过的容器
-n=数字  #显示最近创建容器列表数==数字
-q  #只显示4容器的编号
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a890870c09fe centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago priceless_lederberg 1f68ca1ba541 hello-world "/hello" 28 hours ago Exited (0) 28 hours ago dazzling_liskov 67176f01ed48 hello-world "/hello" 10 days ago Exited (0) 10 days ago thirsty_thompson

退出容器

exit        #直接容器停止并退出
Ctrl + p + q     #容器不停止退出

删除容器

docker  rm   容器id        #删除容器  不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq)  #删除所有容器

启动和停止容器的操作

docker  start 容器id    #启动容器
docker restart 容器id    #重启容器
docker stop 容器id    #停止容器
docker kill 容器id    #强制停止容器
原文地址:https://www.cnblogs.com/pengranxindong/p/13341226.html