热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用dockerrun命令将参数传递给Dockerfile中的CMD-UsedockerruncommandtopassargumentstoCMDinDockerfile

ImnewtoDockerandImhavingahardtimetosetupthedockercontainerasIwant.Ihaveanodej

I'm new to Docker and I'm having a hard time to setup the docker container as I want. I have a nodejs app can take two parameters when start. For example, I can use

我是Docker的新手,我很难按自己的意愿设置Docker容器。我有一个nodejs应用,启动时可以取两个参数。例如,我可以使用。

node server.js 0 dev

节点服务器。js 0开发

or

node server.js 1 prod

节点服务器。js - 1刺激

to switch between production mode and dev mode and determine if it should turn the cluster on. Now I want to create docker image with arguments to do the similar thing, the only thing I can do so far is to adjust the Dockerfile to have a line

在生产模式和开发模式之间切换,并确定是否应该打开集群。现在我想用参数创建docker图像来做类似的事情,到目前为止我唯一能做的就是调整Dockerfile以得到一条直线。

CMD [ "node", "server.js", "0", "dev"]

CMD[“节点”、“服务器。js”、“0”、“开发”)

and

docker build -t me/app . to build the docker.

docker构建-t me/app。构建码头工人。

Then docker run -p 9000:9000 -d me/app to run the docker.

然后docker运行-p 9000:9000 -d me/app来运行docker。

But If I want to switch to prod mode, I need to change the Dockerfile CMD to be

但是如果我想切换到prod模式,我需要将Dockerfile CMD更改为

CMD [ "node", "server.js", "1", "prod"] ,

CMD[“节点”、“服务器。js”、“1”、“刺激”),

and I need to kill the old one listening on port 9000 and rebuild the image. I wish I can have something like

我需要杀死那个听9000端口的老家伙,重建这个图像。我希望我能有类似的东西

docker run -p 9000:9000 envirOnment=dev cluster=0 -d me/app

docker运行-p 9000:9000环境=dev cluster=0 -d me/app

to create an image and run the nodejs command with "environment" and "cluster" arguments, so I don't need to change the Dockerfile and rebuild the docker any more. How can I accomplish this?

要创建一个映像并使用“environment”和“cluster”参数运行nodejs命令,所以我不再需要更改Dockerfile并重新构建docker。我怎样才能做到这一点呢?

3 个解决方案

#1


19  

Make sure your Dockerfile declares an environment variable with ENV:

确保Dockerfile使用ENV声明了一个环境变量:

ENV environment default_env_value
ENV cluster default_cluster_value

The ENV form can be replaced inline.

ENV 表单可以内联替换。

Then you can pass an environment variable with docker run

然后,您可以通过docker run传递环境变量。

docker run -p 9000:9000 -e envirOnment=dev -e cluster=0 -d me/app

Or you can set them through your compose file:

或者你可以通过你的撰写文件来设置它们:

node:
  environment:
    - envirOnment=dev
    - cluster=0

Your Dockerfile CMD can use that environment variable, but, as mentioned in issue 5509, you need to do so in a sh -c form:

您的Dockerfile CMD可以使用该环境变量,但是,正如第5509号问题所提到的,您需要以sh -c形式这样做:

CMD ["sh", "-c", "node server.js ${cluster} ${environment}"]

The explanation is that the shell is responsible for expanding environment variables, not Docker. When you use the JSON syntax, you're explicitly requesting that your command bypass the shell and be executed directly.

解释是shell负责扩展环境变量,而不是Docker。当您使用JSON语法时,您显式地请求您的命令绕过shell并直接执行。

Same idea with Builder RUN (applies to CMD as well):

Builder RUN也有同样的想法(同样适用于CMD):

Unlike the shell form, the exec form does not invoke a command shell.
This means that normal shell processing does not happen.

与shell表单不同,exec表单不调用命令shell。这意味着正常的shell处理不会发生。

For example, RUN [ "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: RUN [ "sh", "-c", "echo $HOME" ].

例如,RUN ["echo", "$HOME"]不会对$HOME进行变量替换。如果您想要shell处理,那么可以使用shell表单或直接执行shell,例如:RUN ["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.

当使用exec形式并直接执行shell时,就像在shell表单中一样,它是环境变量扩展的shell,而不是docker。

#2


12  

Another option is to use ENTRYPOINT to specify that node is the executable to run and CMD to provide the arguments. The docs have an example in Exec form ENTRYPOINT example.

另一种选择是使用ENTRYPOINT指定该节点是运行的可执行文件和CMD来提供参数。文档在Exec表单入口点示例中有一个示例。

Using this approach, your Dockerfile will look something like

使用这种方法,您的Dockerfile将看起来像这样

FROM ...

ENTRYPOINT [ "node",  "server.js" ]
CMD [ "0", "dev" ]

Running it in dev would use the same command

在dev中运行它将使用相同的命令

docker run -p 9000:9000 -d me/app

and running it in prod you would pass the parameters to the run command

在prod中运行时,将参数传递给run命令

docker run -p 9000:9000 -d me/app 1 prod

You may want to omit CMD entirely and always pass in 0 dev or 1 prod as arguments to the run command. That way you don't accidentally start a prod container in dev or a dev container in prod.

您可能希望完全省略CMD,并始终将0 dev或1 prod作为run命令的参数传递给它。这样,您就不会意外地在dev中启动一个prod容器,或者在prod中启动一个dev容器。

#3


4  

The typical way to do this in Docker containers is to pass in environment variables:

在Docker容器中这样做的典型方法是传入环境变量:

docker run -p 9000:9000 -e NODE_ENV=dev -e CLUSTER=0 -d me/app

推荐阅读
  • docker-logs6000tcp,0.0.0.0:6000-80tcpvigilant_mclean尝试使用URLhttp:0.0.0.0:60 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • uniapp开发H5解决跨域问题的两种代理方法
    本文介绍了uniapp开发H5解决跨域问题的两种代理方法,分别是在manifest.json文件和vue.config.js文件中设置代理。通过设置代理根域名和配置路径别名,可以实现H5页面的跨域访问。同时还介绍了如何开启内网穿透,让外网的人可以访问到本地调试的H5页面。 ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
  • 本文介绍了H5游戏性能优化和调试技巧,包括从问题表象出发进行优化、排除外部问题导致的卡顿、帧率设定、减少drawcall的方法、UI优化和图集渲染等八个理念。对于游戏程序员来说,解决游戏性能问题是一个关键的任务,本文提供了一些有用的参考价值。摘要长度为183字。 ... [详细]
  • 本文详细介绍了在Centos7上部署安装zabbix5.0的步骤和注意事项,包括准备工作、获取所需的yum源、关闭防火墙和SELINUX等。提供了一步一步的操作指南,帮助读者顺利完成安装过程。 ... [详细]
  • 以管理员身份打开命令行粘贴上面 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文介绍了高校天文共享平台的开发过程中的思考和规划。该平台旨在为高校学生提供天象预报、科普知识、观测活动、图片分享等功能。文章分析了项目的技术栈选择、网站前端布局、业务流程、数据库结构等方面,并总结了项目存在的问题,如前后端未分离、代码混乱等。作者表示希望通过记录和规划,能够理清思路,进一步完善该平台。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 我有dockerfile和docker-compose。当我写docker-composeup时出现错误。我的应用程序目录为wwwdjango-fo ... [详细]
author-avatar
飞翔1
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有