Docker-Compose Args (Variable Expansion) in Multi-stage Builds

Viewed 274

I am trying to use docker-compose ARGs in multi-stage build context. My dockerfile looks like this:

ARG PHP_VERSION

FROM php:${PHP_VERSION}

ARG COMPOSER_VERSION

COPY --from=composer:${COMPOSER_VERSION} /usr/bin/composer /usr/bin/composer

The PHP_VERSION argument is working just fine. However, the COMPOSER_VERSION in the fourth line results in the following error:

ERROR: Service 'app' failed to build: invalid from flag value composer:${COMPOSER_VERSION}: invalid reference format.

Any help would be greatly appreciated.

1 Answers

Variable expansion is not supported in COPY --from. See this issue for more details. You can modify your Dockerfile to implement this by defining a stage that you late copy from:

ARG PHP_VERSION
ARG COMPOSER_VERSION

FROM composer:${COMPOSER_VERSION} as composer
FROM php:${PHP_VERSION}

COPY --from=composer /usr/bin/composer /usr/bin/composer
Related