首页 > 其他分享 >Dockerfile - CMD

Dockerfile - CMD

时间:2023-12-10 21:22:07浏览次数:30  
标签:shell form exec CMD ENTRYPOINT command Dockerfile

CMD

The CMD instruction has three forms:

  • CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  • CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.

Note

The exec form is parsed as a JSON array, which means that you must use double-quotes (") around words not single-quotes (').

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

When used in the shell or exec formats, the CMD instruction sets the command to be executed when running the image.

If you use the shell form of the CMD, then the <command> will execute in /bin/sh -c:

FROM ubuntu
CMD echo "This is a test." | wc -

If you want to run your <command> without a shell then you must express the command as a JSON array and give the full path to the executable. This array form is the preferred format of CMD. Any additional parameters must be individually expressed as strings in the array:

FROM ubuntu
CMD ["/usr/bin/wc","--help"]

If you would like your container to run the same executable every time, then you should consider using ENTRYPOINT in combination with CMD. See ENTRYPOINT.

If the user specifies arguments to docker run then they will override the default specified in CMD.

Note

Do not confuse RUN with CMDRUN actually runs a command and commits the result; CMD does not execute anything at build time, but specifies the intended command for the image.

 

Copied from: https://docs.docker.com/engine/reference/builder/#cmd

标签:shell,form,exec,CMD,ENTRYPOINT,command,Dockerfile
From: https://www.cnblogs.com/zhangzhihui/p/17893248.html

相关文章

  • dockerfile基本命令+镜像制作
    DockerFile03DockerFile1.DockerFile的概念用来构建docker​镜像的构建文件,由一系列参数和命令构成的脚本大体总览:​​‍1.构建过程要遵循的规则:​​2.执行流程‍Docker​执行一个Dockerfile​脚本的流程大致如下​Docker​从基础镜像运行一个容器执行一条......
  • Windows 11 cmd命令行修改背景色、设置指定图片、桌面背景
    前言全局说明Windows11cmd命令行修改背景色、设置指定图片、桌面背景一、找到设置--外观可以自定义图片,也可以使用桌面背景图片(二选一)如果设置图片位置或高、宽,没有达到你想要的,可以在“拉伸模式”、“图像对齐”设置二、设置不透明度1.设置背景100%透明度效果......
  • Windows 11命令提示符cmd,默认路径修改
    前言全局说明cmd命令提示符终端,启动默认目录是当前用户的目录下。为了方便使用,默认修改成其他路径一、cmd默认路径默认路径是%USERPROFILE%,如果为空“就使用父路径”二、设置成环境变量值路径%TEMP%设置后,记得点保存效果:如何获取系统变量和系统变量列表:https://......
  • PowerShell原生Cmdlets Get-Command详细使用介绍
    在我们学习PowerShell的初始阶段,我们最需要知道的应该是PowerShell本身给我们提供那些原生的Cmdlets。为了达到这个目的,使用PowerShell的get-command是个不二之选。为了了解这个命令输出的是什么对象,我们可以输入下面的命令进行了解Get-Command|Get-Member从输出种我们可以看到从上......
  • Java执行cmd命令.并打印输出. 解决中文乱码 .
    packageorg.example;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.nio.charset.Charset;publicclassMain{publicstaticvoidmain(String[]args){try{ProcessBuilder......
  • maven 配置(cmd 黑窗口执行 mvn 时默认的 settings 文件和 idea maven 相关配置)
    写在前面:本文章用于记录博主平时遇到的问题,步骤略粗糙,目的在于记录一边后续博主自己查找,如果能帮助到其他人更好。文章中用到的链接均为自行引入,侵删,谢谢(2I2Rc*@JY8)问题说明:在一次使用cmdmvn命令通过下载到本地的第三方jar包(ojdbc8.jar)创建本地maven仓库的文件结构时发现......
  • 深入理解Dockerfile:构建容器化应用的基石
    Docker已经成为现代软件开发和部署的标配工具之一,它的轻量级容器技术使得应用可以在不同环境中快速部署和运行。Dockerfile是构建Docker镜像的蓝图,定义了从基础镜像到最终应用镜像的一系列步骤。本篇博文将深入解析Dockerfile中常见的指令,带你逐步了解如何构建高效、可维护的Docker......
  • window 使用cmd命令生成项目的目录树
    window使用tree命令生成目录树,只有/F和/A命令,并不满足我们需要过滤不必要文件和排序等等需求,所以我使用了一个插件tree-node-cli。 在cmd窗口安装tree-node-cli插件npminstall-gtree-node-cli 插件安装成功后在cmd窗口执行命令,执行命令前使用cd命令切到项目文件夹......
  • 【DevEco Studio】报错Error: spawn cmd.exe ENOENT怎么解决?
    ​【关键字】hvigor报错、Error:spawncmd.exeENOENT 【问题背景】编译的时候报Error:spawncmd.exeENOENT该怎么解决?预览的时候报Error:spawncmd.exeENOENT该怎么解决?具体报错截图如下:​​ 【解决方案】这种是环境变量缺少了C:\Windows\System32导致的,在Path......
  • 【DevEco Studio】报错Error: spawn cmd.exe ENOENT怎么解决?
    【关键字】hvigor报错、Error:spawncmd.exeENOENT【问题背景】编译的时候报Error:spawncmd.exeENOENT该怎么解决?预览的时候报Error:spawncmd.exeENOENT该怎么解决?具体报错截图如下:【解决方案】这种是环境变量缺少了C:\Windows\System32导致的,在Path里面新建一个把值复制进......