Dockerfile,ARG和ENV无法正常工作

时间:2018-04-06 07:36:24

标签: docker

我有以下Dockerfile

#Copyright (c) 2014-2017 Oracle and/or its affiliates. All rights reserved.
#
#Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
#
# ORACLE DOCKERFILES PROJECT
# --------------------------
# This Dockerfile extends the Oracle WebLogic image by creating a sample domain.
#
# Util scripts are copied into the image enabling users to plug NodeManager 
# automatically into the AdminServer running on another container.
#
# HOW TO BUILD THIS IMAGE
# -----------------------
# Put all downloaded files in the same directory as this Dockerfile
# Run: 
#      $ sudo docker build -t 12213-domain
#
# Pull base image
# ---------------
FROM oracle/weblogic:12.2.1.3-developer

# Maintainer
# ----------
MAINTAINER Monica Riccelli <monica.riccelli@oracle.com>

ARG DOMAIN_NAME
ARG ADMIN_PORT
ARG ADMIN_NAME
ARG ADMIN_USERNAME
ARG ADMIN_PASSWORD

# WLS Configuration 
# ---------------------------
ENV ADMIN_HOST="wlsadmin" \
    NM_PORT="5556" \
    MS_PORT="8001" \
    DEBUG_PORT="8453" \
    ORACLE_HOME=/u01/oracle \
    SCRIPT_FILE=/u01/oracle/createAndStartWLSDomain.sh \
    CONFIG_JVM_ARGS="-Dweblogic.security.SSL.ignoreHostnameVerification=true"  \
    PATH=$PATH:/u01/oracle/oracle_common/common/bin:/u01/oracle/wlserver/common/bin:/u01/oracle/user_projects/domains/${DOMAIN_NAME:-base_domain}/bin:/u01/oracle

# Domain and Server environment variables
# ------------------------------------------------------------
ENV DOMAIN_NAME="${DOMAIN_NAME}" \
    PRE_DOMAIN_HOME=/u01/oracle/user_projects \
    ADMIN_PORT="${ADMIN_PORT}"  \
    ADMIN_USERNAME="${ADMIN_USERNAME}" \
    ADMIN_NAME="${ADMIN_NAME}" \
    MS_NAME="${MS_NAME:-""}" \
    NM_NAME="${NM_NAME:-""}" \
    ADMIN_PASSWORD="${ADMIN_PASSWORD}" \
    CLUSTER_NAME="${CLUSTER_NAME:-DockerCluster}" \
    DEBUG_FLAG=true \
    PRODUCTION_MODE=dev 

