docker命令
目录
启动docker:systemctl start docker
查看本地所有镜像docker images
搜索Docker官方仓库的镜像
docker search [--limit=int] KEYWORD
删除镜像
docker rmi NAME:TAG [-force|-f]
docker rmi ID [-force|-f]
清理镜像(临时镜像和没有被使用的镜像)
docker image prune
启动镜像
docker run -it NAME:TAG /bin/bash
编辑完后按Esc退出编辑模式,:wq 保存退出
docker version #显示docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #显示docker 帮助命令
镜像命令
docker images 查看所有本地镜像
[root@VM-8-17-centos java]# docker images
REPOSITORY (镜像的仓库源) TAG(镜像的标签) IMAGE ID(镜像的id) CREATED(镜像的创建时间) SIZE(镜像的大小)
tomcat latest 57c97f91f49a 9 hours ago 654MB
mongo latest ef5c2207766e 2 weeks ago 493MB
nginx latest bc9a0695f571 2 weeks ago 133MB
mysql latest dd7265748b5d 2 weeks ago 545MB
redis latest 74d107221092 3 weeks ago 104MB
alpine latest d6e46aa2470d 7 weeks ago 5.57MB
primetoninc/jdk latest 4f4be6992080 2 years ago 879MB
primetoninc/jdk 1.8 f4b4fccc65bb 2 years ago 657MB可选项
-a --all 列出所有镜像
-q --quiet只显示镜像的id
docker search搜索镜像
可选项,通过搜索来过滤
--filter =STRAT=3000
docker pull下载镜像
# 下载镜像 docker pull 镜像名[:tag]
[root@VM-8-17-centos ~]# docker pull tomcat
Using default tag: latest #如果不写tag 默认是latest
latest: Pulling from library/tomcat
756975cb9c7e: Pull complete #分层下载,docker image的核心 联合文件系统
d77915b4e630: Pull complete
5f37a0a41b6b: Pull complete
96b2c1e36db5: Pull complete
27a2d52b526e: Pull complete
a867dba77389: Pull complete
0939c055fb79: Pull complete
0b0694ce0ae2: Pull complete
ecfd61680f76: Pull complete
4cc8f7c49073: Pull complete
Digest: sha256:21e8b2da4fb3d6b92324bc1a06de331f82d90a58a9a37f7ffb9585f227cdca8b # 签名
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest # 真实地址docker pull mysql:5.7指定版本下载
rim --remove 删除镜像
docker rmi -f 容器id #删除指定的容器
docker rmi -f 容器id 容器id 容器id 容器id #删除多个容器
docker rmi -f $(docker images -aq) #删除全部容器
容器命令
说明:有了镜像才可以创建容器,Linux,下载一个centos容器
新建容器并启动
docker run [可选参数] image
#参数说明
--name="name" 容器名称 tomcat01 tomact02; 用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进行容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口
#退出容器
exit 直接退出容器并停止运行容器
ctrl + p + Q 容器不停止退出
列出所有运行的容器
docker ps
列出当前正在运行的容器
-a 列出当前正在运行的容器 +带出历史运行过的容器
-n=? 显示最近创建的容器
-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 镜像名:
常见的坑 docker 容器使用后台运行 ,就必须要有一个前台进程,docker 发现没用应用,就会自动停止
Nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs
部署Nginx
docker run -d --name nginx01 -p 3344:80 nginx
参数
-d 后台运行
--name 给容器命名
-p 宿主机端口,容器内部端口
3a230ab2109602d0a22ef6037c551104d263cce3b06e76f14b66352acf10ed86
[root@VM-8-17-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a230ab21096 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:3344->80/tcp nginx01
本机自测:curl localhost:3344 (通过之后出现下面代码)
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
进入容器
docker exec -it nginx01 /bin/bash
exit 退出容器
停止容器
docker stop 容器id
官方使用的测试容器,用完就删
docker run -it --rm tomcat:9.0
部署tomcat
问题:1Linux命令减少,2、没有webapps,阿里云镜像的原因,默认是最小的镜像,所有不必要的都删掉,保证最小可运行的环境。
解决方案
root@c217e2351c36:/usr/local/tomcat/webapps# cd ..回退上一级
root@c217e2351c36:/usr/local/tomcat# ls查看目录文件
BUILDING.txt README.md conf temp
CONTRIBUTING.md RELEASE-NOTES lib webapps
LICENSE RUNNING.txt logs webapps.dist
NOTICE bin native-jni-lib work
root@c217e2351c36:/usr/local/tomcat# cd webapps进入webapps文件
root@c217e2351c36:/usr/local/tomcat/webapps# cd ..
root@c217e2351c36:/usr/local/tomcat# cd webapps.dist/
root@c217e2351c36:/usr/local/tomcat/webapps.dist# ls
ROOT docs examples host-manager manager
root@c217e2351c36:/usr/local/tomcat/webapps.dist# cd ..
root@c217e2351c36:/usr/local/tomcat# cp -r webapps.dist/* webapps复制webapps.dist/*下的文件到webapps
root@c217e2351c36:/usr/local/tomcat# cd webapps
root@c217e2351c36:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
root@c217e2351c36:/usr/local/tomcat/webapps#