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
Related
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
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}"
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'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?