# Add files required to build this image
COPY container-scripts/* /u01/oracle/

#Create directory where domain will be written to
USER root
RUN chmod +xw /u01/oracle/*.sh && \ 
    chmod +xw /u01/oracle/*.py && \ 
    mkdir -p $PRE_DOMAIN_HOME && \ 
    chmod a+xr $PRE_DOMAIN_HOME && \
    chown -R oracle:oracle $PRE_DOMAIN_HOME

VOLUME $PRE_DOMAIN_HOME
# Expose Node Manager default port, and also default for admin and managed server 
EXPOSE $NM_PORT $ADMIN_PORT $MS_PORT $DEBUG_PORT

USER oracle
WORKDIR $ORACLE_HOME

# Define default command to start bash. 
CMD ["/u01/oracle/createAndStartWLSDomain.sh"]

我使用命令构建

#!/bin/sh
#
#Copyright (c) 2014-2017 Oracle and/or its affiliates. All rights reserved.
#
#Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
#
docker build -t 12213-domain \
--no-cache \
--build-arg DOMAIN_NAME=domain \
--build-arg ADMIN_PORT=7001 \
--build-arg ADMIN_NAME=admin \
--build-arg ADMIN_USERNAME=wlsuser \
--build-arg ADMIN_PASSWORD=wlsuser1 \
. 

获取以下构建日志

$ ./build.sh
Sending build context to Docker daemon  51.71kB
Step 1/17 : FROM oracle/weblogic:12.2.1.3-developer
 ---> 15ba3f59a9f9
Step 2/17 : MAINTAINER Monica Riccelli <monica.riccelli@oracle.com>
 ---> Running in ac70adb36a4b
Removing intermediate container ac70adb36a4b
 ---> fe34e24ffce7
Step 3/17 : ARG DOMAIN_NAME
 ---> Running in 073a89d7613c
Removing intermediate container 073a89d7613c
 ---> de10930a27d6
Step 4/17 : ARG ADMIN_PORT
 ---> Running in d213833315c2
Removing intermediate container d213833315c2
 ---> 9af410c46028
Step 5/17 : ARG ADMIN_NAME
 ---> Running in 2baee277da54
Removing intermediate container 2baee277da54
 ---> a76f3f3d6642
Step 6/17 : ARG ADMIN_USERNNAME
 ---> Running in 9127852dae20
Removing intermediate container 9127852dae20
 ---> bb9af74b5804
Step 7/17 : ARG ADMIN_PASSWORD
 ---> Running in 4d0b1969605b
Removing intermediate container 4d0b1969605b
 ---> af18d5b6be2d
Step 8/17 : ENV ADMIN_HOST="wlsadmin"     NM_PORT="5556"     MS_PORT="8001"
DEBUG_PORT="8453"     ORACLE_HOME=/u01/oracle     SCRIPT_FILE=/u01/oracle/create
AndStartWLSDomain.sh     CONFIG_JVM_ARGS="-Dweblogic.security.SSL.ignoreHostname
Verification=true"      PATH=$PATH:/u01/oracle/oracle_common/common/bin:/u01/ora
cle/wlserver/common/bin:/u01/oracle/user_projects/domains/${DOMAIN_NAME:-base_do
main}/bin:/u01/oracle
 ---> Running in 449a28590d90
Removing intermediate container 449a28590d90
 ---> 2a1ddd961d5c
Step 9/17 : ENV DOMAIN_NAME="${DOMAIN_NAME}"     PRE_DOMAIN_HOME=/u01/oracle/use
r_projects     ADMIN_PORT="${ADMIN_PORT}"      ADMIN_USERNAME="${ADMIN_USERNAME}
"     ADMIN_NAME="${ADMIN_NAME}"     MS_NAME="${MS_NAME:-""}"     NM_NAME="${NM_
NAME:-""}"     ADMIN_PASSWORD="${ADMIN_PASSWORD}"     CLUSTER_NAME="${CLUSTER_NA
ME:-DockerCluster}"     DEBUG_FLAG=true     PRODUCTION_MODE=dev
 ---> Running in 0b01881a1ca4
Removing intermediate container 0b01881a1ca4
 ---> 7a3cd53ea5a3
Step 10/17 : COPY container-scripts/* /u01/oracle/
 ---> ce67247c3f7e
Step 11/17 : USER root
 ---> Running in 61adcafc1226
Removing intermediate container 61adcafc1226
 ---> f9a781fda963
Step 12/17 : RUN chmod +xw /u01/oracle/*.sh &&     chmod +xw /u01/oracle/*.py &&
     mkdir -p $PRE_DOMAIN_HOME &&     chmod a+xr $PRE_DOMAIN_HOME &&     chown -
R oracle:oracle $PRE_DOMAIN_HOME
 ---> Running in 82b7b258d6f1
Removing intermediate container 82b7b258d6f1
 ---> 0cda254bc640
Step 13/17 : VOLUME $PRE_DOMAIN_HOME
 ---> Running in 6650ff8092d3
Removing intermediate container 6650ff8092d3
 ---> c469ff0ac9a2
Step 14/17 : EXPOSE $NM_PORT $ADMIN_PORT $MS_PORT $DEBUG_PORT
 ---> Running in a551b6bd5363
Removing intermediate container a551b6bd5363
 ---> 08253c4d94bd
Step 15/17 : USER oracle
 ---> Running in f1e6b4e482e9
Removing intermediate container f1e6b4e482e9
 ---> 85a75641e866
Step 16/17 : WORKDIR $ORACLE_HOME
Removing intermediate container a2b75ecbb0b6
 ---> 0124a8251ce3
Step 17/17 : CMD ["/u01/oracle/createAndStartWLSDomain.sh"]
 ---> Running in 1455fdc0d39d
Removing intermediate container 1455fdc0d39d
 ---> c1c947c2816c
[Warning] One or more build-args [ADMIN_USERNAME] were not consumed
Successfully built c1c947c2816c
Successfully tagged 12213-domain:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Win
dows Docker host. All files and directories added to build context will have '-r
wxr-xr-x' permissions. It is recommended to double check and reset permissions f
or sensitive files and directories.

注意消息

[Warning] One or more build-args [ADMIN_USERNAME] were not consumed

当我启动容器并打印环境时

FMW_PKG=fmw_12.2.1.3.0_wls_quick_Disk1_1of1.zip
CONFIG_JVM_ARGS=-Dweblogic.security.SSL.ignoreHostnameVerification=true
HOSTNAME=74adf82e8092
PRODUCTION_MODE=dev
TERM=xterm
ADMIN_NAME=AdminServer
CLUSTER_NAME=DockerCluster
FMW_JAR=fmw_12.2.1.3.0_wls_quick.jar
USER_MEM_ARGS=-Djava.security.egd=file:/dev/./urandom
DEBUG_FLAG=true
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd
=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;4
2:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:
*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=0
1;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;
31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=0
1;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sa
r=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:
*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01
;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.ti
ff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;3
5:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp
4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:
*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01
;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=
01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.f
lac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;
36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
MS_NAME=
DOMAIN_NAME=base_domain
SCRIPT_FILE=/u01/oracle/createAndStartWLSDomain.sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/java/defa
ult/bin:/u01/oracle/oracle_common/common/bin:/u01/oracle/wlserver/common/bin:/u0
1/oracle/oracle_common/common/bin:/u01/oracle/wlserver/common/bin:/u01/oracle/us
er_projects/domains/base_domain/bin:/u01/oracle
ADMIN_HOST=wlsadmin
DOMAIN_HOME=/u01/oracle/user_projects/domains/base_domain
NM_NAME=
PWD=/u01/oracle
DEBUG_PORT=8453
ADMIN_PORT=7001
JAVA_HOME=/usr/java/default
JAVA_PKG=server-jre-8u*-linux-x64.tar.gz
SHLVL=1
HOME=/u01/oracle
ADMIN_USERNAME=weblogic
NM_PORT=5556
PRE_DOMAIN_HOME=/u01/oracle/user_projects
ADMIN_PASSWORD=
ORACLE_HOME=/u01/oracle
MS_PORT=8001
_=/usr/bin/env

未正确设置环境变量,但使用基础映像中的环境变量。我想覆盖那些变量。

修改

问题是--build-arg未将参数传递给ARG,或ARG未正确绑定到ENV。如果我在ENV中使用常量字符串,则会正确覆盖环境。更有趣的是,如果我没有将--build-arg传递给那些ARG变量,我就不会收到任何警告或错误,仍然可以获得完全相同的构建日志。 AFAIK,未绑定的ARG没有默认值会引发错误。

这是一个错误吗?我在windows上使用docker-toolbox,这是docker版本

docker-machine.exe version 0.14.0, build 89b8332
Docker version 18.03.0-ce, build 0520e24302

1 个答案:

答案 0 :(得分:1)

来自docs.docker.com

  

始终使用ENV指令定义的环境变量   覆盖同名的ARG指令

因此,解决此问题的唯一方法是为ARG变量使用不同的名称。

您的ARG声明中有拼写错误。

我认为改为ADMIN_USERNAME会解决您的问题。