简体   繁体   English

将 filePath 作为变量传递给 dockerfile _ nodeJS dockerode Docker

[英]pass filePath to dockerfile as variable _ nodeJS dockerode Docker

In my case, I am creating a config.json that I need to copy from the host to my container.就我而言,我正在创建一个 config.json,我需要将它从主机复制到我的容器。

I figured out there is some option that I can pass args to my dockerfile.我发现有一些选项可以将 args 传递给我的 dockerfile。 so first step is :所以第一步是:

1.create Dockerfile: 1.创建Dockerfile:

FROM golang
WORKDIR /go/src/app
COPY . .                     /* here we have /foo directory */
COPY  $CONFIG_PATH ./foo/
EXPOSE $PORT
CMD ["./foo/run", "-config", "./foo/config.json"]

as you can see, I have 2 variable [ "$CONFIG_PATH", "$PORT"].如您所见,我有 2 个变量 ["$CONFIG_PATH", "$PORT"]。

so these to variables are dynamic and comes from my command in docker run .所以这些变量是动态的,来自我在docker run 中的命令。 here I need to copy my config file from my host to my container, and I need to run my project with that config.json file.在这里,我需要将我的配置文件从我的主机复制到我的容器,并且我需要使用该config.json文件运行我的项目。

after building image:构建镜像后:

  1. second step:第二步:

get my config file from user and run the docker image with these variables.从用户那里获取我的配置文件并使用这些变量运行 docker 镜像。

let configFilePath = '/home/baazz/baaaf/config.json'
let port = "8080"
docker.run('my_image', null, process.stdout, { Env: [`$CONFIG_PATH=${configFilePath}`, `$PORT=${port}`] }).then(data => {

        }).catch(err => { console.log(err) })

I am getting this error message when I am trying to execute my code.当我尝试执行我的代码时收到此错误消息。

Error opening JSON configuration (./foo/config.json): open ./foo/config.json: no such file or directory .打开 JSON 配置 (./foo/config.json) 时出错:打开 ./foo/config.json: no such file or directory 。 Terminating.终止。

You generally don't want to COPY configuration files like this in your Docker image.您通常不想在 Docker 映像中COPY配置文件。 You should be able to docker run the same image in multiple environments without modification.您应该能够在多个环境中docker run相同的图像而无需修改。

Instead, you can use the docker run -v option to inject the correct config file when you run the image:相反,您可以使用docker run -v选项在运行映像时注入正确的配置文件:

docker run -v $PWD/config-dev.json:/go/src/app/foo/config.json my_image

(The Dockerode home page shows an equivalent Binds option. In Docker Compose, this goes into the per-container volumes: . There's no requirement that the two paths or file names match.) (该Dockerode主页上显示了相当的Binds选项,在泊坞撰写,该进入每个容器。 volumes: 。有没有要求,这两个路径或文件名匹配)

Since file paths like this become part of the external interface to how people run your container, you generally don't want to make them configurable at build time.由于像这样的文件路径成为人们如何运行容器的外部接口的一部分,因此您通常希望在构建时使它们可配置。 Pick a fixed path and document that that's the place to mount your custom config file.选择一个固定路径和文档,这是安装自定义配置文件的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM