Docker: using SSH in builds with Buildkit - docker

Following the documentation I'm trying to pass an SSH key to my container. This is my original Dockerfile
# syntax=docker/dockerfile:experimental
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.6
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh git clone git#github.com:USER/REPO.git
and this works
DOCKER_BUILDKIT=1 docker build --ssh default=~/github .
However, if I try to install anything with apt:
# syntax=docker/dockerfile:experimental
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.6
RUN apt update
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh git clone git#github.com:USER/REPO.git
I get the following error:
[+] Building 1.8s (7/9)
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 306B 0.0s
=> resolve image config for docker.io/docker/dockerfile:experimental 1.1s
=> CACHED docker-image://docker.io/docker/dockerfile:experimental#sha256:de85b2f3a3e8a2f7fe48e8e84a65f6fdd5cd5183afa6412fff9caa6871649c44 0.0s
=> [internal] load metadata for docker.io/tiangolo/uvicorn-gunicorn-fastapi:python3.6 0.0s
=> CACHED [1/4] FROM docker.io/tiangolo/uvicorn-gunicorn-fastapi:python3.6 0.0s
=> ERROR [2/4] RUN apt update 0.4s
------
> [2/4] RUN apt update:
#7 0.352
#7 0.352 WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
#7 0.352
#7 0.359 Reading package lists...
#7 0.375 E: Could not get lock /var/lib/apt/lists/lock - open (13: Permission denied)
#7 0.375 E: Unable to lock directory /var/lib/apt/lists/
------
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = failed to build LLB: executor failed running [/bin/sh -c apt update]: runc did not terminate sucessfully
However, the second Dockerfile actually works if Buildkit is disabled. Any suggestions on what might be the problem?

I had this exact same issue. For me the solution was to upgrade Docker. I had this issue with 19.03.11 which my Ubuntu install was pulling in as a snap. 20.10.1 (latest as of this post) worked for me.
More info here: https://github.com/moby/moby/issues/39106#issuecomment-752246367
edit: Unfortunately this doesn't work when the build is run non-interactively (for example, as a systemd-based CI agent) - at least for me.

Related

Docker build fails in ubuntu 18.04 wsl2

I am trying to build docker image in my local machine , I am using docker desktop and ubuntu 18.04 distro wsl2.
It pulls the image successfully yet fails during the "RUN" phase.
I have tried many solutions provided yet no luck . Below is the error , if anybody has any clue please let me know. Been stuck with this since a while.
Docker Image:
FROM alpine:3.14
RUN apk add --no-cache mysql-client
ENTRYPOINT ["mysql"]
Error:
root#LAPTOP-BOSTG8L5:/home/abilash/alpine1# docker build .
[+] Building 14.4s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 118B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/alpine:3.14 3.3s
=> [1/2] FROM docker.io/library/alpine:3.14#sha256:4c869a63e1b7c0722fed1e402a6466610327c3b83bdddb94bd94fb71da7f638a 0.7s
=> => resolve docker.io/library/alpine:3.14#sha256:4c869a63e1b7c0722fed1e402a6466610327c3b83bdddb94bd94fb71da7f638a 0.0s
=> => sha256:dd53f409bf0bd55eac632f9e694fd190244fef5854a428bf3ae1e2b636577623 1.47kB / 1.47kB 0.0s
=> => sha256:c7ed990a2339ee598662849de4f56e2241399f5a32340c8c4a7bbd5378a12b5f 2.83MB / 2.83MB 0.6s
=> => sha256:4c869a63e1b7c0722fed1e402a6466610327c3b83bdddb94bd94fb71da7f638a 1.64kB / 1.64kB 0.0s
=> => sha256:92d13cc58a46e012300ef49924edc56de5642ada25c9a457dce4a6db59892650 528B / 528B 0.0s
=> => extracting sha256:c7ed990a2339ee598662849de4f56e2241399f5a32340c8c4a7bbd5378a12b5f 0.1s
=> ERROR [2/2] RUN apk add --no-cache mysql-client 10.3s
------
> [2/2] RUN apk add --no-cache mysql-client:
#4 0.293 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
#4 5.299 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
#4 5.299 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/main: temporary error (try again later)
#4 10.30 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/community: temporary error (try again later)
#4 10.30 ERROR: unable to select packages:
#4 10.30 mysql-client (no such package):
#4 10.30 required by: world[mysql-client]
------
executor failed running [/bin/sh -c apk add --no-cache mysql-client]: exit code: 1
Issue was with docker desktop I suppose. I uninstall docker desktop and did the docker install directly in wsl did few changes to the network-host and it worked fine.

docker build ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.14/community: temporary error (try again later)

