docker的常用命令
# docker version: docker版本
# docker info docker的系统信息
# docker help 帮助命令(https://docs.docker.com/reference/)
镜像命令
# docker images docker的所有镜像
# docker images -a 所有的镜像
# docker images -q 只显示镜像的id
# docker search mysql 查询mysql镜像及镜像信息
# docker search mysql --filter=stars=4000 查询收藏大于等于4000的mysql镜像信息
root@MiWiFi-R4CM-srv ~]# docker search mysql --filter=stars=4000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11498 [OK]
mariadb MariaDB Server is a high performing open sou… 4363 [OK]
下载命令
#docker pull 镜像名 [tag]
#docker pull mysql
Using default tag: latest #如果不写tag,默认最新版
latest: Pulling from library/mysql
07aded7c29c6: Pull complete #分层下载,docker image的核心,联合文件系统
f68b8cbd22de: Pull complete
30c1754a28c4: Pull complete
1b7cb4d6fe05: Pull complete
79a41dc56b9a: Pull complete
00a75e3842fb: Pull complete
b36a6919c217: Pull complete
635b0b84d686: Pull complete
6d24c7242d02: Pull complete
5be6c5edf16f: Pull complete
cb35eac1242c: Pull complete
a573d4e1c407: Pull complete
Digest: sha256:4fcf5df6c46c80db19675a5c067e737c1bc8b0e78e94e816a778ae2c6577213d #签名
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 指定版本下载
**删除命令**
# docker rmi -f 镜像id 删除指定的镜像
# docker rmi -f 镜像id 镜像id 删除多个镜像
# docker rmi -f $(docker images -aq) 删除全部容器
容器命令
新建容器并启用
#docker pull centos #下载一个容器
#启动并进入容器
# docker run -it centos /bin/bash
#从容器中退回主机
# exit
列出所有运行的容器
#docker ps
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器+带出 历史运行过的容器
-n # 显示最近创建的容器
-q # 只显示容器的编号
退出容器
# exit 直接退出容器,并且容器停止运行
Ctrl + P + 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 run -d 镜像名
# 问题docker ps,发现centos停止了
#常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用 ,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
```shell
[root@MiWiFi-R4CM-srv ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative
(e.g. 42m for 42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative
(e.g. 42m for 42 minutes)
# -t 日志加时间 -f 保留打印窗口,持续打印,--tail 10 显示 最近10条
#docker logs -f -t tail 10 容器id 查看十条日志
查看镜像的元数据
# docker inspect 容器id
进入当前正在运行的容器
# docker exec -it 容器id bashShell 以交互模式进入容器(进入以后会开启一个新的终端,可以在里面操作)
# docker attach 容器id 进入容器正在执行的中断,不会启动新的进程!
从容器内拷贝文件到主机
# docker cp 容器id:容器内路径 目的主机路径
# docker cp 132854541:/home/test.java /home (无论容器是否启动,都可以在目的主机上执行)
小结
attach Attach to a running container #当前she11下 attach连接指定运行镜像
build Build an image from 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 filesystem #查看 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 tarba11 #从tar包中的内容创建一个新的文件系统映像[对应 export]
info Display system-wide information #显示系统相关信息
inspect Return low- level 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包[对应1oad
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 repos tory #给源中镜像打标签
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 #截取容器停止时的退出状态值