I'm trying to create a nanoserver w/ nodejs base image, but I can't seem to get the ARG (or ENV) command to work properly.
My docker file:
FROM microsoft/nanoserver
ENV NODE_VERSION=8.11.4
ADD https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-win-x64.zip C:\\build\\node-v${NODE_VERSION}-win-x64.zip
RUN powershell -Command Expand-Archive C:\build\node-v${NODE_VERSION}-win-x64.zip C:\; Rename-Item C:\node-v${NODE_VERSION}-win-x64 node
RUN SETX PATH C:\node
ENTRYPOINT C:\node\node.exe
Build command:
docker build . -t base-image:latest
It downloads the zip file, but when it tries to rename the downloaded file it throws an error:
Expand-Archive : The path 'C:\build\node-v-win-x64.zip' either does not exist
or is not a valid file system path.
According to the ENV documentation:
Environment variables are supported by the following list of
instructions in the Dockerfile:
ADD COPY ENV EXPOSE FROM LABEL STOPSIGNAL USER VOLUME WORKDIR as well
as:
ONBUILD (when combined with one of the supported instructions above)
Therefore it appears variables defined with ENV are not supported by the RUN directive.
However, you can instead replace the ENV directive with the ARG directive and NODE_VERSION will be availablein subsequent RUN directives.
Example:
FROM microsoft/nanoserver
ARG NODE_VERSION=8.11.4
ADD https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-win-x64.zip C:\\build\\node-v${NODE_VERSION}-win-x64.zip
RUN powershell -Command Expand-Archive C:\build\node-v${NODE_VERSION}-win-x64.zip C:\; Rename-Item C:\node-v${NODE_VERSION}-win-x64 node
RUN SETX PATH C:\node
ENTRYPOINT C:\node\node.exe
Additionally you can override the value of NODE_VERSION in your docker build command.
$ docker build -t base-image:latest --build-arg NODE_VERSION=10.0.0 .
Using the ARG directive will not make NODE_VERSION available in the environment of a running container. Depending on your use case you may also need to use an additional ENV definition.
Found an answer here:
https://github.com/docker/for-win/issues/542
Essentially - within the powershell commands the %VARIABLE_NAME% format has to be used:
FROM microsoft/nanoserver
ENV NODE_VERSION=8.11.4
ADD https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-win-x64.zip C:\\build\\node-v${NODE_VERSION}-win-x64.zip
RUN powershell -Command Expand-Archive C:\build\node-v%NODE_VERSION%-win-x64.zip C:\; Rename-Item C:\node-v%NODE_VERSION%-win-x64 node
RUN SETX PATH C:\node
ENTRYPOINT C:\node\node.exe
Related
I have a Dockerfile:
...
COPY ${JAR_FILENAME}-*.jar /${JAR_FILENAME}.jar
...
CMD java -jar $(echo /${JAR_FILENAME}.jar)
And I am building the image with: docker build --build-arg JAR_FILENAME='some-file' -t some-name:tag
But when I run the container, I get:
Error: Unable to access jarfile /.jar
How do I pass the environment variables during the build time to my CMD command so that I can run java -jar some-file.jar?
You use an ENV, setting it to the value of the ARG. An ENV is nearly identical to an ARG, but gets saved to the image metadata and therefore persists to when you run the container.
...
ARG JAR_FILENAME=app.jar
ENV JAR_FILENAME=${JAR_FILENAME}
COPY ${JAR_FILENAME}-*.jar /${JAR_FILENAME}.jar
...
CMD java -jar /${JAR_FILENAME}.jar
Note that users of the image can alter ENV values, and it may be easier to just set the jar in the image to a well known name:
...
COPY ${JAR_FILENAME}-*.jar /app.jar
...
CMD java -jar /app.jar
Use the ARG construct in Docker file to pass in build time arguments from command line.
Read the docs at : https://docs.docker.com/engine/reference/builder/#arg
I have a dockerfile with these lines:
ARG ENVIRONMENT
ENV ENVIRONMENT $ENVIRONMENT
RUN npm run ng build --configuration=${ENVIRONMENT}
I cant get the "RUN npm run ng build --configuration= to pass the value of $ENVIRONMENT to the npm command.
What is the syntax for this?
Per the Dockerfile ARG docs,
The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg = flag.
in order to accept an argument as part of the build, we use --build-arg.
Dockerfile ENV docs:
The ENV instruction sets the environment variable to the value .
We also need to include an ENV statement because the CMD will be executed after the build is complete, and the ARG will not be available.
FROM busybox
ARG ENVIRONMENT
ENV ENVIRONMENT $ENVIRONMENT
CMD echo $ENVIRONMENT
will cause an environment variable to be set in the image, so that it is available during a docker run command.
docker build -t test --build-arg ENVIRONMENT=awesome_environment .
docker run -it test
This will echo awesome_environment.
Try changing your RUN command do this:
RUN npm run ng build --configuration=$ENVIRONMENT
This should work. Check here
Thanks.
I use Docker Toolbox for windows (for compatibility issues) and in the Dockerfile I specify an ARG so that I can use it when building the image with --build-arg command. Inside the dockerfile I also have some COPY commands and there I would like to use my variable but when I run docker build --build-arg VERSION_APP=something . it does not translate the variable . I have already used $VERSION_APP or ${VERSION_APP} or %VERSION_APP%.
FROM alpine
MAINTAINER Marinos
ARG VERSION_APP
RUN apk update && apk add dos2unix
COPY script.sh /home/script.sh
RUN chmod a+x /home/script.sh
RUN dos2unix /home/script.sh
RUN sh /home/script.sh
COPY installation.txt /home/Desktop/${VERSION_APP}
UPDATE
It seems that you should pass the whole path to the variable you use that is how I got it working.
If you actually use the command below then it is expected not to work because the argument called VERSION_APP
docker build --build-arg myVar=something
So the command should be
docker build --build-arg VERSION_APP=something
And in Dockerfile it should be %VERSION_APP% also you may need to use ENV like below:
ARG VERSION_APP
ENV VERSION_APP ${VERSION_APP}
I'm trying to use docker and docker-compose to build a set of containers with some customized parameters using environment variables - for example, I want to mount a directory at a specific location in the container. This specific location is stored in an environment variable and created in the Dockerfile using a command like:
RUN mkdir $custom_location
I keep the parameters in an .env file which populates the docker-compose file.
Once the containers are running, using the printenv command I can see that the transfer of the env variables has worked and I can manually run the command
mkdir $custom_location but the command hasn't been successful during the dockerfile build process.
A summarized version of the Dockerfile looks like this:
FROM python:3.6.5 as my_base
ENV CUSTOM_PATH=$CUSTOM_PATH
RUN mkdir $CUSTOM_PATH
A summarized version of the docker-compose.yml looks like this:
service:
environment:
- CUSTOM_PATH=${CUSTOM_PATH}
build: ./Docker
The error I'm getting looks like this:
Step 20/20 : RUN mkdir $CUSTOM_PATH ---> Running in 437aa3f09dbb
mkdir: missing operand Try 'mkdir --help' for more information. The
command '/bin/sh -c mkdir $CUSTOM_PATH' returned a non-zero code: 1
Why would this be?
The ENV variables are successfully in the running container. Using printenv I can see them.
You may successfully use ARG with ENV in your Dockerfile. See the docs for this:
You can use an ARG or an ENV instruction to specify variables that are available to the RUN instruction. Environment variables defined using the ENV instruction always override an ARG instruction of the same name. Consider this Dockerfile with an ENV and ARG instruction.
FROM ubuntu
ARG CONT_IMG_VER
ENV CONT_IMG_VER ${CONT_IMG_VER:-v1.0.0}
RUN echo $CONT_IMG_VER
So, in your case it should be
FROM python:3.6.5 as my_base
ARG CUSTOM_PATH=<some default path if you wish>
ENV CUSTOM_PATH=$CUSTOM_PATH
RUN mkdir $CUSTOM_PATH
After that you may build the image with docker build --build-arg CUSTOM_PATH=/var/log/whatever . and it should work.
Using the following docker file I am getting an error on RUN md $SiteFolderPath and I am not sure why every example I look at:
https://docs.docker.com/engine/reference/builder/#environment-replacement
Indicates I am doing it correctly.
FROM microsoft/iis
#FROM nanoserver/iis
SHELL ["powershell"]
ENV SiteFolderPath c:\\app
ENV SiteFolderPathLogs c:\\app\\logs
ENV WorkFolder /app
ENV SiteAppPool LocalAppPool
ENV SiteName LocalWebSite
ENV SiteHostName LocalSite
RUN md $SiteFolderPath
RUN md $SiteFolderPathLogs
WORKDIR $WorkFolder
COPY ./Public .
RUN New-Item $SiteFolderPath -type Directory
RUN Set-Content $SiteFolderPath\Default.htm "<h1>Hello IIS</h1>"
RUN New-Item IIS:\AppPools\$SiteAppPool
RUN New-Item IIS:\Sites\$SiteName -physicalPath $SiteFolderPath -bindings #{protocol="http";bindingInformation=":80:"+$SiteHostName}
RUN Set-ItemProperty IIS:\Sites\$SiteName -name applicationPool -value $SiteAppPool
EXPOSE 80
EXPOSE 443
VOLUME ${SiteFolderPathLogs}
I get an error message when building the docker file:
mkdir : Cannot bind argument to parameter 'Path' because it is null.
The page you linked to states:
Environment variables are supported by the following list of instructions in the Dockerfile:
ADD
COPY
ENV
EXPOSE
FROM
LABEL
STOPSIGNAL
USER
VOLUME
WORKDIR
as well as:
ONBUILD (when combined with one of the supported instructions above)
Since you are using the variables as part of a RUN block, you should use the Windows environment variable syntax: %SiteFolderPath%