Dockerfile 多阶段构建参数传递

Dockerfile 多阶段构建参数传递

dockerfile

 
  
ARG SVC1
ARG SVC2

##### BUILD STAGE
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS BUILD
ARG SVC1

WORKDIR /workspace

RUN mkdir ${SVC1}

##### RUN STAGE
FROM mcr.microsoft.com/dotnet/aspnet:3.1
ARG SVC2
ENV SVC2=${SVC2}

COPY --from=BUILD /workspace /workspace

WORKDIR /workspace

RUN mkdir ${SVC2}

  • ARG 声明传入的参数
  • FROM 分隔了不同的构建阶段
  • 不同构建阶段必须先声明 ARG 后才能使用

  • COPY --from=BUILD 从 BUILD 阶段拷贝
ARG SVC
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS BUILD
ARG SVC
...
FROM mcr.microsoft.com/dotnet/aspnet:3.1
ARG SVC
...

docker build 构建镜像

docker build -t test --build-arg SVC1="text1" --build-arg SVC2="text2" .

通过 --build-arg KEY="VAL" 传入参数

docker run 启动容器并进入shell

[root@localhost ~]# docker run -it test /bin/bash

检查容器查看结果

检查目录是否被创建

root@0cc3f0c70904:/workspace# ls

text1 text2            ##### BUILD STAGE & RUN STAGE 创建的东西都在

检查环境变量是否被创建

root@0cc3f0c70904:/workspace# env
HOSTNAME=0cc3f0c70904
ASPNETCORE_URLS=http://+:80
PWD=/workspace
HOME=/root
TERM=xterm
SHLVL=1
SVC2=text2							##### 环境变量OK
DOTNET_RUNNING_IN_CONTAINER=true
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

你可能感兴趣的:(Docker,运维)