使用Docker compose传递Dockerfile的参数 [英] Passing arguments for Dockerfiles using Docker compose

查看:129
本文介绍了使用Docker compose传递Dockerfile的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在构建阶段对我的dockerfile进行参数化,并在Docker-compose中使用参数。例如,在Docker compose中,我定义了一个名为bpp的服务,如下所示:

I'm trying to use parametrize my dockerfiles on build phase and use arguments in Docker-compose. For example in Docker compose I have defined one service called bpp as following:

bpp:
  build:
    context: .
    dockerfile: Dockerfile.bpp
    args:
      gp : 8080
  image: serv/bpp
  restart: always
  depends_on:
    - data
  links:
    - data

我正在尝试传递名为gp的参数到Dockerfile.bpp,在启动Python应用程序时使用参数,公开端口等。
例如,在dockerfile.bpp中,尝试公开端口gp如下:

I'm trying to pass argument named gp to Dockerfile.bpp, where I'm using argument when starting a Python application, exposing a port etc. For example in dockerfile.bpp in trying to expose port gp as following:

EXPOSE gp

但是,在构建时命令 docker-compose build 的docker文件我收到以下错误:
错误:服务'bpp'生成失败:无效的containerPort:gp

However, when building docker file by command docker-compose build I get following error: ERROR: Service 'bpp' failed to build: Invalid containerPort: gp

似乎参数gp在dockerfile中不可见。

It seems that argument gp is not visible in dockerfile. Any suggestions?

推荐答案

您需要将 ARG gp 添加到Dockerfile中。

You need to add ARG gp to your Dockerfile.

...
ARG gp

EXPOSE $gp
...

https://docs.docker.com/engine/reference/builder/#arg

值得一提的是这不会为了在通过compose运行端口时公开端口,您需要为此在docker-compose.yml中添加 ports 指令。

Worth mentioning that this isn't going to expose the port when you're running it via compose though, you would need to add a ports instruction to your docker-compose.yml for that.

这篇关于使用Docker compose传递Dockerfile的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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