我们继承centos镜像,在新的镜像中安装curl工具,然后通过curl访问 https://ip.cn 网址,返回当前电脑的IP地址所属地。
CMD命令实例:
# 继承基础 centos FROM centos # 安装curl程序 RUN yum install -y curl # 访问 https://ip.cn 网址 CMD ["curl", "-s", "https://ip.cn"]
使用 docker build -f Dockerfile3 -t mycentos2 . 命令构建镜像,运行过程:
$ docker build -f Dockerfile3 -t mycentos2 . Sending build context to Docker daemon 32.77kB Step 1/3 : FROM centos ---> 9f38484d220f Step 2/3 : RUN yum install -y curl ---> Running in b41017c3617d Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Package curl-7.29.0-51.el7.x86_64 already installed and latest version Nothing to do Removing intermediate container b41017c3617d ---> c171c6bcc5e8 Step 3/3 : CMD ["curl", "-s", "https://ip.cn"] ---> Running in c31a33840f44 Removing intermediate container c31a33840f44 ---> 17945222efb6 Successfully built 17945222efb6 Successfully tagged mycentos2:latest
运行我们的镜像,如下:
$ docker images mycentos2 REPOSITORY TAG IMAGE ID CREATED SIZE mycentos2 latest 17945222efb6 5 minutes ago 306MB $ curl -s https://ip.cn 当前 IP:218.241.251.153 来自:北京市
如果我们想要查看HTTP报文的头部信息,此时需要使用 -i 选项,运行如下:
$ curl -s https://ip.cn -i docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"-i\": executable file not found in $PATH": unknown.
抛出错误信息。为什么呢?这是因为 CMD 命令只能存在一行。如果我们在运行容器时,添加 -i 参数,此时将作为一个 CMD 命令去覆盖容器最后的 CMD命令。如下:
FROM centos RUN yum install -y curl CMD ["curl", "-s", "https://ip.cn"] # -i命令肯定时不存在的 CMD "-i"
解决办法:使用 ENTRYPOINT 替代 CMD 命令。如下:
# 继承基础 centos FROM centos # 安装curl程序 RUN yum install -y curl # 访问 https://ip.cn 网址 ENTRYPOINT ["curl", "-s", "https://ip.cn"] # 如果我们在运行容器时,添加了 -i 参数,此时 ENTRYPOINT 如下 ENTRYPOINT ["curl", "-s", "https://ip.cn", "-i"]
根据Dockerfile构建镜像:
$ docker build -f Dockerfile4 -t mycentos3 . Sending build context to Docker daemon 33.79kB Step 1/3 : FROM centos ---> 9f38484d220f Step 2/3 : RUN yum install -y curl ---> Using cache ---> c171c6bcc5e8 Step 3/3 : ENTRYPOINT ["curl", "-s", "https://ip.cn"] ---> Running in 13d430f1fa5b Removing intermediate container 13d430f1fa5b ---> 09ab3dc88a65 Successfully built 09ab3dc88a65 Successfully tagged mycentos3:latest
使用“docker images”查询刚刚创建的镜像:
$ docker images mycentos3 REPOSITORY TAG IMAGE ID CREATED SIZE mycentos3 latest 09ab3dc88a65 27 seconds ago 306MB
运行镜像,如下:
$ docker run -it mycentos3 -i HTTP/1.1 200 ok Date: Mon, 08 Jul 2019 14:24:31 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Set-Cookie: __cfduid=d2599359f74f3f026dec2ccd9f6207a731562595871; expires=Tue, 07-Jul-20 14:24:31 GMT; path=/; domain=.ip.cn; HttpOnly; Secure Cache-Control: max-age=15 Expires: Mon, 08 Jul 2019 14:24:46 GMT X-Frame-Options: SAMEORIGIN Server: cloudflare CF-RAY: 4f32b8e59b219376-SJC 当前 IP:218.241.251.153 来自:北京市
实例:
FROM centos RUN echo "hello...." ENTRYPOINT ["ping", "-c", "5", "https://www.baidu.com"] ENTRYPOINT ["curl", "-s", "https://ip.cn"] ENTRYPOINT ["echo", "hello world...."]
使用上面的Dockerfile文件构建镜像,然后运行镜像。将输出如下信息:
# 构建镜像 $ docker build -f Dockerfile5 -t myip . # 运行镜像 $ docker run -it mypi hello world....
注意:ENTRYPOINT命令也只能出现一条,只有最后一条有效,只是运行容器指定的参数将追加到ENTRYPOINT命令后面,作为该命令的一部分,而不是整体替换ENTRYPOINT命令。