一、Docker基本命令
docker version #显示docker版本信息
docker info #显示docker更详细的信息
docker 命令 --help #docker的万能命令
1、镜像命令
①查看镜像信息命令
docker images #查看镜像信息
#可选项
-a #列出所有镜像
-q #只显示镜像ID
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 12 days ago 13.3kB#解释
REPOSITORY 镜像仓库源
TAG 镜像标签
IMAGE ID 镜像ID
CREATED 镜像创建时间
SIZE 镜像大小
②搜索镜像信息命令
docker serach 镜像名称
[root@localhost ~]# docker search mysql
#可选项
-f #搜索收藏大于等于STARS数的镜像
③下载镜像的命令
dokcer pull 镜像名称:[tag版本号]
[root@localhost ~]# docker pull mysql
Using default tag: latest #如果不写tag版本号,默认的是最新一版latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete #分层下载,docker镜像的核心,联合文件
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c #Docker的防伪签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #Docker的真实地址#指定版本下载
这里指定下载的版本一定是Docker Hub里有的
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists #这里显示已经下载了,说明是可以共用的,这样可以为以后节省很多资源
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete #下面下载的就是不同于其他版本的东西
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
④上传镜像
docker push 镜像名称
⑤删除镜像
docker rmi -f ID/名称
选项
-f #删除全部
docker rmi -f 镜像ID #删除指定镜像
docker rmi -f 镜像ID 镜像ID 镜像ID #删除多个镜像
docker rmi -f $(dokcer images -aq) #删除检索出来的所有ID的镜像
⑥镜像标签
docker tag 原仓库名称:标签 新仓库名:标签
2、容器命令
#有了镜像才能创建容器
①创建容器
docker create -it nginx:latest /bin/bash
②运行进行的命令
docker run [可选项] 镜像
#可选项
–name=“Name” #给容器取名字,方便以后区别
-d #后台运行
-it #使用交互方式运行,进入容器查看内容
-p #指定容器的端口
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
-P(大写) #随机指定端口
列子:
下载一个Centos的镜像
[root@localhost ~]# docker pull centos
[root@localhost ~]# docker run -it centos /bin/bash
③查看容器运行状态的命令
docker ps #查看正在运行的容器
#可选项
-a #列出当前正在运行的容器+历史运行过的容器
-n=? #列出最近创建的容器
-q #只显示容器的编号
docker ps -a #查看历史运行记录
④退出容器命令
exit #直接退出
Ctrl +P +Q #退出后容器仍在运行
⑤删除容器
docker rm 容器ID #删除指定的容器,如果容器正在运行,需要rm -f强制删除
docker rm -f $(docker ps -aq) #强制删除所有的容器
docker ps -aq | xargs docker rm #强制删除所有容器
⑥启动和停止容器
docker start 容器ID #启动容器
docker restart 容器ID #重启容器
docker stop 容器ID #停止容器
docker kill 容器ID #杀死容器
容器导入和导出
docker export 容器ID #导出的是一个镜像
cat 镜像存储路径 | docker import - 仓库名:标签名
3、查看命令
①后台启动容器
docker run -d 镜像名 #后台启动容器
#当docker ps查看时,发现容器自动停止了
#常见的坑,docker容器使用后台运行,必须有一个相对应的前台进程,docker发现没有应用就会自动停止
②查看日志
docker logs -tf --tail 容器ID
#可选项
-tf #显示日志 -f是动态显示 ,-t时间戳
–tail number #显示日志条数
③查看进程
docker top 容器ID
④查看镜像的元数据
docker inspect 容器ID
⑤进入Docker的命令
docker exec -it 容器ID 平台路径 #进入容器后会开启一个新窗口
docker attach 容器ID #进入容器后会处于正在执行的终端,不会开启新的窗口
⑥拷贝命令
#有容器内拷贝到主机
docker cp 容器ID:文件路径 目的地
二、Docker数据卷
[root@localhost ~]# docker run -v /opt/aaa:/data1 --name centos01 -it centos /bin/bash
#-v:挂载 /opt/aaa:宿主机的目录 /data1:容器目录 centos01 别名
[root@b53f219508fa /]# cd data1/
#进入容器目录
[root@b53f219508fa data1]# touch bbb.txt
#创建目录
[root@localhost ~]# cd /opt/aaa/
#查看本机共享目录
三、数据卷容器
[root@localhost ~]# docker run --name centos100 -v /data1 -v /data2 -it centos /bin/bash
#centos100:共享容器的别名 -v /data1/data2:共享目录
[root@localhost ~]# docker run -it --volumes-from centos100 --name centos200 centos /bin/bash
#–volumes-from:数据卷容器命令 centos100:共享容器的别名 centos200本容器的别名
四、暴露端口
[root@localhost ~]# docker search nginx #搜索nginx的镜像
[root@localhost ~]# docker pull nginx #下载nginx镜像
[root@localhost ~]# docker run -d -p 4455:80 --name nginx01 nginx #启动nginx容器
-d:后台运行 -p:暴露端口号 主机端口:容器端口 --name 起名
root@localhost ~]# curl localhost:4455 #访问nginx
#也可以在本机进行访问
七、总结
1、镜像命令
上传镜像:docker push 镜像名
下载镜像:docker pull 镜像名
打标签:docker tag 原仓库名称:标签 新仓库名:标签
删除镜像:docker rmi 镜像ID
导出镜像:docker save 镜像ID -o /导入路径
导入镜像:docker load -i /镜像存储路径
2、容器命令
创建容器:docker create 镜像名
进入容器:docker exec -itd 镜像名 控制台
创建并允许容器:docker run -itd 镜像名 控制台
删除容器:docker rm 容器名
导出容器:docker export 容器名
导入容器:cat 镜像存储路径 | docker import - 仓库名:标签名
3、仓库命令
暴露端口:docker run -itd -p 主机端口:容器端口 --name 别名 /bin/bash