文章目录
Docker的常用命令
帮助命令
docker version #显示docker的版本信息
docker info #显示docker的系统信息,包括境像和容器的数量
docker 命令 --help# 帮助命令
帮助文档的地址:https://docs.docker.com/engine/reference/commandline
镜像命令
docker images 查看所有本地的主机上的镜像
[root@xxx /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
he11o-world Tatest bf756fblae65 4 months ago 13.3kB
解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a,--all 列出所有镜像
-q,--quiet 只显示ID
docker search 搜索镜像
[root@xxx /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14338 [OK]
mariadb MariaDB Server is a high performing open sou… 5474 [OK]
#可选项,通过搜藏来过滤
--filter=STARS=3000# 搜索出来的镜像就是STARS大于3000的
[root@xxx ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14338 [OK]
mariadb MariaDB Server is a high performing open sou… 5474 [OK]
拉取镜像docker pull 镜像名[:tag]
[root@163 ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认最新版本
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #分层下载,docker image的核心 联合文件系统
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
*docker pull mysql等价于docker pull docker.io/library/mysql:latest
*docker pull mysql:5.7
[root@163 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists //以上为重复部分
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
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 镜像ID //删除多个镜像
docker rmi -f $(docker images -aq) //删除镜像z
容器命令
说明:我们有了镜像才可以创建容器,linux,下载一个 centos 镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口-p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
容器端口
-P 随机指定端口
#测试,启动并进入容器
[root@xxx ~]# docker run -it centos /bin/bash
[root@c0eed8b69c30 /]# ls //查看容器内的centos,基础版本,很多命令都不完善
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
exit //从容器中退回主机
列出所有运行的容器
#docker ps 命令
列出当前正在运行的容器
-a //列出当前正在运行的容器+带出历史运行过的容器
-n=? // 显示最近创建的容器
-q //只显示容器的编号
退出容器
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@xxx /]# docker run -d centos
#问题docker ps,发现 centos 停止了
#常见的坑:docker 容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs -f -t --tail 容器,没有日志
#自己编写一段shell脚本
[root@xxx /]# docker run -d centos /bin/sh -c "while true;do echo hhhhhh;sleep 1;done"
[root@xxx /]# docker ps
CONTAINER ID IMAGE
dce7b86171bf centos
#显示日志
-tf //显示日志
--tai1 number //要显示日志条数
[root@xxx /]# docker logs -tf --tai1 10 dce7b86171bf
查看容器中进程信息ps
#命令docker top 容器id
UID PID PPID C STIME
root 1432624 1432594 0 20:14
root 1452486 1432624 0 20:20
查看镜像的元数据
#命令 docker inspect 容器id
进入当前正在运行的容器
方式一:docker exec -it 容器id /bin/bash //进入容器后开启一个新的终端,可以在里面操作
方式二:docker attach 容器id //进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的的主机路径
#查看当将主机目录下
[root@163 home]# ls
hhh.java redis www
[root@163 home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69687a39e545 centos "/bin/bash" 2 minutes ago Up 2 minutes vigilant_blackburn
#进入docker容器内部
[root@163 home]# docker attach 69687a39e545
[root@69687a39e545 /]# cd /home
[root@69687a39e545 home]# ls
#在容器内新建一个文件
[root@69687a39e545 home]# touch test.java
[root@69687a39e545 home]# exit
exit
[root@163 home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@163 home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69687a39e545 centos "/bin/bash" 4 minutes ago Exited (0) 31 seconds ago vigilant_blackburn
#将文件拷贝出来到主机上
[root@163 home]# docker cp 69687a39e545:/home/test.java /home
Successfully copied 1.54kB to /home
[root@163 home]# ls
hhh.java redis test.java www
#拷贝是一个手动过程,使用-v卷的技术可以实现
小结
attach | Attach to a running container | 当前 she11 下 attach 连接指定运行镜像 |
---|---|---|
build | Build an imagerfrom a Dockerfile | 通过 Dockerfile 定制镜像 |
commit | Create a new image from a container changes | 提交当前容器为新的镜像 |
cp | Copy files/folders from the containers filesystem to the host path | 从容器中搏贝指定文件或者目录到宿主机中 |
create | Create a new container | 创建一个新的容器,同 run,但不启动容器 |
diff | Inspect changes on a container’s fiesystem | 查看 docker 容器变化 |
events | Get real time events from the server | 从 docker 服务获取容器实时事件 |
exec | Run a command in an existing container | 在已存在的容器上运行命令 |
export | Stream the contents of a container as a tar archive | 导出容器的内容作为一个 tar 归档文件[对应import] |
history | Show the history of an image | 展示一个镜像形成历史 |
images | List images | 列出系统当前镜像 |
import | Create a new filesystem image from the contents of a tarball | 从tar包中的内容创建一个新的文件系统映像[对应export] |
info | Display system-wide information | 显示系统相关信息 |
inspect | Return low-Tevel information on a container | 查看容器详细信息 |
kill | Kill a running container | kill 指定 docker 容器 |
load | Load an image from a tar archive | 从一个 tar 包中加载一个像[对应 save] |
login | Register or Login to the docker registry server | 注册或者陆一个 docker 源服务器 |
logout | Log out from a Docker registry server | 从当前 Docker registry 退出 |
logs | Fetch the logs of a container | 输出当前容器日志信息 |
port | Lookup the public-facing port which is NAT-ed to PRIVATE_PORT | 查看映射端口对应的容器内部源端口 |
pause | Pause all processes within a container | 暂停容器 |
ps | List containers | 列出容器列表 |
pull | Pull an image or a repository from the docker registry server | 从docker镜像源服务器拉取指定镜像或者库镜像 |
push | Push an image or a repository to the docker registry server | 推送指定镜像或者库镜像至docker源服务器 |
restart | Restart a running container | 重启运行的容器 |
rm | Remove one or more containers | 移除一个或者多个容器 |
rmi | Remove one or more images | 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除] |
run | Run a command in a new container | 创建一个新的容器并运行一个命令 |
save | Save an image to a tar archive | 保存一个镜像为一个 tar 包[对应 load] |
search | Search for an image on the Docker Hub | 在 docker hub 中搜索镜像 |
start | Start a stopped containers | 启动容器 |
stop | Stop a running containers | 停止容器 |
tag | Tag an image into a repository | 给源中镜像打标签 |
top | Lookup the running processes of a container | 查看容器中运行的进程信息 |
unpause | Unpause a paused container | 取消暂停容器 |
version | Show the docker version information | 查看 docker 版本号 |
wait | Block until a container stops,then print its exit code | 截取容器止时的退出状态值 |