0

I want to make the FROM instruction in my Dockerfile dynamic based on an environment variable, so that I can build images based on different versions of a base image without having to change the Dockerfile itself. Here's an example of what I'm trying to do:

FROM php:${PHP_VERSION}-fpm

In this example, I want to use the PHP_VERSION environment variable to specify the version of the php image that my image should be based on. However, when I try to build my image using this Dockerfile, I get an error that says:

invalid reference format

Is it possible to use an environment variable in the FROM instruction of a Dockerfile? If so, how can I do it?

ulou
  • 5,542
  • 5
  • 37
  • 47
Khaled Boussoffara
  • 1,567
  • 2
  • 25
  • 53
  • yes thank you i was trying docker-compose up -d --build --build-arg PHP_VERSION=${PHP_VERSION} it doesn't woek because a problem in my system, that's why i post the question – Khaled Boussoffara Apr 24 '23 at 12:05

1 Answers1

1

Yes, use ARG and ENV:

ARG PHP_VERSION
ENV PHP_VERSION ${PHP_VERSION}

FROM php:${PHP_VERSION}-fpm
ulou
  • 5,542
  • 5
  • 37
  • 47