Is it possible to specify build options from within a Dockerfile to control the build process?

3/23/2021

Consider a the following docker command, which builds an image from a Dockerfile:

docker image build --network host -t test -f Dockerfile .

Is it possible to specify options of docker image build in the Dockerfile instead of the command-line (in this case --network host)?

This could be useful, since the host running docker image build ... uses a set of fixed flags, which would overriden by custom flags.

-- Shuzheng
cloud
containers
docker
kubernetes

1 Answer

3/23/2021

In general, no.

In this particular case (network access): kinda, actually, using BuildKit, a new build system for Docker.

If you're using BuildKit (export DOCKER_BUILDKIT=1) you can add a comment at the top of the Dockerfile to enable newer syntax. And you can specify different versions of the syntax, which basically works by downloading a new builder, implemented a Docker image.

(A lot more details here: https://pythonspeed.com/articles/docker-buildkit/).

The latest experimental BuildKit syntax has an option for setting network access per build step. Scroll to bottom of https://hub.docker.com/r/docker/dockerfile/ for details, but short version:

  1. Add #syntax=docker/dockerfile:1.2-labs as first line of Dockerfile.
  2. Change RUN mycommand to RUN --network=host mycommand.
-- Itamar Turner-Trauring
Source: StackOverflow