moby / buildkit

concurrent, cache-efficient, and Dockerfile-agnostic builder toolkit
https://github.com/moby/moby/issues/34227
Apache License 2.0
8.22k stars 1.16k forks source link

Invalid `--add-host` in docker buildx #5075

Open ethan256 opened 4 months ago

ethan256 commented 4 months ago

When I use buildx for multi-architecture compiled images, I find that I can't go the dns configured in /etc/hosts, and adding --add-host still doesn't work either

command is as follows:

docker buildx create --use --bootstrap --driver docker-container --name edgex_device_hj212 > /dev/null 2>&1 || true
docker buildx build --add-host {your_private_registry}:10.xx.xx.xx --load -f Dockerfile  --label "Name=device-hj212 Version=0.0.0-dev" -t {your_private_registry}/gateway/device-hj212:0.0.0-dev 

Error log:

ERROR: failed to solve: {your_private_registry}/docker.io/alpine:3.18: failed to resolve source metadata for {your_private_registry}/docker.io/alpine :3.18: failed to do request: Head "https://{your_private_registry}/v2/docker.io/alpine/manifests/3.18": dial tcp: lookup {your_private_registry} on 208.67.222.222:53: no such host

Why don't we do domain name resolution via add-host here?

ethan256 commented 4 months ago

docker info:

Client: Docker Engine - Community
 Version:    26.1.3
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.14.0
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.27.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose
WARNING: Plugin "/usr/local/lib/docker/cli-plugins/docker-scan" is not valid: failed to fetch metadata: fork/exec /usr/local/lib/docker/cli-plugins/docker-scan: no such file or directory

Server:
 Containers: 1
  Running: 1
  Paused: 0
  Stopped: 0
 Images: 318
 Server Version: 26.1.3
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 8b3b7ca2e5ce38e8f31a34f35b2b68ceb8470d89
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
 Kernel Version: 5.15.153.1-microsoft-standard-WSL2
 Operating System: Ubuntu 22.04.4 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 6
 Total Memory: 11.69GiB
 Name: ethan
 ID: 25e8c8de-fba9-44e4-8e2d-b67f38a4c173
 Docker Root Dir: /var/lib/docker
ethan256 commented 4 months ago

Docker file:

FROM {your_private_registry}/docker.io/alpine:3.18
# TARGETOS and TARGETARCH are set automatically when --platform is provided.
ARG TARGETOS TARGETARCH

LABEL license='SPDX-License-Identifier: Apache-2.0' \
      copyright='Copyright (c) 2019-2021: IoTech Ltd'

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories

RUN apk add --update --no-cache dumb-init
# Ensure using latest versions of all installed packages to avoid any recent CVEs
RUN apk --no-cache --force-missing-repositories upgrade
tonistiigi commented 4 months ago

add-host is for the extra hosts added to build containers (RUN steps in Dockerfiles). Custom configuration for container registries (where I think the error comes from unless you are runnings some container stack inside RUN step) like TLS and mirror config can be defined with buildkit config.

ethan256 commented 4 months ago

buildkit config

add-host is for the extra hosts added to build containers (RUN steps in Dockerfiles). Custom configuration for container registries (where I think the error comes from unless you are runnings some container stack inside RUN step) like TLS and mirror config can be defined with buildkit config.

Thanks for your reply. According to the buildkit config documentation, I don't find any configuration item for DNS. My private repository DNS is configured on the host's /etc/hosts, how can I make this DNS effective?

I found that using --driver-opt network=host in docker buildx create works fine, but I'm not sure if this option enables DNS in /etc/hosts or not!

tonistiigi commented 4 months ago

Are you asking for options to expose some of these flags of docker run container via --driver-opt ?

      --dns list                         Set custom DNS servers
      --dns-option list                  Set DNS options
      --dns-search list                  Set custom DNS search domains

I don't this is enabled atm but could be extended. You can also create a buildkit container with docker run and then add it to buildx, either with remote driver (eg. docker-container:// endpoint) or by creating the container with same name that container driver would create.

ethan256 commented 4 months ago

Are you asking for options to expose some of these flags of docker run container via --driver-opt ?

      --dns list                         Set custom DNS servers
      --dns-option list                  Set DNS options
      --dns-search list                  Set custom DNS search domains

I don't this is enabled atm but could be extended. You can also create a buildkit container with docker run and then add it to buildx, either with remote driver (eg. docker-container:// endpoint) or by creating the container with same name that container driver would create.

Yes, I look forward to this feature.