如何在 dockerfile 中使用 CMD 内部的变量? [英] How to use variable inside of CMD in dockerfile?

查看:24
本文介绍了如何在 dockerfile 中使用 CMD 内部的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如您在这个 Dockerfile 中看到的,我确实在构建时将 PORT 号作为 --build-arg 传递.现在我需要运行 npx next start -p ${PORT}:

As you can see in this Dockerfile, I do pass the PORT number as --build-arg at buildtime. Now I need to run npx next start -p ${PORT}:

FROM node:16.6.1-alpine3.14
RUN apk add dumb-init

ARG PORT

EXPOSE $PORT
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["npx", "next", "start", "-p", "echo ${PORT}"]

但这不起作用.该应用程序在默认的 3000 端口上运行.如果我这样做了

But this is not working. The app is running at the default 3000 port. If I do

CMD ["npx", "next", "start", "-p", "3100"]

应用程序按预期运行在 3100.但是为什么我不能使用变量呢?

The app is running at 3100 as expected. But why can't I use a variable?

推荐答案

您使用的格式 (exec) 不起作用.来自 docs:

The format you are using (exec) won't work. From the docs:

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

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

相反,您可以直接执行 shell:

Instead, you can execute a shell directly:

CMD ["sh", "-c", "npx next start -p $PORT"]

这篇关于如何在 dockerfile 中使用 CMD 内部的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