简体   繁体   English

使用环境变量构建Docker映像

[英]Build the docker image with environment variables

so the objective is to have a different image for prod and testing so there are certain variables change accordingly so I need to set env variables during the build. 因此,目标是为产品和测试提供不同的图像,因此某些变量会相应更改,因此我需要在构建过程中设置env变量。

# Dockerfile
ENV Somename: $value
...

docker build --build-arg Somename=value -t test .

docker run -d -p port:port test

this work flow is not taking the env variables 这个工作流程没有采用env变量

First you need to consume the build-arg inside you dockerfile using the ARG command. 首先,您需要使用ARG命令使用dockerfile内部的build-arg。

FROM alpine

# consume the build arg    
ARG somename  

# persist the env variable in the built image
ENV somename=$somename  

# somename will appear as an env variable
RUN echo $somename 
RUN env

And running the build command docker build --build-arg somename=hello . 并运行构建命令docker build --build-arg somename=hello . will show you an env variable somename=hello 将显示一个环境变量somename=hello

your syntax is not correct, do not put 你的语法不正确,不要放

:

it is either 要么是

ENV somename somevalue

or 要么

ENV somename=somevalue

Check the doc 检查文件

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

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

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