I am using Azure Devops to build and push my Docker image. How can I pass arguments while doing buildAndPush using Docker task?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you are using the Docker@2 task in Azure DevOps, the important detail is that arguments are ignored when the command is buildAndPush. That means adding --build-arg ... under arguments does not behave the way many people expect. The practical fix is to split the operation into separate build and push steps, then pass build arguments on the build step.
Why buildAndPush Is the Problem
buildAndPush is a convenience command that combines two actions. In the Docker task, that convenience comes with a limitation: the task does not honor the arguments input for that combined command.
So if your pipeline looks correct but the build arguments never reach Docker, the task behavior is the issue, not your Dockerfile syntax.
Split the Pipeline into build and push
The reliable pattern is to run a build task first, passing the build arguments there, then run a separate push task.
This is usually the clearest solution because each step does exactly one thing.
Make Sure the Dockerfile Accepts the Argument
Passing --build-arg only helps if the Dockerfile declares the corresponding ARG.
If the Dockerfile never declares ARG APP_ENV, the pipeline argument has nowhere meaningful to go.
Use Variables Intentionally
Azure DevOps variables are a natural way to keep the build argument value configurable.
That makes the pipeline easier to reuse across environments. It also keeps the YAML cleaner than hardcoding values into the task input directly.
Be Careful with Secrets
Build arguments are not a secret-management boundary. If the value is sensitive, do not assume --build-arg is the right place for it.
That is an important design point because Docker build arguments can end up in build history or image metadata depending on how they are used. Sensitive values usually belong in a proper secret-management path instead.
This matters even more in CI systems, where logs and build metadata may be visible to more people than the final runtime environment.
Prefer Explicit Pipelines Over Convenience Commands When Behavior Matters
The broader lesson is that convenience wrappers are great until you need precise control. As soon as build-time customization matters, separate build and push steps are often easier to understand, troubleshoot, and maintain than a single combined task.
This is not only about Azure DevOps. It is a general CI/CD design principle: use the high-level shortcut only when its behavior is actually sufficient.
Once you need precise control, explicit steps are usually easier to trust.
That tradeoff is usually worth the extra YAML.
Predictability beats convenience in CI pipelines.
Common Pitfalls
- Passing
argumentstobuildAndPushand expecting them to be honored. - Forgetting to declare matching
ARGinstructions in the Dockerfile. - Mixing secrets into Docker build arguments casually.
- Treating convenience tasks as if they always expose the full underlying Docker feature set.
- Debugging the Dockerfile first when the real issue is task behavior in the pipeline.
Summary
- In Azure DevOps
Docker@2,argumentsare ignored forbuildAndPush. - Split the work into separate
buildandpushtasks if you need--build-arg. - Declare matching
ARGinstructions in the Dockerfile. - Use pipeline variables for maintainable argument values.
- Keep sensitive data out of build arguments unless you fully understand the exposure tradeoffs.

