Why are the ARG variables in my dockerfile are always empty?
Command
docker build --rm --force-rm --no-cache -f ./Dockerfile
Dockerfile
ARG APP_NAME='ground-station'
FROM node:current AS build-node
WORKDIR /${APP_NAME}
RUN echo "APP_NAME=${APP_NAME}"
Output
Sending build context to Docker daemon 1.199MB
Step 1/4 : ARG APP_NAME='ground-station'
Step 2/4 : FROM node:current AS build-node
---> 6e72986b1b6e
Step 3/4 : WORKDIR /${APP_NAME}
---> Running in 39f12e36d4a1
Removing intermediate container 39f12e36d4a1
---> 93f5cdef6402
Step 4/4 : RUN echo "APP_NAME=${APP_NAME}"
---> Running in a18ac6f3bee8
APP_NAME=
Removing intermediate container a18ac6f3bee8
---> 746cea84bb8f
Successfully built 746cea84bb8f
On step 4, APP_NAME is always empty.
I searched for a solution but all I've found is this. I tried with --no-cache but it still doesn't work.
Output of docker version
Client: Docker Engine - Community
Version: 20.10.5
API version: 1.41
Go version: go1.13.15
Git commit: 55c4c88
Built: Tue Mar 2 20:17:52 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.5
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 363e9a8
Built: Tue Mar 2 20:15:47 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4
GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc:
Version: 1.0.0-rc93
GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
docker-init:
Version: 0.19.0
GitCommit: de40ad0
ARG steps are scoped. Before the first FROM step, the ARG only applies to FROM steps. And within each FROM step, it only applies to the lines after that ARG step until the next FROM (in a multistage build).
To fix this, reorder your steps:
FROM node:current AS build-node
ARG APP_NAME='ground-station'
WORKDIR /${APP_NAME}
RUN echo "APP_NAME=${APP_NAME}"
Related
I am trying to COPY a source file that is locally present to a destination path that is dynamically passed using the docker ARG command.
Example dockerfile is:
$ cat mydockerfile
FROM debian:latest
RUN apt update
ENV app_env='prod'
ARG src_app_dir
ARG dest_app_dir
RUN echo ${src_app_dir}
RUN echo ${dest_app_dir}
RUN mkdir /root/${dest_app_dir}
COPY ${src_app_dir}/file.txt /root/${dest_app_dir}/filenew.txt
WORKDIR /
CMD ["bash"]
I am trying to pass the build arg dest_app_dir="server_app_dir" and expecting the build process creates the container path /root/server_app_dir/
The source folder is already present on my local machine and where the docker-build context is present.
$ ls -d local_app_dir/
local_app_dir/
$ ls local_app_dir/
file.txt
But I am getting the following error for the destination path:
$ docker image build --build-arg src_app_dir="local_app_dir" dest_app_dir="server_app_dir" --tag arg_env:1.0 --file mydockerfile
unable to prepare context: path "dest_app_dir=server_app_dir" not found
Does not it work that way or am I missing the correct concept/usage of Docker build ARG and COPY commands here?
I am using docker-desktop on Windows11.
$ docker version
Client: Docker Engine - Community
Cloud integration: v1.0.23
Version: 20.10.14
API version: 1.41
Go version: go1.16.15
Git commit: a224086
Built: Thu Mar 24 01:48:21 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Desktop
Engine:
Version: 20.10.14
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: 87a90dc
Built: Thu Mar 24 01:46:14 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.5.11
GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc:
Version: 1.0.3
GitCommit: v1.0.3-0-gf46b6ba
docker-init:
Version: 0.19.0
GitCommit: de40ad0
You need to specify the build-arg as many times as the arguments
docker image build --build-arg src_app_dir="local_app_dir" --build-arg dest_app_dir="server_app_dir" --tag arg_env:1.0 --file mydockerfile .
Example
EDIT: Forgot to add context. Thanks #BMitch
I try to build a docker image with Jenkins, using here docuemnt. Part of the The shell:
# Docker image build.
mkdir -p "$BASE_PATH/.docker"
cd "$BASE_PATH/.docker"
echo "docker version: "
docker version
docker login --username=****** --password ****** ******
docker build -t "******/$DOCKER_IMAGE" -f- . <<EOF
FROM ******
ARG NGINX_CONF_FILE=$NGINX_CONF_FILE
ENV DEPLOY_PATH=$DEPLOY_PATH
ENV NGINX_CONF_DIR=$NGINX_CONF_DIR
RUN mkdir -p \$DEPLOY_PATH \\
&& chmod 777 "\$DEPLOY_PATH"
WORKDIR \$DEPLOY_PATH
ADD customer customer/
ADD mall mall/
ADD marketing marketing/
ADD portal portal/
ADD setup setup/
ADD store store/
ADD work work/
WORKDIR \$NGINX_CONF_DIR
COPY ./\$NGINX_CONF_FILE .
EXPOSE 8000
EOF
When running it with bash, Jenkins complains:
docker version:
Client:
Version: 17.03.1-ce
API version: 1.27
Go version: go1.7.5
Git commit: 0801b25
Built: Tue Mar 28 08:29:28 2017
OS/Arch: linux/amd64
Server:
Version: 17.03.1-ce
API version: 1.27 (minimum version 1.12)
Go version: go1.7.5
Git commit: 0801b25
Built: Tue Mar 28 08:29:28 2017
OS/Arch: linux/amd64
Experimental: false
Login Succeeded
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /var/jenkins_home/workspace/docker-test-scrmv3/.docker/-: no such file or directory
Build step 'Execute shell' marked build as failure
New run name is '#17 ver:0.0.17'
Finished: FAILURE
Looks like docker treat the hyphen (-) as a directory not stdin. Accroding to the offical recommand this should work but dont know how. Any way to fix it?
Docker should be above 17.05. The document not metion it.
I have the following Dockerfile
FROM openjdk:8-jdk-alpine
RUN mkdir -p /webpieces
COPY * /webpieces
WORKDIR "/webpieces"
ENTRYPOINT ["./bin/webpiecesexample"]
When I build like so, I get the following error
Deans-MacBook-Pro:webpiecesexample dean$ docker build -t gcr.io/braided-topic/webpieces2 .
Sending build context to Docker daemon 75.56MB
Step 1/5 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/5 : RUN mkdir -p /webpieces
---> Running in bc615c0cd540
Removing intermediate container bc615c0cd540
---> 69a2f4530c44
Step 3/5 : COPY * /webpieces
When using COPY with more than one source file, the destination must be a directory and end with a /
When I trim the DockerFile and just build with the first two lines and then run with a basic shell to view the directories, I see the webpieces directory there
docker run -it --entrypoint sh gcr.io/braided-topic-266113/webpieces2
I can cd into webpieces and everything. Why is the copy command not working here?
docker version here:
Deans-MacBook-Pro:distributions dean$ docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:22:34 2019
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:29:19 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
When using COPY with more than one source file, the destination must be a directory and end with a /.
Change the COPY line to
COPY * /webpieces/
...it seems to copy the contents of each directory instead of the directories themselves. I would prefer not to name each directory I am moving as we prefer auto-add when we make changes.
Use . instead of * and it'll preserve all the nesting.
COPY . /webpieces/
I'm having some trouble with passing argument value to dockerfile.
Running docker in Windows Server 2016
My docker version info is
PS C:\Users\Administrator\Desktop> docker version
Client: Docker Engine - Enterprise
Version: 19.03.4
API version: 1.40
Go version: go1.12.10
Git commit: 9e27c76fe0
Built: 10/17/2019 23:42:50
OS/Arch: windows/amd64
Experimental: false
Server: Docker Engine - Enterprise
Engine:
Version: 19.03.4
API version: 1.40 (minimum version 1.24)
Go version: go1.12.10
Git commit: 9e27c76fe0
Built: 10/17/2019 23:41:23
OS/Arch: windows/amd64
Experimental: false
and powershell opened as administrator.
My reference is this.
But it not worked for me.
So this is my dockerfile.
FROM microsoft/iis
ARG a_version
RUN echo $a_version
Also i tried many different types of echo value
Such as
RUN echo "$a_version"
RUN echo ${a_version}
RUN echo "${a_version}"
And this is my execute command.
docker build . --build-arg a_version=1234
My expected result was print 1234
But actual result was
PS C:\Users\Administrator\Desktop> docker build . --build-arg a_version=1234
Sending build context to Docker daemon 14.04MB
Step 1/3 : FROM microsoft/iis
---> 595015675977
Step 2/3 : ARG a_version
---> Running in 91698d9e71da
Removing intermediate container 91698d9e71da
---> 2bad94a2ce74
Step 3/3 : RUN echo $a_version
---> Running in 3fe25ecb813c
$a_version
Why it happens? How can is fix it?
In Windows Command-Prompt the syntax is echo %a_version% as your
base image is based on window.
how-can-i-display-the-contents-of-an-environment-variable-from-the-command-promp
So you can change this to
FROM microsoft/iis
ARG a_version
RUN echo %a_version%
env in window dockerfile
I had to do something simmilar and this is a piece of code that worked for me.
ARG PORT_SITE
ENV PORT_SITE ${PORT_SITE}
and then I refer it in the entrypoint as following
$env:PORT_SITE
That's the way I did it, but i suppose that you can ommit the env variable and just refer to the arg value using
${PORT_SITE}
I've just added a Dockerfile to my simple asp.net core demo project.
When i run
docker build -f .\deploy\Dockerfile -t coreapp .
It results in a image of 1,35GB..
Docker version (docker for windows)
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:21:34 2018
OS/Arch: windows/amd64
Experimental: false
Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.24)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:36:40 2018
OS/Arch: windows/amd64
Experimental: false
Dockerfile
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /build
COPY . /build
#Restore
RUN dotnet restore ./WebAppDockerDemo.sln
RUN dotnet publish ./src/WebApp/WebApp.csproj --output /publish --configuration Release
# Build runtime image
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /publish/ .
ENTRYPOINT ["dotnet", "WebApp.dll"]
Why is the image so large? I thought the runtime image I create would be around 2-300MB.. I checked different examples, 1, but can't find what's wrong here.
How can I reduce the size?