In Dockerfile build how to get container with ssh keys loaded in - docker

I have docker image which already have keys saved.I want to get docker container which will have ssh keys loaded when i run it. I went over few stackoverflow questions where its suggested to use as below which didnt help
RUN eval $(ssh-agent -s) && ssh-add /root/.ssh/ok
My Dockerfile look like
FROM cmf
RUN eval $(ssh-agent -s) && ssh-add /root/.ssh/ok
when i created docker image and ran it like , i dont see keys loaded
docker run -it intro_to_docker2 /bin/bash
root#17a094f42efb:/# ssh-add -l
Could not open a connection to your authentication agent.
root#17a094f42efb:/#
Below is build
% docker build --no-cache -t intro_to_docker2 .
[+] Building 0.3s (6/6) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 102B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/cmf:latest 0.0s
=> CACHED [1/2] FROM docker.io/library/cmf 0.0s
=> [2/2] RUN eval $(ssh-agent -s) && ssh-add /root/.ssh/ok 0.2s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:4acb58d65b43a15e6a85a99f99d223d87c90736eed07d427e8ceaeac544f87a4 0.0s
=> => naming to docker.io/library/intro_to_docker2

Related

Is there a way to create multiple tags using the buildx build command in docker?

I am trying to use the docker buildx build command and I would like to be able to create multiple tags in one line. An example of what I have tried
docker buildx build . \
--platform linux/arm64,linux/amd64 \
--no-cache --push \
-t test:latest -t test:${CI_COMMIT_SHORT_SHA}
I have seen this issue, but I haven't been able to get it to work
https://github.com/docker/buildx/issues/396
It works exactly as you've posted with multiple -t args, e.g. -t repo1:tag1 -t repo2:tag2:
$ regctl tag ls sudobmitch/demo
alpine
regcli-test
regctl
ubuntu
$ docker buildx build -f build/Dockerfile.regctl.buildkit -t sudobmitch/demo:regctl1 -t sudobmitch/demo:regctl2 --push .
[+] Building 13.4s (24/24) FINISHED
=> [internal] load build definition from Dockerfile.regctl.buildkit 0.0s
=> => transferring dockerfile: 4.40kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 172B 0.0s
=> resolve image config for docker.io/docker/dockerfile:1 0.3s
=> [auth] docker/dockerfile:pull token for registry-1.docker.io 0.0s
=> CACHED docker-image://docker.io/docker/dockerfile:1#sha256:443aab4ca21183e069e7d8b2dc68006594f40bd 0.0s
=> => resolve docker.io/docker/dockerfile:1#sha256:443aab4ca21183e069e7d8b2dc68006594f40bddf1b15bbd83 0.0s
=> [internal] load metadata for docker.io/library/golang:1.17-alpine 0.4s
=> [auth] library/golang:pull token for registry-1.docker.io 0.0s
=> [golang 1/4] FROM docker.io/library/golang:1.17-alpine#sha256:3bdce0a4828648811dc3ac155b8f5155ca6e 0.0s
=> => resolve docker.io/library/golang:1.17-alpine#sha256:3bdce0a4828648811dc3ac155b8f5155ca6e13c5a86 0.0s
=> [internal] load build context 0.2s
=> => transferring context: 8.95MB 0.2s
=> CACHED [golang 2/4] RUN apk add --no-cache ca-certificates git make 0.0s
=> CACHED [golang 3/4] RUN addgroup -g 1000 appuser && adduser -u 1000 -G appuser -D appuser && mkd 0.0s
=> CACHED [golang 4/4] WORKDIR /src 0.0s
=> [dev 1/1] COPY --link . /src/ 0.3s
=> [build 1/1] RUN --mount=type=cache,id=gomod,target=/go/pkg/mod/cache --mount=type=cache,id=gor 1.5s
=> CACHED [release-scratch 1/5] ADD --link build/root.tgz / 0.0s
=> CACHED [release-scratch 2/5] COPY --link --from=build /etc/passwd /etc/group /etc/ 0.0s
=> CACHED [release-scratch 3/5] COPY --link --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/ 0.0s
=> CACHED [release-scratch 4/5] COPY --link --from=build --chown=1000:1000 /home/appuser/ /home/appus 0.0s
=> [release-scratch 5/5] COPY --link --from=build /src/bin/regctl /regctl 0.0s
=> exporting to image 3.7s
=> => exporting layers 0.4s
=> => exporting manifest sha256:ebd28e5f9096008ce106880bea389dfaf164cd298429705ccb922fd72142f83c 0.0s
=> => exporting config sha256:5fc50e1c198eb9061de9f0c1a12cb660753c41b46d1eb73f6117e3e260ba641d 0.0s
=> => pushing layers 0.2s
=> => pushing manifest for docker.io/sudobmitch/demo:regctl1#sha256:ebd28e5f9096008ce106880bea389dfaf 0.2s
=> => pushing manifest for docker.io/sudobmitch/demo:regctl2#sha256:ebd28e5f9096008ce106880bea389dfaf 0.1s
=> [auth] sudobmitch/demo:pull,push token for registry-1.docker.io 0.0s
=> [auth] sudobmitch/demo:pull,push token for registry-1.docker.io 0.0s
=> [auth] sudobmitch/demo:pull,push token for registry-1.docker.io 0.0s
=> [auth] sudobmitch/demo:pull,push token for registry-1.docker.io 0.0s
$ regctl tag ls sudobmitch/demo
alpine
regcli-test
regctl
regctl1
regctl2
ubuntu
This is assuming test was an example in your question. If you actually tried to push to the test repo, that expands to Docker Hub's Library, along side other official images like alpine, busybox, debian, etc. You don't have access to push there, so make sure to point to a repository where you have access to push images and have logged in.

