Docker 基础
帮助命令
docker vrsion #显示Docker客户端和服务端的版本信息
docker info #显示Docker的系统信息包括镜像和容器的数量
docker 命令 --help #帮助命令
镜像命令
docker images 查看所有本地的主机上的镜像
root@iZ2vc88pq80pz66p2jw51hZ:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 2 months ago 13.3kB
#解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a, --all #列出所有镜像
-q, --quiet #只显示镜像的id
docker search 搜索镜像
root@iZ2vc88pq80pz66p2jw51hZ:~# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14321 [OK]
mariadb MariaDB Server is a high performing open sou… 5467 [OK]
#可选项,通过收藏数量来过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
-f=STARS=3000
root@iZ2vc88pq80pz66p2jw51hZ:~# docker search mysql -f=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14321 [OK]
mariadb MariaDB Server is a high performing open sou… 5467 [OK]
root@iZ2vc88pq80pz66p2jw51hZ:~# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14321 [OK]
mariadb MariaDB Server is a high performing open sou… 5467 [OK]
docker pull 下载镜像
#下载镜像 docker pull 镜像名[:tag]
root@iZ2vc88pq80pz66p2jw51hZ:~# docker pull mysql
Using default tag: latest #如果不写 tag,默认就是latest最新标签
latest: Pulling from library/mysql
e2c03c89dcad: Pull complete #分层下载,docker image的核心 联合文件系统
68eb43837bf8: Pull complete
796892ddf5ac: Pull complete
6bca45eb31e1: Pull complete
ebb53bc0dcca: Pull complete
2e2c6bdc7a40: Pull complete
6f27b5c76970: Pull complete
438533a24810: Pull complete
e5bdf19985e0: Pull complete
667fa148337b: Pull complete
5baa702110e4: Pull complete
Digest: sha256:232936eb036d444045da2b87a90d48241c60b68b376caf509051cb6cffea6fdc #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
#docker pull mysql 等价于 docker pull docker.io/library/mysql:latest(真实地址)
#指定版本下载
root@iZ2vc88pq80pz66p2jw51hZ:~# docker pull mysql:5.7
5.7: Pulling from library/mysql
70e9ff4420fb: Pull complete
7ca4383b183f: Pull complete
3e282e7651b1: Pull complete
1ffa0e0ca707: Pull complete
6eb790cf6382: Pull complete
2b7ffc37d8e9: Pull complete
4393c12228b9: Pull complete
389d2c130d52: Pull complete
e5df3caef94c: Pull complete
5c6aa409290d: Pull complete
faa350980ea9: Pull complete
Digest: sha256:bd873931ef20f30a5a9bf71498ce4e02c88cf48b2e8b782c337076d814deebde
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像!
#docker rmi -f 镜像id #删除指定的镜像
#docker rmi -f 镜像id 镜像id ... #删除多个镜像
#docker rmi -f $(docker images -aq)#删除全部的镜像
#docker rmi 镜像名 #默认删除latest最新标签 eg:docker rmi mysql
#docker rmi 镜像名:tag #删除指定版本的镜像 eg:docker rmi mysql:5.7
容器命令
说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数说明
--name="Name" 容器名字 tomcat01 tomcat02...用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-P大写 指定容器的端口 —p 8080:8080
-P ip:主机端口:容器端口
-P 主机端口:容器端口 #主机端口映射到容器端口(常用)
-P 容器端口
容器端口
-p小写 随机指定端口
#测试
root@iZ2vc88pq80pz66p2jw51hZ:~# docker run -it centos /bin/bash
[root@886d334c115a /]# ls #查看容器内的centos,基础版本,很多命令都是不完善的!
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
#从容器中退回主机
[root@886d334c115a /]# exit
exit
root@iZ2vc88pq80pz66p2jw51hZ:~# ls
3000 snap
列出所有的运行的容器
#docker ps 命令
空 #列出当前正在运行的容器
-a #列出当前正在运行的容器+带出历史运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器的编号
root@iZ2vc88pq80pz66p2jw51hZ:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@iZ2vc88pq80pz66p2jw51hZ:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
886d334c115a centos "/bin/bash" 10 minutes ago Exited (0) 6 minutes ago stupefied_curie
1ecae675ada3 centos "/bin/bash" 13 minutes ago Exited (127) 11 minutes ago confident_solomon
d1da630cf372 centos "/bin/bash" 14 minutes ago Exited (0) 14 minutes ago strange_mendeleev
9b66b567263f hello-world "/hello" 3 hours ago Exited (0) 3 hours ago admiring_swanson
46a896fbb132 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago eager_kalam
root@iZ2vc88pq80pz66p2jw51hZ:~# docker ps -an=2//列出最近两条内容
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
886d334c115a centos "/bin/bash" 12 minutes ago Exited (0) 8 minutes ago stupefied_curie
1ecae675ada3 centos "/bin/bash" 15 minutes ago Exited (127) 13 minutes ago confident_solomon
root@iZ2vc88pq80pz66p2jw51hZ:~# docker ps -a -n=2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
886d334c115a centos "/bin/bash" 12 minutes ago Exited (0) 8 minutes ago stupefied_curie
1ecae675ada3 centos "/bin/bash" 15 minutes ago Exited (127) 13 minutes ago confident_solomon
root@iZ2vc88pq80pz66p2jw51hZ:~# docker ps -aq
886d334c115a
1ecae675ada3
d1da630cf372
9b66b567263f
46a896fbb132
退出容器
exit #直接停止并退出
ctrl+p+q#容器不停止退出
删除容器
docker rm 容器id #删除指定的容器(该容器没有运行)强制删除rm -f
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 run -d 镜像名
root@iZ2vc88pq80pz66p2jw51hZ:~# docker run -d centos
25a555265107a8830f7b786312eb0865195242e0bfef411ead2333c032655cd2
#问题docker ps,发现centos 停止了
root@iZ2vc88pq80pz66p2jw51hZ:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止。
查看日志
docker logs -tf --tail 10 #查看最近10条日志
--tail #要显示日志的条数
-t #显示日志时间戳
-f #实时跟踪日志输出
-n #从日志末尾显示指定行数的日志,默认为 “all”(显示所有日志)
--since:指定从何时开始显示日志。可以接受一个时间戳(例如“2013-01-02T13:23:37Z”)或相对时间(例如“42m”表示过去42分钟)。
查看容器中进程信息 ps
#命令
docker top 容器id
查看镜像的元数据
#docker inspect id
进入当前正在运行的容器
#通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
#命令
#docker exec -it 容器id bashShell
#测试
root@r7ftf-virtual-machine:/home/r7ftf# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
289103fecada centos "/bin/bash" 16 minutes ago Up 16 minutes affectionate_pare
root@r7ftf-virtual-machine:/home/r7ftf# docker exec -it 289103fecada /bin/bash
[root@289103fecada /]# ps
PID TTY TIME CMD
15 pts/1 00:00:00 bash
29 pts/1 00:00:00 ps
[root@289103fecada /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:21 pts/0 00:00:00 /bin/bash
root 15 0 0 03:38 pts/1 00:00:00 /bin/bash
root 30 15 0 03:38 pts/1 00:00:00 ps -ef
#方式二
#docker attach 容器id
root@r7ftf-virtual-machine:/home/r7ftf# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
289103fecada centos "/bin/bash" 21 minutes ago Up 21 minutes affectionate_pare
root@r7ftf-virtual-machine:/home/r7ftf# docker attach 289103fecada
[root@289103fecada /]# 正在执行当前的代码......
#docker exec #进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach #进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径
docker cp 目的主机路径 容器id:容器内路径 #从主机上拷贝文件到容器
浙公网安备 33010602011771号