hello guys i have wierd problem when i try to build docker image in ubuntu 22 vm
[+] Building 10.6s (5/14)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 864B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/kong/kong:2.7.0 0.0s
=> CACHED [ 1/12] FROM docker.io/kong/kong:2.7.0 0.0s
=> ERROR [ 2/12] RUN apk update && apk add git unzip luarocks 10.5s
------
> [ 2/12] RUN apk update && apk add git unzip luarocks:
#0 0.240 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
#0 5.245 ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.14/main: temporary error (try again later)
#0 5.283 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
#0 10.29 ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.14/community: temporary error (try again later)
#0 10.48 v3.14.3-58-g7fc21b9dfb [https://dl-cdn.alpinelinux.org/alpine/v3.14/main]
#0 10.48 v3.14.3-57-g005638434d [https://dl-cdn.alpinelinux.org/alpine/v3.14/community]
#0 10.48 2 errors; 14942 distinct packages available
------
failed to solve: executor failed running [/bin/sh -c apk update && apk add git unzip luarocks]: exit code: 2
docker version
Docker version 20.10.18, build b40c2f6
tried restarting docker engine but with no luck
added network=host fixed my problem
build:
context: ../kong2
network: host

dockerfile cannot build: CONDA env create

Hi there I'm new to Docker and Dockerfiles in general,
However, I will need to create one in order to load an application on a server using WDL. With that said, there are few important aspects of this Dockerfile:
requires to create a Conda environment
in there I have to install Snakemake (through Mamba)
finally, I will need to git clone a repository and follow the steps to generate an executable for the application, later invoked by Snakemake
Luckily, it seems most of the pieces are already on dockerhub; correct if I'm wrong based on the script (see below)
# getting ubuntu base image & anaconda3 loaded
2 FROM ubuntu:latest
3 FROM continuumio/anaconda3:2021.05
4 FROM condaforge/mambaforge:latest
5 FROM snakemake/snakemake:stable
6
7 FROM node:alpine
8 RUN apk add --no-cache git
9 RUN apk add --no-cache openssh
10
11 MAINTAINER Name <email>
12
13 WORKDIR /home/xxx/Desktop/Pangenie
14
15 ## ACTUAL PanGenIe INSTALLATION
16 RUN git clone https://github.com/eblerjana/pangenie.git /home/xxx/Desktop/Pangenie
17 # create the environment
18 RUN conda env create -f environment.yml
19 # build the executable
20 RUN conda activate pangenie
21 RUN mkdir build; cd build; cmake .. ; make
First, I think that loading also Mamba and Snakemake would allow me to simply launch the application, as the tools are already set-up by the Dockerfile. Then, I ideally would like to build from the repository the executable, still I get an error at line 18 when I try to create a Conda environment, this is what I get:
[+] Building 1.7s (10/10) FINISHED
[internal] load build definition from Dockerfile
0.1s => => transferring dockerfile: 708B 0.1s => [internal] load .dockerignore 0.1s => => transferring context: 2B 0.1s => [internal] load metadata for docker.io/library/node:alpine 1.4s => [auth] library/node:pull token for registry-1.docker.io 0.0s => [stage-4 1/6] FROM docker.io/library/node:alpine#sha256:1a04e2ec39cc0c3a9657c1d6f8291ea2f5ccadf6ef4521dec946e522833e87ea
0.0s => CACHED [stage-4 2/6] RUN apk add --no-cache git 0.0s => CACHED [stage-4 3/6] RUN apk add --no-cache openssh 0.0s => CACHED [stage-4 4/6] WORKDIR /home/mat/Desktop/Pangenie 0.0s => CACHED [stage-4 5/6] RUN git clone https://github.com/eblerjana/pangenie.git /home/mat/Desktop/Pangenie
0.0s => ERROR [stage-4 6/6] RUN conda env create -f environment.yml 0.1s
[stage-4 6/6] RUN conda env create -f environment.yml:
#10 0.125 /bin/sh: conda: not found executor failed running [/bin/sh -c conda env create -f environment.yml]: exit code: 127
Now, I'm not really experienced as I said, and I spent some time looking for a solution and tried different things, but nothing worked out... if anyone has an idea or even suggesions on how to fix this Dockerfile, please let me know.
Thanks in advance!

Get container ID from Docker buildkit for interactive debugging

