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

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.

Related

Access docker build context inside dockerfile

I have multiple projects/folders inside a single directory called root. There is a common Dockerfile that runs all projects/folders. I run the projects passing different build context in docker build command as below:
$ docker build -t project1:1.0.0 -f . root/project1
$ docker build -t project2:1.0.0 -f . root/project2
$ docker build -t project2:1.0.0 -f . root/project3
Now, I need to add some conditions based on docker build context in dockerfile. Can that be done? I didn't find a way to get docker build context.
I think you can pass the project name to the Dockerfile as an argument and build for instance:
ARG ENV
RUN if [ "$ENV" = "production" ] ; then yarn client:build:prod ; else yarn client:build ; fi
finally:
docker build -t node-image . --build-arg ENV=production

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 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}

pass arguments from docker run to dockerfile

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

Resources