Dockerfile: pass argument to RUN npm run build - docker

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.

Related

ENV vars for docker build in multi-stage build

I have a multi-stage build where a python script runs in the first stage and uses several env vars.
How do I set these variables in the docker build command?
Here's the Dockerfile:
FROM python:3 AS exporter
RUN mkdir -p /opt/export && pip install mysql-connector-python
ADD --chmod=555 export.py /opt/export
CMD ["python", "/opt/export/export.py"]
FROM nginx
COPY --from=exporter /tmp/gen/* /usr/share/nginx/html
My export.py script reads several env vars, and I have a .env file. If I run a container built with teh first stage and pass --env-file it works, but I can't seem to get it to work in the build stage.
How can I get the env vars to be available when building the first stage?
I don't care if they are saved in the image or not...
its seens you are looking for the ARG instruction. it's only avaible at the building time and won't be avaible at image runtime. Don’t use them for secrets which are not meant to stick around!
# default value if not using --build-arg instruction
ARG GLOBAL_AVAILABLE=iamglobal
FROM python:3 AS exporter
RUN mkdir -p /opt/export && pip install mysql-connector-python
ADD --chmod=555 export.py /opt/export
ARG GLOBAL_AVAILABLE
ENV GLOBAL_AVAILABLE=$GLOBAL_AVAILABLE
# only visible at exporter build stage:
ARG LOCAL_AVAILABLE=aimlocal
# multistage visible:
RUN echo ${GLOBAL_AVAILABLE}
# local stage visible (exporter build stage):
RUN echo ${LOCAL_AVAILABLE}
CMD ["python", "/opt/export/export.py"]
FROM nginx
COPY --from=exporter /tmp/gen/* /usr/share/nginx/html
you can pass custom ARG values by using the --build-arg flag:
docker build -t <image-name>:<tag> --build-arg GLOBAL_AVAILABLE=abc .
the general format to pass multiple args is:
docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .
some refs:
https://docs.docker.com/engine/reference/builder/
https://blog.bitsrc.io/how-to-pass-environment-info-during-docker-builds-1f7c5566dd0e
https://vsupalov.com/docker-arg-env-variable-guide/

How do I make Timeout command from entrypoint in Dockerfile

I have been trying to run timeout command on my shellscript with time being passed as variabe through Dockerfile
This is my DokcerFile(sample)
FROM locustio/locust:1.2.3
ARG TIME_CHECK=15
COPY --chown=locust:locust ping.sh .
RUN echo "Hello $TIME_CHECK"
RUN chown locust:locust /home/locust && chmod +x ./ping.sh
ENTRYPOINT ["/bin/bash","-c", "timeout $TIME_CHECK ./ping.sh"]
Docker build happens successfully with below command and I can the value being passed correctly
docker build -t pingit --build-arg TIME_CHECK=10
When I do docker run it fails with following error
Try 'timeout --help' for more information.
I do understand this is because ENTRYPOINT is not recogninsing variable as such.
What am I doing wrong can you anyone help me here.
From docker reference, you can only access ARG values at build-time.
To pass a value to the runtime environment variable, you can use ENV instruction:
FROM locustio/locust:1.2.3
ARG TIME_CHECK=15
# assign ENV from ARG
ENV TIME_CHECK=${TIME_CHECK}
COPY --chown=locust:locust ping.sh .
RUN echo "Hello $TIME_CHECK"
RUN chown locust:locust /home/locust && chmod +x ./ping.sh
ENTRYPOINT ["/bin/bash","-c", "timeout $TIME_CHECK ./ping.sh"]
Just add a ENV TIME_CHECK=$TIME_CHECK to your dockerfile after the ARG statement.
The issue is that ARG is at build time. If you need to use it as environment variable then you need to set the variable yourself

How to verify build args is set properly in Dockerfile

I've got the following line in Dockerfile:
ARG COOL_ID
...
ENTRYPOINT ["java", "-jar", "/usr/share/java/${COOL_ID}/app.jar"]
but when I run it there's an error:
Error: Unable to access jarfile /usr/share/java//app.jar
and I can see that my ${COOL_ID} argument was not formatted correctly.
How can I fix it?
It will not substitute the variable, as Docker treat ENTRYPOINT and CMD as a command so processing variables like shell will not work. Try to change the CMD to run it as a shell and then you will able to process variable the same as the shell.
Also, you can not use ARG in CMD to treat them as an environment variable, you just use them inside Dockerfile, to use them as an environment variable you must assign them to some ENV.
ARG COOL_ID
ENV COOL_ID=$COOL_ID
I will also suggest to verify and check COOL_ID in Docker build time, if not set then it should print the warning or error to the user, see below example if ARG not passed to builds params then it will print an error message to the user.
ARG COOL_ID
#see ARG is for build time
RUN if [ -z $COOL_ID ];then \
>&2 echo "\n****************Warning!!!!*************\n"; \
>&2 echo "COOL_ID seems empty!" ;\
fi
ENV COOL_ID=$COOL_ID
# ENV is for run time
CMD ["sh", "-c", "java -jar /usr/share/java/${COOL_ID}/app.jar"]
Now build the docker with --build-arg
docker build --build-arg COOL_ID=myid -t myjava .
If you missed passing COOL_ID you will be notified.

Docker ARG or ENV not working as expected in Dockerfile

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}

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.
# 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
First you need to consume the build-arg inside you dockerfile using the ARG command.
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 . will show you an env variable 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

Resources