pass arguments from docker run to dockerfile - docker

Below one is my docker build command
docker build -t test/magento2:1.0.0 --build-arg BASE_URL=http://www.hostname.net/ --build-arg DATABASE_HOST=localhost --build-arg DATABASE_NAME=magento --build-arg DATABASE_USER=root --build-arg DATABASE_PASSWORD=root --build-arg ADMIN_USERNAME=test --build-arg ADMIN_FIRSTNAME=test --build-arg ADMIN_LASTNAME=Mobi --build-arg ADMIN_EMAIL=support#test.mobi --build-arg ADMIN_PASSWORD=test#123 --build-arg DEFAULT_LANGUAGE=en_US --build-arg DEFAULT_CURRENCY=INR --build-arg DEFAULT_TIMEZONE=Asia/Kolkata --build-arg BACKEND_FRONTNAME=admin .
Dockerfile
ARG BASE_URL
ARG DATABASE_HOST
ARG DATABASE_NAME
ARG DATABASE_USER
ARG DATABASE_PASSWORD
ARG ADMIN_USERNAME
ARG ADMIN_FIRSTNAME
ARG ADMIN_LASTNAME
ARG ADMIN_EMAIL
ARG ADMIN_PASSWORD
ARG DEFAULT_LANGUAGE
ARG DEFAULT_CURRENCY
ARG DEFAULT_TIMEZONE
ARG BACKEND_FRONTNAME
RUN service mysql start && \
cd /var/www/html && php bin/magento setup:install --base-url=$BASE_URL --db-host=$DATABASE_HOST --db-name=$DATABASE_NAME --db-user=$DATABASE_USER --db-password=$DATABASE_PASSWORD --admin-firstname=$ADMIN_FIRSTNAME --admin-lastname=$ADMIN_LASTNAME --admin-email=$ADMIN_EMAIL --admin-user=$ADMIN_USERNAME --admin-password=$ADMIN_PASSWORD --language=$DEFAULT_LANGUAGE --currency=$DEFAULT_CURRENCY --timezone=$DEFAULT_TIMEZONE --use-rewrites=1 --backend-frontname=$BACKEND_FRONTNAME
It is working fine, but I'm looking something
docker run <here I need to pass my arguments>
I'm thinking about ENV but it makes confusion. I don't know how to pass env variable from docker run command to dockerfile.
I believe there is a way to done.
can anyone help me on this?

you can pass argument to
docker run
so, when you run a container
just check
docker run --help
and you will get, among other things
-e, --env value Set environment variables (default [])
--env-file value Read in a file of environment variables (default [])
ENV is taken care of at build time
if you want
"I don't know how to pass env variable from docker run command to dockerfile"
you can't, docker run starts a container from a created image, a Dockerfile helps you build a new image
The flow
A Dockerfile -> docker build -t myuser/myimage:v12.3 . my new image
launch a container docker run myuser/myimage:v12.3 myoptions from my image myuser/myimage:v12.3

Related

Where to put environment variables for a SPA during docker build time in a CI pipeline?

At build time in localhost, the environment variables are set in .env files.
At build time during a Continuos Integration process are set onto the Dockerfile using ARG and ENV so the CI is responsible of "hydrating" the values as build argument for docker build command.
I'm wondering if this is an scalable solution when the docker image needs a considerable amount of environment variables making the CI command line a bit cumbersome. Keeping in mind that is for a SPA and not sensible information or secret is been added as environment variable.
`
# This is an example of a dummy Dockerfile with multi stage
FROM your.npmregistry.com/node:16-alpine3.11 AS nginx_conf
LABEL owner=whoever
WORKDIR /build
COPY . .
FROM your.npmregistry.com/node:16-alpine3.11 AS build
LABEL owner=whoever
RUN apk --no-cache add python build-base
WORKDIR /app
COPY . .
ARG THEME
ARG NEW_RELIC_ENABLED="true"
ENV REACT_APP_THEME=$THEME
ENV REACT_APP_NEW_RELIC_ENABLED=$NEW_RELIC_ENABLED
RUN yarn --frozen-lockfile && yarn build
FROM your.npmregistry.com/nginx:stable-alpine AS dist
LABEL owner=whoever
RUN this and that
WORKDIR /app
COPY ./foo.sh .
ENTRYPOINT ["./foo.sh"]
CMD ["nginx", "-g", "pid /tmp/nginx.pid; daemon off;"]
# This is just an example
EXPOSE XYZ XYZ
`
An example of Jenkinsfile with just 2 arguments looks like this one below.
agent {
dockerfile {
filename 'docker/Dockerfile.nonprod'
additionalBuildArgs "--target build --build-arg THEME=foo --build-arg NEW_RELIC_ENABLED=false -t foo-build"
reuseNode true
}
}
However if it requires a considerable amount of environment variables it could become cumbersome.
Just a dummy example below.
agent {
dockerfile {
filename 'docker/Dockerfile.nonprod'
additionalBuildArgs "--target build --build-arg THEME=foo --build-arg NEW_RELIC_ENABLED=false --build-arg another0=foo --build-arg another0=foo --build-arg another0=foo --build-arg another0=foo --build-arg another0=foo --build-arg another0=foo --build-arg another0=foo --build-arg another0=foo --build-arg another0=foo -t foo-build"
reuseNode true
}
}
I expect to have a solution that makes the code more readable.

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/

Trying to pass argument to dockerfile not working when build an image with .net core app

I have .NET Core web app and I'm trying to build image using the following command:
docker build -f "C:\myapp\Dockerfile" --force-rm -t infoeditor --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=InfoEditor.Web" --build-arg USER=MYUSERNAME --build-arg PAT=MYPASS "C:\myapp\InfoEditor"
in dockerfile I have:
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
ARG USER
ARG PAT
RUN echo $PAT
RUN echo $USER
but echo returns $PAT instead of MYPASSWORD. Also happen to $USER.
What I'm doing wrong ? I put those ARG in first line before FROM ... same thing
In a multistage build, you need to renew the arguments in each stage:
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
ARG USER
RUN echo "1) $USER"
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
ARG USER
RUN echo "2) $USER"
And then, to test:
$ docker build --tag temp --build-arg USER=MYUSERNAME .
From the reference:
An ARG instruction goes out of scope at the end of the build stage
where it was defined. To use an arg in multiple stages, each stage
must include the ARG instruction.

docker build --build-arg with github password is not working

When my Dockerfile was like below, it was working well.
...
RUN pip install git+https://user_name:my_password#github.com/repo_name.git#egg=repo_name==1.0.0
...
But when I changed Dockerfile to the below
...
RUN pip install git+https://user_name:${GITHUB_PASSWORD}#github.com/repo_name.git#egg=repo_name==1.0.0
...
And used the command below, it's not working.
docker build -t my_repo:tag_name . --build-arg GITHUB_PASSWORD=my_password
You need to add an ARG declaration into the Dockerfile:
FROM ubuntu
ARG PASSWORD
RUN echo ${PASSWORD} > /password
Then build your docker image:
$ docker build -t foo . --build-arg PASSWORD="foobar"
After this, you can check for the existence of the parameter in your docker container:
$ docker run -it foo bash
root#ebeb5b33941e:/# cat /password
foobar
Therefore, add the ARG GITHUB_PASSWORD build arg into your dockerfile to get it to work.

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