It's commonly known that you can run docker commit against a failed build process to take a snapshot of a container for debugging purposes. The container ID is gleaned from the running in <ID> text. However, this text is not emitted during builds that happen with Docker's newer BuildKit buildx functionality.
I tried using --progress plain on the Docker build command, but that hasn't shown me the container IDs. Plus, I cannot run a new container from the image layer IDs (SHA hashes) that are spit out.
Sample BuildKit Output
Using this command:
#1 [internal] load build definition from Dockerfile
#1 sha256:0e70418d547c3ccb20da7b100cf4f69564bddc416652e3e2b9b514e9a732b4aa
#1 transferring dockerfile: 32B done
#1 DONE 0.0s
#2 [internal] load .dockerignore
#2 sha256:396b2cfd81ff476a70ecda27bc5d781bd61c859b608537336f8092e155dd38bf
#2 transferring context: 34B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/node:latest
#3 sha256:1c0b05b884068c98f7acad32e4f7fd374eba1122b4adcbb1de68aa72d5a6046f
#3 DONE 0.0s
#4 [1/4] FROM docker.io/library/node
#4 sha256:5045d46e15358f34ea7fff145af304a1fa3a317561e9c609f4ae17c0bd3359df
#4 DONE 0.0s
#5 [internal] load build context
#5 sha256:49d7a085caed3f75e779f05887e53e0bba96452e3a719963993002a3638cb8a3
#5 transferring context: 35.17kB 0.0s done
#5 DONE 0.1s
#6 [2/4] ADD [trevortest/*, /app/]
#6 sha256:6da32965a50f6e13322efb20007ff49fb0546e2ff55799163b3b00d034a62c57
#6 CACHED
Question: How can I obtain the container IDs of the build process, during each step, specifically when using Docker BuildKit?
The BuildKit works differently than the legacy docker build system. At the moment, there is no direct way to spawn a container from a step in the build and troubleshoot it.
To use the BuildKit potential up to the maximum, best approach is to organize the builds in smaller logical stages. Once the build is organized in this way, When running the builds, you can specify that you want to stop at a certain stage by using --target. When the target is specified, Docker creates an image with the results of the build up to that stage. You can use this container to further troubleshoot in the same way that was possible with the old build system.
Take this example. Here I have 4 stages out of which 2 are parallel stages:
FROM debian:9.11 AS stage-01
# Prepare for installation
RUN apt update && \
apt upgrade -y
FROM stage-01 as stage-02
# Install building tools
RUN apt install -y build-essential
FROM stage-02 as stage-02a
RUN echo "Build 0.1" > /version.txt
FROM stage-02 as stage-03
RUN apt install -y cmake gcc g++
Now you can use the --target option to tell Docker that you want to stop at the stage-02 as follows:
$ docker build -f test-docker.Dockerfile -t test . --target stage-02 [+] Building 67.5s (7/7) FINISHED
=> [internal] load build definition from test-docker.Dockerfile 0.0s
=> => transferring dockerfile: 348B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/debian:9.11 0.0s
=> [stage-01 1/2] FROM docker.io/library/debian:9.11 0.0s
=> CACHED [stage-01 2/2] RUN apt update && apt upgrade -y 0.0s
=> [stage-02 1/1] RUN apt install -y build-essential 64.7s
=> exporting to image 2.6s
=> => exporting layers 2.5s
=> => writing image sha256:ac36b95184b79b6cabeda3e4d7913768f6ed73527b76f025262d6e3b68c2a357 0.0s
=> => naming to docker.io/library/test 0.0s
Now you have the image with the name test and you can spawn a container to troubleshoot.
docker run -ti --rm --name troubleshoot test /bin/bash
root#bbdb0d2188c0:/# ls
Using multiple stages facilitates the troubleshooting, however it really speeds up the build process since the parallel branches can be build on different instances. Also, the readability of the build file is significantly improved.

Building alpine based images for PowerPC (PPC64le) fails when trying to run apk add

Adding any apk packages whilst building docker images for target platform linux/ppc64le results in a "bad signature" error.
6 0.470 (1/1) Installing sudo (1.8.27-r0) 6 0.537 ERROR: sudo-1.8.27-r0: BAD signature
I have tried many packages and all of them results in this error. I have however been successful if I use alpine version 3.8 and below.
Im doing a docker build using buildx on my Macbook Pro (X86) I can successfully build docker images for operating systems ubuntu and debian from my macbook for PPC64le, but not for alpine version 3.9 and above.
Dockerfile
FROM alpine
RUN apk update
RUN apk add sudo
Docker build command
docker buildx build -t alpine_test . --platform=linux/ppc64le --load
I expect this simple build script to build a simple alpine docker image for linux/ppc64le architecture with the sudo package installed.
However I get the following error during the build process:
[+] Building 3.6s (6/6) FINISHED => [internal] load build
definition from Dockerfile 0.0s => => transferring dockerfile: 81B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/alpine:latest 2.9s => [1/3] FROM docker.io/library/alpine#sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb 0.0s => => resolve docker.io/library/alpine#sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb 0.0s => CACHED [2/3] RUN apk update 0.0s
=> ERROR [3/3] RUN apk add sudo 0.7s
[3/3] RUN apk add sudo:
6 0.452 (1/1) Installing sudo (1.8.27-r0)
6 0.566 ERROR: sudo-1.8.27-r0: BAD signature
#6 0.577 1 error; 6 MiB in 14 packages
failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to build LLB: executor failed running [/bin/sh -c apk add sudo]: exit code: 1

Resources