Docker操作
Docker基本操作
bash
# 删除 docker 容器
docker rm 关键字(名字、容器ID)
# 删除docker所有容器
docker ps -a -q | xargs docker rm
docker rm -f $(docker ps -qa)
# 删除 docker镜像
docker rmi 关键字(名字、容器ID)
# 删除docker所有镜像
docker images -a -q | xargs docker rmi
docker rm -f $(docker images -a -q)
# 容器内数据复制到docker主机
docker cp
# 启动tomcat并且设置对外暴露端口
docker run -it -p 8888:8080 tomcat
# 进入容器跟目录
docker exec -it ID /bin/bash
# 容器内文件复制到宿主机
docker cp ID:/1.txt /root/1.txt
# 容器打镜像
docker commit -a="CHENYB -m="chen test" ID atquiqu/mytomcat:test1.0
# 查看容器详情
docker inspect ID
# 镜像变更历史
docker history ID
创建数据容器卷
- 命令创建
```bash
# 创建数据容器卷,启动容器,并且在容器内建立与宿主机共享文件夹':'前为宿主机文件夹后为容器内文件夹
docker run -it -v /centos7Data:/hostData ID
# 创建数据容器卷,但是容器内只读权限,'ro'red only 权限智能限制容器,不能限制宿主机
docker run -it -v /centos7Data:/hostData:ro ID
```
-
Dockerfile 创建
- Docker脚本内容
bash# volume test FROM centos VOLUME ["/hostData1","/hostData2"] CMD echo "finished,--- success1 " CMD /bin/bash
-
执行Dodkcerfile 脚本
- 执行根目录下/mydocker下的Dockerfile文件,创建daokcer镜像,启动镜像后会在宿主机默认位置/var/lib/docker/volumes/?/_data 创建对应的共享文件夹,'docker inspect'命令可以查看,命令行最后一个'.'不要忽略
bashdocker build -f /mydocker/Dockerfile -t CHENYB/centos:1 .
-
容器数据复制
*bashdocker run -it --name data2 --volumes-from data1 ID docker run -it --name data3 --volumes-from data1 ID
- 多容器之间的复制是互相的,1、2、3之间可以互相复制,少了哪个容器后复制仍会继续,容器卷共享生命周期一直持续到没有容器使用为止
Dockerfile 保留字指令
-
FROM 基础新镜像基于的镜像
-
MAINTAINER 作者+作者邮箱
-
RUN 容器构建时需要运行的命令
-
EXPOSE 对外暴露的端口号
-
WOEKDIR 登录后进入的目录,没有指定默认为根目录
-
ENV 在构建过程中设置环境变量,可以被引用
-
ADD COPY+解压缩
-
COPY 复制,源路径的文件复制到新的一层镜像内路径 例如:1、COPY src mysrc;2、COPY["src""mysrc"]两种写法
-
VOLUME 容器数据卷,用户数据保存和持久化工作
-
CMD 指定一个容器启动时要执行的命令,Dockerfile中可以有多个CMD指令,但是只有最后一个生效,CMD会被docker run 之后的参数替换掉
-
ENTRYPOINT 指定一个容器启动是运行的命令,ENTRYPOINT的目的和CMD一样,都是指定容器启动程序及参数,不会被docker run之后的参数替换,会追加
-
ONBUILD 当构建一个被继承的Dockerfile是运行命令,父镜像在被子镜像继承后父镜像的ONBUILD被触发
相近保留字指令详解
-
CMD与ENTRYPOINT 区别脚本
```bash
FROM centos:7
RUN yum install -y curl
# update
#CMD ["curl","-s","http://www.baidu.com"]
# add
#ENTRYPOINT ["curl","-s","http://www.baidu.com"]
```
-
docker run -it mycentos:2.3 -i 启用CMD保留字指令时运行该命令会将参数'-i'替换["curl","-s","http://www.baidu.com"] 命令,导致单独执行"-i"linux指令,会报错无法执行
-
docker run -it mycentos:2.3 -i 启用ENTRYPOINT留字指令时运行该命令会将参数'-i'追加["curl","-s","http://www.baidu.com"] 命令后方执行,完整指令["curl","-s","http://www.baidu.com","-i"] 响应结果
```bash
[root@192 mydocker]# docker run -it mycentos:2.3 -i
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2381
Content-Type: text/html
Date: Tue, 27 Jun 2023 14:27:46 GMT
Etag: "588604c1-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:29 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
```
-
Dockerfile脚本中,多个ENTRYPOINT保留字指令与CMD一样,只会执行最后一个
```bash
update
# add
CMD ["curl","-s","http://www.baidu.com"]
# add
ENTRYPOINT ["curl","-s","www.qq.com"]
```
-
如果Dockerfile脚本中同时存在CMD与ENTRYPOINT保留字指令,两者都会生效,优先执行ENTRYPOINT保留字指令
-
Dockerfile ONBUILD促发脚本
-
被继承的父级镜像的Dockerfile脚本
bashROM centos:7 ONBUILD RUN echo "father hello!" CMD echo "1"
- 继承父级脚本构建的子镜像Docker脚本
bashFROM myonbuildtest CMD ["echo","son!!!"]
-
子镜像构建时,会触发父Dockerfile脚本中的ONBUILD保留字指令
bash[root@192 mydocker]# docker build -f ./Dockerfile3 -t myonbuildtest . [+] Building 15.3s (5/5) FINISHED => [internal] load build definition from Dockerfile3 0.0s => => transferring dockerfile: 157B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:7 15.3s => CACHED [1/1] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315 0.0s => => resolve docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:6be84bd208a361a45fd7ec9c8f4120905b148ed25aa4b22ea6060910908e618f 0.0s => => naming to docker.io/library/myonbuildtest 0.0s [root@192 mydocker]# docker build -f ./Dockerfile4 -t myonbuildtest2 . [+] Building 0.0s (6/6) FINISHED => [internal] load build definition from Dockerfile4 0.0s => => transferring dockerfile: 136B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/myonbuildtest:latest 0.0s => [1/1] FROM docker.io/library/myonbuildtest 0.0s => CACHED [2/1] RUN echo "father hello!" 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:86a69e7f1bf75a7acbe9755bbfc17a09b7823af63005444095bded6bd90f5789 0.0s => => naming to docker.io/library/myonbuildtest2
-
推送本地镜像到云端
```bash
# 这里推送到阿里云,在阿里云上都有详细说明
docker login --username=tb99961359 registry.cn-hangzhou.aliyuncs.com
docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/chenyongbin/test:[镜像版本号]
docker push registry.cn-hangzhou.aliyuncs.com/chenyongbin/test:[镜像版本号]
```