How do you get a directory listing to output during build in Docker 20.10.3? (March 2022) [duplicate]

This question already has answers here:
Why is docker build not showing any output from commands?
(6 answers)
Closed 11 months ago.
The answers here don't seem to work. The answer here also doesn't work. I suspect something has changed about Docker's build engine since then.
My Dockerfile:
FROM node:16.14.2-alpine
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn
COPY dist .
EXPOSE $SEEDSERV_PORT
RUN pwd
RUN echo "output"
RUN ls -alh
RUN contents="$(ls -1 /usr/src/app)" && echo $contents
# CMD ["node","server.js"]
ENTRYPOINT ["tail", "-f", "/dev/null"]
Which gives this output from build:
✗ docker build --progress auto --build-arg SEEDSERV_PORT=9999 -f build/api/Dockerfile .
[+] Building 2.1s (14/14) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/node:16.14.2-alpine 1.9s
=> [internal] load build context 0.0s
=> => transferring context: 122B 0.0s
=> [1/9] FROM docker.io/library/node:16.14.2-alpine#sha256:da7ef512955c906b6fa84a02295a56d0172b2eb57e09286ec7abc02cfbb4c726 0.0s
=> CACHED [2/9] WORKDIR /usr/src/app 0.0s
=> CACHED [3/9] COPY package.json yarn.lock ./ 0.0s
=> CACHED [4/9] RUN yarn 0.0s
=> CACHED [5/9] COPY dist . 0.0s
=> CACHED [6/9] RUN pwd 0.0s
=> CACHED [7/9] RUN echo "output" 0.0s
=> CACHED [8/9] RUN ls -alh 0.0s
=> CACHED [9/9] RUN contents="$(ls -1 /usr/src/app)" && echo $contents 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:d1dd7ac452ecacc803eed2bb1deff654c3296a5576b6f418dbd07c5f2e644f1a 0.0s
Adding --progress plain gives slightly different output but not what I'm looking for, e.g.:
#11 [7/9] RUN echo "output"
#11 sha256:634e07d201926b0f70289515fcf4a7303cac3658aeddebfa9552fc3054ed4ace
#11 CACHED
How can I get a directory listing during build in 20.10.3? I can exec into the running container but that's a lot more work.
If your build is cached, there's no output from the run to show. You need to include --no-cache to run the command again for any output to display, and also include --progress plain to output to the console.

How do I get the latest version of file in GitHub Release and download the file in Dockerfile?

