Docker基本的命令 一、镜像命令 1.查看仓库镜像 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 # 查询所有的镜像 docker images # 可以使用的参数 Options: -a, --all Show all images (default hides intermediate images) #显示所有信息 --digests Show digests -f, --filter filter Filter output based on conditions provided #过滤请求 --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet Only show numeric IDs #只显示ID # image的属性 REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 11 months ago 13.3kB # REPOSITORY 仓库的名字,我们可以通过这个在dockerhub里面获得镜像 # TAG 版本号 # IMAGE ID 镜像ID # CREATED 创建时间 # SIZE 大小
2.搜索仓库镜像 1 2 3 4 # 命令解释 docker search --help Search the Docker Hub for images #从docker Hub里面搜索镜像 # 按照条件搜索镜像 docker search mysql --filter=STARs=3000 #搜索镜像stars大于3000的镜像
3.下载镜像 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 # docker pull --help 解释 Pull an image or a repository from a registry # 下载 mysql docker pull mysql #默认版本为最新 # 最新版下载信息如下: Using default tag: latest latest: Pulling from library/mysql 852e50cd189d: Pull complete 29969ddb0ffb: Pull complete a43f41a44c48: Pull complete 5cdd802543a3: Pull complete b79b040de953: Pull complete 938c64119969: Pull complete 7689ec51a0d9: Pull complete a880ba7c411f: Pull complete 984f656ec6ca: Pull complete 9f497bce458a: Pull complete b9940f97694b: Pull complete 2f069358dc96: Pull complete Digest: sha256:4bb2e81a40e9d0d59bd8e3dc2ba5e1f2197696f6de39a91e90798dd27299b093 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest # 接下来再下载其他版本的,看一下区别 docker pull mysql:5.7#指定的版本只可以是仓库里面已经有的东西 5.7: Pulling from library/mysql 852e50cd189d: Already exists 29969ddb0ffb: Already exists a43f41a44c48: Already exists 5cdd802543a3: Already exists b79b040de953: Already exists 938c64119969: Already exists 7689ec51a0d9: Already exists 36bd6224d58f: Pull complete cab9d3fa4c8c: Pull complete 1b741e1c47de: Pull complete aac9d11987ac: Pull complete Digest: sha256:8e2004f9fe43df06c3030090f593021a5f283d028b5ed5765cc24236c2c4d88e #签名信息 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7 #仓库地址
从上面我们可以看出来,有的文件实现了复用,这样的好处就是使我们的文件大小更加小,更加节约空间
4.删除镜像 1 2 3 4 5 6 7 8 # 命令解释 docker rmi --help Remove one or more images # 根据名字删除镜像 docker rmi mysql:5.7 # 根据ID删除镜像 docker rmi -f bf756fb1ae65 # 删除全部镜像 使用$取值,这里的就是我们所有的镜像编号 docker rmi $(docker images -aq)
二、仓库命令 1.运行命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # 命令 docker run [OPTIONS] IMAGE [COMMAND][ARG...] # 常用参数说明 --name="Name" # 给容器指定一个名字 -d # 后台方式运行容器,并返回容器的id! -i # 以交互模式运行容器,通过和 -t 一起使用 -t # 给容器重新分配一个终端,通常和 -i 一起使用 -P # 随机端口映射(大写) -p # 指定端口映射(小结),一般可以有四种写法 ip:hostPort:containerPort ip::containerPort hostPort:containerPort (常用) containerPort # 测试 [root@kuangshen ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 470671670cac 3 months ago 237MB # 使用centos进行用交互模式启动容器,在容器内执行/bin/bash命令! [root@kuangshen ~]# docker run -it centos /bin/bash [root@dc8f24dd06d0 /]# ls # 注意地址,已经切换到容器内部了! bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr
2.退出命令 1 2 exit # 容器停止退出 ctrl+P+Q # 容器不停止退出
3.启动和停止容器 1 2 3 4 docker start (容器id or 容器名) # 启动容器 docker restart (容器id or 容器名) # 重启容器 docker stop (容器id or 容器名) # 停止容器 docker kill (容器id or 容器名) # 强制停止容器
4.删除容器 1 2 3 docker rm 容器id # 删除指定容器 docker rm -f $(docker ps -a -q) # 删除所有容器 docker ps -a -q|xargs docker rm # 删除所有容器
5.列出所有的容器 1 2 3 4 5 6 7 # 命令 docker ps [OPTIONS] # 常用参数说明 -a # 列出当前所有正在运行的容器 + 历史运行过的容器 -l # 显示最近创建的容器 -n=? # 显示最近n个创建的容器 -q # 静默模式,只显示容器编号。
三、其他的常用命令 后台启动容器 1 2 3 4 5 6 7 8 # 命令 docker run -d 容器名 # 例子 docker run -d centos # 启动centos,使用后台方式启动 # 问题: 使用docker ps 查看,发现容器已经退出了! # 解释:Docker容器后台运行,就必须有一个前台进程,容器运行的命令如果不是那些一直挂起的命 令,就会自动退出。 # 比如,你运行了nginx服务,但是docker前台没有运行应用,这种情况下,容器启动后,会立即自杀,因为他觉得没有程序了,所以最好的情况是,将你的应用使用前台进程的方式运行启动。
查看日志 1 2 3 4 5 6 7 8 9 10 11 12 # 命令 docker logs -f -t --tail 容器id # 例子:我们启动 centos,并编写一段脚本来测试玩玩!最后查看日志 [root@kuangshen ~]# docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1;done" [root@kuangshen ~]# docker ps CONTAINER ID IMAGE c8530dbbe3b4 centos # -t 显示时间戳 # -f 打印最新的日志 # --tail 数字 显示多少条! [root@kuangshen ~]# docker logs -tf --tail 10 c8530dbbe3b4
在实际测试中,我遇到bug他会一直卡在界面上,看不见日志,所以我也在找原因
查看docker里面的进程信息 1 2 3 4 5 6 # 命令 docker top 容器id # 测试 [root@kuangshen ~]# docker top c8530dbbe3b4 UID PID PPID C STIME TTY TIME CMD root 27437 27421 0 16:43 ? 00:00:00 /bin/sh -c ....
与docker里面的 docker ps 对比
信息比较如下:
docker top 容器id
1 2 3 UID PID PPID C STIME TTY TIME CMD root 47937 47921 0 20:52 pts/0 00:00:00 /bin/bash
docker ps
1 2 3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e5f4d5207c7a centos "/bin/bash" 8 minutes ago Up 8 minutes agitated_wing
docker top 容器id 里面显示的PPID指的是容器里面的pid的值,但是docker ps里面却无法显示这一个数据
进入正在运行的进程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 # 命令1 docker exec -it 容器id bashShell+命令 可以在后面加上一些类似于touch 的命令 # 测试1 [root@kuangshen ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c8530dbbe3b4 centos "/bin/sh -c 'while t…" 12 minutes ago Up 12 minutes happy_chaum [root@kuangshen ~]# docker exec -it c8530dbbe3b4 /bin/bash [root@c8530dbbe3b4 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 08:43 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep root 751 0 0 08:56 pts/0 00:00:00 /bin/bash root 769 1 0 08:56 ? 00:00:00 /usr/bin/coreutils -- coreutils-prog-shebang=s root 770 751 0 08:56 pts/0 00:00:00 ps -ef # 命令2 docker attach 容器id # 测试2 [root@kuangshen ~]# docker exec -it c8530dbbe3b4 /bin/bash [root@c8530dbbe3b4 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 08:43 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep root 856 0 0 08:57 pts/0 00:00:00 /bin/bash root 874 1 0 08:57 ? 00:00:00 /usr/bin/coreutils -- coreutils-prog-shebang=s root 875 856 0 08:57 pts/0 00:00:00 ps -ef # 区别 # exec 是在容器中打开新的终端,并且可以启动新的进程 # attach 直接进入容器启动命令的终端,不会启动新的进程
理解上面的两个命令:
使用exec,那么每一次都是打开一个新的终端,那么他的每一次的bash 的pid 都是不一样的,并且直接使用 exit 容器不会直接退出;
使用attach,每次打开都是同一个Bash,并且你所在位置就在你上一次推出时候的位置,也代表了这个命令不会产生新的进程,使用exit 直接关闭容器
查看容器/镜像的元数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 # 命令 docker inspect 容器id # 测试 [root@kuangshen ~]# docker inspect c8530dbbe3b4 [ { # 完整的id,有意思啊,这里上面的容器id,就是截取的这个id前几位! "Id": "c8530dbbe3b44a0c873f2566442df6543ed653c1319753e34b400efa05f77cf8", "Created": "2020-05-11T08:43:45.096892382Z", "Path": "/bin/sh", "Args": [ "-c", "while true;do echo kuangshen;sleep 1;done" ], # 状态 "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 27437, "ExitCode": 0, "Error": "", "StartedAt": "2020-05-11T08:43:45.324474622Z", "FinishedAt": "0001-01-01T00:00:00Z" }, // ........... ]
把容器里面的数据拷贝到宿主机里面 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # 命令 docker cp 容器id:容器内路径 目的主机路径 # 测试 # 容器内执行,创建一个文件测试 # 环境在容器类,当前在容器里面 [root@c8530dbbe3b4 /]# cd /home [root@c8530dbbe3b4 home]# touch f1 [root@c8530dbbe3b4 home]# ls f1 [root@c8530dbbe3b4 home]# exit exit # linux复制查看,是否复制成功 [root@kuangshen ~]# docker cp c8530dbbe3b4:/home/f1 /home [root@kuangshen ~]# cd /home [root@kuangshen home]# ls f1
四、相关练习 在Docker里面部署nginx 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 docker search nginx docker pull nginx docker run -d -p 3333:80 nginx #宿主机端口:容器内端口 后台运行 # 测试 curl localhost:3333 <!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里面部署tomcat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # 下载 docker pull tomcat # 官方文档解释 # -it :交互模式 # --rm:容器启动成功并退出以后容器就自动移除,一般在测试情况下使用! docker run -it --rm tomcat:9.0 # 1、下载tomcat镜像 docker pull tomcat # 2、启动 docker run -d -p 8080:8080 --name tomcat9 tomcat # 3、进入tomcat docker exec -it tomcat9 /bin/bash # 4、思考:我们以后要部署项目,还需要进入容器中,是不是十分麻烦,要是有一种技术,可以将容器 # 内和我们Linux进行映射挂载就好了?我们后面会将数据卷技术来进行挂载操作,也是一个核心内容,这 # 里大家先听听名词就好,我们很快就会讲到!
布置好上面的配置以后,我们发现页面是404 ,就是代表我们的webapps里面没有相应的项目,所以我们要把webapps.dist里面的项目移动到webapps目录下面;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root@iZwz94khotag1q066igytrZ home]# docker restart tomcat tomcat [root@iZwz94khotag1q066igytrZ home]# docker exec -it tomcat /bin/bash root@81e095d08b23:/usr/local/tomcat# ls BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work root@81e095d08b23:/usr/local/tomcat# cd webapps root@81e095d08b23:/usr/local/tomcat/webapps# ls root@81e095d08b23:/usr/local/tomcat/webapps# cd .. root@81e095d08b23:/usr/local/tomcat# cp webapps.dist/* webapp cp: target 'webapp' is not a directory root@81e095d08b23:/usr/local/tomcat# cp webapps.dist/* webapps cp: -r not specified; omitting directory 'webapps.dist/ROOT' cp: -r not specified; omitting directory 'webapps.dist/docs' cp: -r not specified; omitting directory 'webapps.dist/examples' cp: -r not specified; omitting directory 'webapps.dist/host-manager' cp: -r not specified; omitting directory 'webapps.dist/manager' root@81e095d08b23:/usr/local/tomcat# cp -r webapps.dist/* webapps root@81e095d08b23:/usr/local/tomcat# cp -r webapps.dist/* webapps root@81e095d08b23:/usr/local/tomcat# cd webapps root@81e095d08b23:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager root@81e095d08b23:/usr/local/tomcat/webapps#
然后再次访问 ip地址+端口号,就会出现下面的图,表示我们部署成功
使用docker 部署 es + kibana 1 2 3 4 # 4、增加上内存限制启动 docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e#配置端口号 "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m"#配置最大的请求内存 elasticsearch:7.6.2 #版本号
部署成功后出现下面的数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 { "name" : "902795f9120d" , "cluster_name" : "docker-cluster" , "cluster_uuid" : "KvuZMGwwRgq2PFaAqcS0zg" , "version" : { "number" : "7.6.2" , "build_flavor" : "default" , "build_type" : "docker" , "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f" , "build_date" : "2020-03-26T06:34:37.794943Z" , "build_snapshot" : false , "lucene_version" : "8.4.0" , "minimum_wire_compatibility_version" : "6.8.0" , "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
内存占用情况
1 902795f9120d elegant_hertz 0.52% 353.2MiB / 1.787GiB 19.31% 6.9kB / 3.63kB 14.5MB / 928kB 43
如果不使用上面的语句的话,docker里面部署elastic search 会占用很大的内存,就会浪费资源。使用以后,资源得到充分的利用