当前位置:   article > 正文

docker 使用Dockerfile生成镜像文件_sending build context to docker daemon 208.9kb ste

sending build context to docker daemon 208.9kb step 1/3 : from microsoft/win

使用 Dockerfile 生成镜像文件

 

dockerfile文件指令不区分大小写

 

from:基础镜像,首行必须为该指令,可设置多个

maintainer:作者信息,如邮箱地址:maintainer user@163.com

 

******************************

run :构建镜像时运行

 

指令格式:

run command:默认在bin/sh 终端执行,执行格式 bin/sh -c command

run ["executable","param1","param2"]:执行格式 executable param1 param2

 

示例:run echo hello world、run echo "hello world"

  1. #Dockerfile
  2. from centos
  3. run echo hello world
  4. run echo "hello world"
  5. #构建镜像输出
  6. [root@centos test2]# docker build -t test .
  7. Sending build context to Docker daemon 2.048kB
  8. Step 1/3 : from centos
  9. ---> 0d120b6ccaa8
  10. Step 2/3 : run echo hello world
  11. ---> Running in 46c168ea687f
  12. hello world //输出:hello world
  13. Removing intermediate container 46c168ea687f
  14. ---> e55a98be540c
  15. Step 3/3 : run echo "hello world"
  16. ---> Running in 78b7f219ffd4
  17. hello world //输出:hello world
  18. Removing intermediate container 78b7f219ffd4
  19. ---> 0cce29370946
  20. Successfully built 0cce29370946
  21. Successfully tagged test:latest

 

示例 2:run ["echo","hello world"]

  1. #Dockerfile
  2. from centos
  3. run ["echo","hello world"]
  4. run ["echo","hello world","hello world2"]
  5. #构建镜像输出
  6. [root@centos test2]# docker build -t test .
  7. Sending build context to Docker daemon 2.048kB
  8. Step 1/3 : from centos
  9. ---> 0d120b6ccaa8
  10. Step 2/3 : run ["echo","hello world"]
  11. ---> Running in dfbfe1474f0b
  12. hello world //输出:hello world
  13. Removing intermediate container dfbfe1474f0b
  14. ---> 5f46fd188430
  15. Step 3/3 : run ["echo","hello world","hello world2"]
  16. ---> Running in cc7888f08910
  17. hello world hello world2 //输出:hello world hello world2
  18. Removing intermediate container cc7888f08910
  19. ---> afab2dbd1e2e
  20. Successfully built afab2dbd1e2e
  21. Successfully tagged test:latest

 

示例 3:run ["bin/bash","-c","echo hello world"]

  1. #Dockerfile
  2. from centos
  3. run ["bin/bash","-c","echo hello world"]
  4. #构建镜像输出
  5. [root@centos test2]# docker build -t test .
  6. Sending build context to Docker daemon 2.048kB
  7. Step 1/2 : from centos
  8. ---> 0d120b6ccaa8
  9. Step 2/2 : run ["bin/bash","-c","echo hello world"]
  10. ---> Running in 9d2db05532ee
  11. hello world //输出:hello world
  12. Removing intermediate container 9d2db05532ee
  13. ---> 6fdebf83d666
  14. Successfully built 6fdebf83d666
  15. Successfully tagged test:latest

说明:-c 表示后面接命令行,第一个空格前的为命令,后面为参数

 

 

******************************

cmd :容器运行时执行

 

命令格式:

cmd command:执行格式 bin/sh -c command

cmd ["executable", "param1", "param2"]:执行格式 executable param1 param2

说明:如果指定多个cmd,只有最后一个执行;docker run如果指定启动命令,则忽略cmd

 

 

******************************

entrypoint :容器运行时执行,优先级较cmd高

 

命令格式:

entrypoint command param1 param2:执行格式 bin/sh -c command param1 param2

entrypoint ["executable", "param1", "param2"]:执行格式 executable param1 param2

 

 

******************************

cmd、entrypoint同时使用

 

cmd command、entrypoint command:忽略cmd command

cmd command、entrypoint ["executable","param","param2"]:cmd command转做做字符串"bin/sh -c command"传递给entrypoint,放在param2后面

cmd ["executable","param","param2"]、entrypoint command:忽略cmd ["executable","param","param2"]

cmd ["executable","param","param2"]、entrypoint ["executable","param","param2"]:将cmd的executable、param、param2当做字符串参数传递给entrypoint

 

示例:cmd echo "hello world"

  1. #Dockerfile
  2. from centos
  3. cmd echo "hello world"
  4. #构建镜像
  5. docker build -t test .
  6. #创建容器输出
  7. [root@centos test2]# docker run -it --name test test
  8. hello world

 

示例 2:cmd ["echo","hello world"]

  1. #Dockerfile
  2. from centos
  3. cmd ["echo","hello world"]
  4. #构建镜像
  5. docker build -t test .
  6. #创建容器
  7. [root@centos test2]# docker run -it --name test test
  8. hello world

 

示例 3:cmd ["echo","hello world","hello world2"]、entrypoint ["echo","瓜田李下"]

  1. #Dockerfile
  2. from centos
  3. cmd ["echo","hello world","hello world2"]
  4. entrypoint ["echo","瓜田李下"]
  5. #构建镜像
  6. docker build -t test .
  7. #创建容器
  8. [root@centos test2]# docker run -it --name test test
  9. 瓜田李下 echo hello world hello world2

 

示例 4:cmd echo "hello world"、entrypoint ["echo","瓜田李下"]

  1. #Dockerfile
  2. from centos
  3. cmd echo "hello world"
  4. entrypoint ["echo","瓜田李下"]
  5. #构建镜像
  6. docker build -t test .
  7. #创建容器
  8. [root@centos test2]# docker run -it --name test test
  9. 瓜田李下 /bin/sh -c echo "hello world"

 

 

******************************

其他指令

 

label :声明镜像的元数据信息,格式为:label key=value,如label version="1.0"

arg :指定镜像内使用的参数,格式为:arg name=default_value,运行时docker run --build-arg name=value传入参数

expose :声明镜像内服务监听的端口

 

env :声明环境变量,格式为:env key value、env key=value

 

add :复制主机目录内容到容器相应目录,如果是tar文件,会自动解压到对应目录,格式为: add src dest

copy :复制主机目录内容到对应目录,格式为:copy src dest

 

volume :创建数据卷挂载点,格式为:volume ["/data"]

user :指定容器运行时的用户名,格式为:user user_name

workdir :容器初始工作目录,格式为:workdir path

 

onbuild :创建的镜像为其他镜像的基础镜像时,执行的命令,格式为 :onbuild command

如:onbuild echo "hello world"

 

stopsignal :指定镜像创建的容器接收的退出信号

shell :指定shell的默认类型,默认为:["bin/sh", "-c"]

 

healthcheck :容器健康监测,命令格式:

healthcheck [options] cmd command:根据执行的命令返回只是否为0判断

healthcheck none:没有健康检查

options选项:

--interval=30s(默认为30s):间隔多久检查一次

--timeout=30s(默认为30s):检查等待的超时时间

--retries=3:检查失败时的重试次数

 

 

********************************

示例:centos 为基础镜像,制作java镜像

 

Dockerfile 文件

  1. from centos
  2. add java /usr/java
  3. env JAVA_HOME=/usr/java
  4. env PATH $JAVA_HOME/bin:$PATH

 

构造镜像并创建容器

    

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/57928?site
推荐阅读
相关标签
  

闽ICP备14008679号