I'm trying to get the version number of the latest release of a Github. (Stored in the LATEST_VERSION argument). Using this, I want to download a particular file from a GitHub repository (Using the ADD) command, however I am getting an error Error Image Screenshot
FROM adoptopenjdk/openjdk11:jdk-11.0.6_10-alpine
RUN mkdir -p /home/app
ARG ORG_NAME=archu0212
ARG REPO_NAME=Test
ARG LATEST_VERSION=$(curl -s https://api.github.com/repos/${ORG_NAME}/${REPO_NAME}/releases/latest | grep "tag_name" | cut -d'v' -f2 | cut -d'"' -f1)
ADD https://github.com/${ORG_NAME}/${REPO_NAME}/releases/download/v${LATEST_VERSION}/MainTestFile.jar /home/app
ENTRYPOINT ["java", "-jar", "/home/app"]
docker build -t test-image .
Building 2.0s (6/7)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 649B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/adoptopenjdk/openjdk11:jdk-11.0.6_10-alpine 1.4s
=> [1/3] FROM docker.io/adoptopenjdk/openjdk11:jdk-11.0.6_10-alpine#sha256:a4e96cebf2f00b354b6f935560d4e64ad24435af77322cdf538975962d7e17d3 0.0s
=> ERROR https://github.com/archu0212/Test/releases/download/v$(curl/MainTestFile.jar 0.3s
=> CACHED [2/3] RUN mkdir -p /home/app 0.0s
------
> https://github.com/archu0212/Test/releases/download/v$(curl/MainTestFile.jar:
Any help would be appreciated. Stuck on this for a week.

file not found error when running container

I have a test automation project which gets uses the code built as part of jar file and that jar gets invoked via bat file. All these files are stored within my project folder.
contents of my Docker file:
FROM maven:3.8.1-adoptopenjdk-11
#WORKDIR C:/Work/Kickstart_TEM/Prefs
COPY Prefs /home/Prefs
COPY KickStart.jar /home/Prefs/KickStart.jar
CMD home\prefs\run.bat && cmd
docker build generates following output
[+] Building 0.3s (8/8) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 210B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/maven:3.8.1-adoptopenjdk-11 0.0s
=> [1/3] FROM docker.io/library/maven:3.8.1-adoptopenjdk-11 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 390B 0.0s
=> CACHED [2/3] COPY Prefs /home/Prefs 0.0s
=> CACHED [3/3] COPY KickStart.jar /home/Prefs/KickStart.jar 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.0s
=> => writing image sha256:4c878e8a895b2fad307e00f1b2fb5c9b5df7dc630e87414230d1989b75a5ee17 0.0s
=> => naming to docker.io/library/demo2
Docker run generates following error:
PS C:\Work\Docker_POC> docker run -i -p 4044:4044 demo2
/bin/sh: 1: homeprefsrun.bat: not found
My containers stops right away, so I am not even able to figure out if my files and folders got copied successfully or not. And I am unsure of how to resolve this error.
First of all, you're trying to run a batch script under Linux (the docker image you're using determines this).
In general, your CMD statement should look like CMD ["/bin/sh", "-c", "/home/Prefs/run.sh && cmd"] (although I'm not sure what cmd is and why you want to run it)
You should convert this batch script (run.bat) to a shell script. Also, there is a difference between home and /home and filenames are case-sensitive (thus it's Prefs and not prefs).

Docker fails to build image with exit code 139

I'm trying to build an image from CentOS 6.9. Using this Dockerfile:
FROM centos:6.9
RUN ls
But it keeps failing with exit code 139 with the following output:
$ docker build -t centos-6.9 .
[+] Building 1.1s (7/7) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 72B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:6.9 0.6s
=> [internal] load build context 0.1s
=> => transferring context: 72B 0.0s
=> CACHED [1/3] FROM docker.io/library/centos:6.9#sha256:6fff0a9edc920968351eb357c5b84016000fec6956e6d745f695e5a34f18ecd2 0.0s
=> [2/3] COPY . . 0.0s
=> ERROR [3/3] RUN ls 0.3s
------
> [3/3] RUN ls:
------
executor failed running [/bin/sh -c ls]: exit code: 139
I'm running:
Windows 10 Enterprise Version 2004
Docker Desktop 3.0.0
This appears to be an issue with WSL 2 with older base images, not docker or the image itself.
Create %userprofile%\.wslconfig file.
Add the following:
[wsl2]
kernelCommandLine = vsyscall=emulate
Restart WSL. wsl --shutdown
Restart Docker Desktop.
References:
https://github.com/microsoft/WSL/issues/4694#issuecomment-556095344
https://github.com/docker/for-win/issues/7284#issuecomment-646910923
https://github.com/microsoft/WSL/issues/4694#issuecomment-558335829

Resources