Passing Build-Arg Values during the Build Process
To pass build arguments (build-arg) during the Docker image build process, you can use the --build-arg
flag when running the docker build
command. This allows you to dynamically set the values of the build arguments defined in your Dockerfile.
Passing Build-Arg Values
The general syntax for passing build-arg values is as follows:
docker build --build-arg < arg-name > = < arg-value > -t < image-name > .
Here, <arg-name>
is the name of the build argument defined in your Dockerfile, and <arg-value>
is the value you want to assign to that argument.
For example, let's say you have a Dockerfile with the following build argument:
ARG PYTHON_VERSION=3.9
FROM python:$PYTHON_VERSION-slim
## Rest of the Dockerfile
To build the image with a different Python version, you can run:
docker build --build-arg PYTHON_VERSION=3.10 -t my-custom-image .
This will use the Python 3.10 base image instead of the default 3.9.
Multiple Build-Arg Values
You can pass multiple build-arg values in a single docker build
command by using the --build-arg
flag multiple times:
docker build --build-arg PYTHON_VERSION=3.10 --build-arg APP_NAME=my-app -t my-custom-image .
This will set both the PYTHON_VERSION
and APP_NAME
build arguments during the build process.
Passing Build-Arg Values from Environment Variables
Instead of hardcoding the build-arg values in the docker build
command, you can also pass them from environment variables. This can be useful for automating the build process or for keeping sensitive information (like API keys) out of your Dockerfile.
export PYTHON_VERSION=3.10
export APP_NAME=my-app
docker build --build-arg PYTHON_VERSION --build-arg APP_NAME -t my-custom-image .
By mastering the art of passing build-arg values, you can create highly customizable and reusable Docker images that cater to your specific needs.