Passing argument to dockerfile not working well - docker

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}

Related

Docker LABEL to access build image in multi-stage build

During my CI build I extract build artifacts from the build image in a multi-stage Docker build (test reports) by adding a LABEL build step and then creating a container with the labeled image to extract the artifact. This seems to be broken in Docker Desktop for Mac, or I misunderstand how this is supposed to work?
Here is a minimal build to reproduce the issue:
# Dockerfile.one
FROM alpine AS one
WORKDIR /test
RUN touch here
LABEL build=here
# Dockerfile.two
FROM alpine AS one
WORKDIR /test
RUN touch here
LABEL build=here
FROM alpine AS two
COPY --from=one /test/* /test/
RUN touch again
# test.sh
#! /bin/bash -u
# Clean up
trap 'rm -r test & docker rmi $image' EXIT
docker build -f $1 .
docker images --filter 'label=build=here'
image=$(docker images --filter 'label=build=here' -q | head -n 1)
id=$(docker create $image) && docker cp $id:/test/ . && docker rm $id
ls -l test
Running ./test.sh Dockerfile.one (only one stage) produces the expected output of the file here being present in the working directory.
However, running ./test.sh Dockerfile.two (multi-stage) fails, apparently because no images match --filter label=build=here.
What is wrong here?
$ docker version
Client:
Cloud integration: 1.0.17
Version: 20.10.7
API version: 1.41
Go version: go1.16.4
Git commit: f0df350
Built: Wed Jun 2 11:56:22 2021
OS/Arch: darwin/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.7
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: b0f5bc3
Built: Wed Jun 2 11:54:58 2021
OS/Arch: linux/amd64
Experimental: true

How to build docker image, using a dockerfile from stdin with shell varaibles

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.

Unspecified error (0x80004005) while running a Docker build

Going thought the onboarding first steps of docker https://hub.docker.com/?overlay=onboarding , I run into an issue when running the build command
docker build -t <user>/cheers2019 .
The error happend at the 6th step, with code error (0x80004005). I am running docker on windows with windows containers.
The full error code:
Sending build context to Docker daemon 13.31kB
Step 1/9 : FROM golang:1.11-alpine AS builder
---> e116d2efa2ab
Step 2/9 : RUN apk add --no-cache git
---> Using cache
---> 07f79e350f69
Step 3/9 : RUN go get github.com/pdevine/go-asciisprite
---> Using cache
---> cfe4fd4064c3
Step 4/9 : WORKDIR /project
---> Using cache
---> 4ba87d64456e
Step 5/9 : COPY cheers.go .
---> Using cache
---> 486d7602dccf
Step 6/9 : RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o cheers cheers.go
---> Running in b77dee3bd612
container b77dee3bd612319c299e077116fc68ccf7f166246f85138b0e1cc0e074c8cb2b encountered an error during CreateProcess: failure in a Windows system call: Unspecified error (0x80004005)
[Event Detail: failed to run runc create/exec call for container b77dee3bd612319c299e077116fc68ccf7f166246f85138b0e1cc0e074c8cb2b: exit status 1 Stack Trace:
github.com/Microsoft/opengcs/service/gcs/runtime/runc.(*container).startProcess
/go/src/github.com/Microsoft/opengcs/service/gcs/runtime/runc/runc.go:580
github.com/Microsoft/opengcs/service/gcs/runtime/runc.(*runcRuntime).runCreateCommand
/go/src/github.com/Microsoft/opengcs/service/gcs/runtime/runc/runc.go:471
github.com/Microsoft/opengcs/service/gcs/runtime/runc.(*runcRuntime).CreateContainer
/go/src/github.com/Microsoft/opengcs/service/gcs/runtime/runc/runc.go:113
github.com/Microsoft/opengcs/service/gcs/core/gcs.(*gcsCore).ExecProcess
/go/src/github.com/Microsoft/opengcs/service/gcs/core/gcs/gcs.go:351
github.com/Microsoft/opengcs/service/gcs/bridge.(*Bridge).execProcess
/go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:637
github.com/Microsoft/opengcs/service/gcs/bridge.(*Bridge).execProcess-fm
/go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:253
github.com/Microsoft/opengcs/service/gcs/bridge.HandlerFunc.ServeMsg
/go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:72
github.com/Microsoft/opengcs/service/gcs/bridge.(*Mux).ServeMsg
/go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:146
github.com/Microsoft/opengcs/service/gcs/bridge.(*Bridge).ListenAndServe.func2.1
/go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:335
runtime.goexit
/usr/lib/go/src/runtime/asm_amd64.s:1333 Provider: 00000000-0000-0000-0000-000000000000] extra info: {"CommandArgs":["/bin/sh","-c","CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags \"-static\"' -o cheers cheers.go"],"WorkingDirectory":"/project","Environment":{"GOLANG_VERSION":"1.11.13","GOPATH":"/go","HOSTNAME":"b77dee3bd612","PATH":"/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"},"CreateStdInPipe":true,"CreateStdOutPipe":true,"CreateStdErrPipe":true,"ConsoleSize":[0,0],"OCISpecification":{"ociVersion":"1.0.1-dev","process":{"user":{"uid":0,"gid":0},"args":["/bin/sh","-c","CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags \"-static\"' -o cheers cheers.go"],"env":["PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","HOSTNAME=b77dee3bd612","GOLANG_VERSION=1.11.13","GOPATH=/go"],"cwd":"/project","capabilities":{"bounding":["CAP_CHOWN","CAP_DAC_OVERRIDE","CAP_FSETID","CAP_FOWNER","CAP_MKNOD","CAP_NET_RAW","CAP_SETGID","CAP_SETUID","CAP_SETFCAP","CAP_SETPCAP","CAP_NET_BIND_SERVICE","CAP_SYS_CHROOT","CAP_KILL","CAP_AUDIT_WRITE"],"effective":["CAP_CHOWN","CAP_DAC_OVERRIDE","CAP_FSETID","CAP_FOWNER","CAP_MKNOD","CAP_NET_RAW","CAP_SETGID","CAP_SETUID","CAP_SETFCAP","CAP_SETPCAP","CAP_NET_BIND_SERVICE","CAP_SYS_CHROOT","CAP_KILL","CAP_AUDIT_WRITE"],"inheritable":["CAP_CHOWN","CAP_DAC_OVERRIDE","CAP_FSETID","CAP_FOWNER","CAP_MKNOD","CAP_NET_RAW","CAP_SETGID","CAP_SETUID","CAP_SETFCAP","CAP_SETPCAP","CAP_NET_BIND_SERVICE","CAP_SYS_CHROOT","CAP_KILL","CAP_AUDIT_WRITE"],"permitted":["CAP_CHOWN","CAP_DAC_OVERRIDE","CAP_FSETID","CAP_FOWNER","CAP_MKNOD","CAP_NET_RAW","CAP_SETGID","CAP_SETUID","CAP_SETFCAP","CAP_SETPCAP","CAP_NET_BIND_SERVICE","CAP_SYS_CHROOT","CAP_KILL","CAP_AUDIT_WRITE"]}},"root":{"path":"rootfs"},"hostname":"b77dee3bd612","mounts":[{"destination":"/proc","type":"proc","source":"proc","options":["nosuid","noexec","nodev"]},{"destination":"/dev","type":"tmpfs","source":"tmpfs","options":["nosuid","strictatime","mode=755","size=65536k"]},{"destination":"/dev/pts","type":"devpts","source":"devpts","options":["nosuid","noexec","newinstance","ptmxmode=0666","mode=0620","gid=5"]},{"destination":"/sys","type":"sysfs","source":"sysfs","options":["nosuid","noexec","nodev","ro"]},{"destination":"/sys/fs/cgroup","type":"cgroup","source":"cgroup","options":["ro","nosuid","noexec","nodev"]},{"destination":"/dev/mqueue","type":"mqueue","source":"mqueue","options":["nosuid","noexec","nodev"]},{"destination":"/dev/shm","type":"tmpfs","source":"shm","options":["nosuid","noexec","nodev","mode=1777"]}],"linux":{"resources":{},"namespaces":[{"type":"mount"},{"type":"network"},{"type":"uts"},{"type":"pid"},{"type":"ipc"}],"maskedPaths":["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"readonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"windows":{"layerFolders":["C:\\ProgramData\\Docker\\lcow\\072ca1f3855a32f3e3b24c2fe4ca8dea9853d4fb64207407094332a2d6541458","C:\\ProgramData\\Docker\\lcow\\c45071b2eff55610a9d9b3f430b8e5fa3ff65465e59b8d5a63a61e744ca94c48","C:\\ProgramData\\Docker\\lcow\\6890c90396b8fcd14669837530eef071ec5e3f10dda6724478c7ab1b58752307","C:\\ProgramData\\Docker\\lcow\\a1998eef0068aac5fb61f53e87ce8d6d46eaf5c83f7eab36cb745fb57ad72e8a","C:\\ProgramData\\Docker\\lcow\\f311f4698a5c1eb5e8800ef04c6066177112d76afc18d089fcf2cd5642712a01","C:\\ProgramData\\Docker\\lcow\\5e7e640cace36a63707967e7109604dacc800b8163eb04a4be7f346ff38ba867","C:\\ProgramData\\Docker\\lcow\\a2a8b17328e92e236a3d30841abed7b815188a3d63e2432272f772acda4ec75d","C:\\ProgramData\\Docker\\lcow\\d774068efa88052dab131c32c40832302f547dd4737738ca19873e0c3bed0074","C:\\ProgramData\\Docker\\lcow\\b433fd6793fe572e509619d719280574a3b1860dbcc647ec6629695666227ca5","C:\\ProgramData\\Docker\\lcow\\b77dee3bd612319c299e077116fc68ccf7f166246f85138b0e1cc0e074c8cb2b"],"hyperv":{},"network":{"endpointList":["0FFBAC8D-5E31-4C4F-A753-85C5148C437D"],"allowUnqualifiedDNSQuery":true}}}}
Any idea of how I should run my builds in docker ?
I encountered exactly the same error. I don' know if it comes from the fact that during installation I checked "only Windows containers", but it switched to Windows containers in Docker For Windows.
So the solution is to click switch to Linux container, then I clean all :
docker system prune
Then I run again:
docker build -t <user>/cheers2019 .
Now it works for me.
Here is my Docker version :
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:37 2019
OS/Arch: windows/amd64
Experimental: true
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: true
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
I have Trying to change the experimental.
Check : Docker version in Console if experimental is false in both Client and Server.
Then you can change in C:\Users\.docker
in config.json you change experimental":"enabled"
then in daemon.json you change "experimental": true
then after save I run again.
docker build -t <user>/cheers2019 .
On Windows 10 I was having the same problem reported in the current question and the following issue as well: NPM Install doesn't work in Docker.
To solve the issue I've combined both solutions:
#JPBlanc (Switch to Linux Containers)
#Muhammad (disable experimental on command line & configuration file)
How I did it:
1) Switch to Linux Containers: On taskbar, right click on docker icon and click on the option as presented bellow:
2) Disable "Experimental Features" on Command Line: Open docker/settings and click on Command Line:
3) Disable Experimental setting on configuration file:: On docker/settings, click on Docker Engine and certify that experimental is set to false:
Hope it helps.
Cheers
replace CRLF characters with LF for Windows in a tool like Notepad++ and rerun.

How to replace --from tag in Dockerfile to be able to build the image from within Jenkins pipeline

I have the following Dockerfile:
FROM docker.m.our-intra.net/microsoft/dotnet:2.1-sdk
WORKDIR /app
COPY . ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out
FROM docker.m.our-intra.net/microsoft/dotnet:2.1.4-aspnetcore-runtime
WORKDIR /app
COPY --from=docker.m.our-intra.net/microsoft/dotnet:2.1-sdk /app/aspnetapp/MyProject.WebApi/out ./
ENTRYPOINT ["dotnet", "MyProject.WebApi.dll"]
The image is builded locally successfully.
I'm trying to configure a Jenkins pipeline and try to build the image from Jenkinsfile using the following step:
stage('Build') {
steps {
script {
echo 'Build...'
sh 'docker build -t mytag:v${BUILD_NUMBER} -f Dockerfile .'
echo 'Build Completed'
}
}
}
But In Jenkins console I get the error:
Unknown flag: from
How should I edit my Dockerfile, particularly --from tag in order to able to build the image locally and through Jenkins pipeline?
UPDATE:
I've updated the Docker to 18.06.1-ce version. Still have the same error.
Jenkins ver. 2.89.4
Docker Pipeline plugin installed version 1.9.1
UPDATE 2:
I added the docker version command to show me the Docker version:
stage('Build') {
steps {
script {
echo 'Build...'
sh 'docker version'
sh 'docker build -t fact:v${BUILD_NUMBER} -f Dockerfile .'
echo 'Build Completed'
}
}
}
And it outputs the following:
Client:
Version: 18.06.1-ce
API version: 1.24 (downgraded from 1.38)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:25:03 2018
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 1.12.6
API version: 1.24 (minimum version )
Go version: go1.8.3
Git commit: 3e8e77d/1.12.6
Built: Wed Dec 13 12:18:58 2017
OS/Arch: linux/amd64
Experimental: false
I believe it's a docker engine version issue. Docker engine version needs to be 17.05 or later on your Jenkins machine to support --from or multi-stage builds.
From docs -
Multi-stage builds are a new feature requiring Docker 17.05 or higher
on the daemon and client.
Ref - https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds

Docker: Unable to execute RUN command after ADD when starting from scratch

I'm trying to build a custom Linux Alpine docker for ARM architecture starting from the "scratch" image. I don't understand why I can't execute any RUN command after I execute ADD. This is my Dockerfile:
FROM scratch
ADD rootfs.tar /
MAINTAINER Vittorio_Cozzolino
RUN apk add nodejs
And this is the output that I get when the automated build completes (the last line contains the error):
Client: Version: 1.8.1 API version: 1.20 Go version:
go1.4.2 Git commit: d12ea79 Built: Thu Aug 13 02:49:29 UTC
2015 OS/Arch: linux/amd64
Server: Version: 1.8.3-rc1 API version: 1.20 Go version:
go1.4.2 Git commit: 6f21aba Built: Mon Sep 28 20:03:03 UTC
2015 OS/Arch: linux/amd64 Step 0 : FROM scratch
---> Step 1 : ADD rootfs.tar /
---> dd771ffd56ea702 Step 2 : MAINTAINER Vittorio_Cozzolino
---> 825fc4c990c8a33 Step 3 : RUN apk add nodejs [91mexec: "/bin/sh": stat /bin/sh: no such file or directory
Actually /bin/sh exists and, in fact, if I run CMD ["/bin/sh"] I don't get any error. Can anyone help me here?
CMD ["/bin/sh"] is a simple declaration of the default command to run, so it will always work.
Check if the tar is indeed unpacked, as there was a similar issue in 9541: limit your Dockerfile to the ADD directive, and use docker exec or a simple ls to see what is in there (and with which owner/permission).
If the Dockerfile complains about a missing /bin/sh, and /bin/sh is in the tar archive... chances are that archive didn't uncompress properly.

Resources