Dockerfile, docker-compose and the ARG instruction

For the repository I created to manage PrestaShop in dev mode, I wanted to add arguments in the docker-compose.yml to send them to the Dockerfile in order to be able to manage PHP and nodeJS versions.

So I used the args statement in my docker-compose.yml:

version: "3"

services:
    web:
        container_name: ${PROJECT_NAME}_web
        build:
            context: ./conf
            args:
                PHP_VERSION: 7.3
                NODEJS_VERSION: 10
        ports:
            - "80:80"
        volumes:
            - ./www:/app:delegated
        links:
            - db:db

And I would get the arguments from the Dockerfile:

ARG PHP_VERSION
ARG NODEJS_VERSION

FROM php:${PHP_VERSION}-apache

RUN curl -sL https://deb.nodesource.com/setup_$NODEJS_VERSION.x | bash - && apt-get install -y nodejs

However... the argument NODEJS_VERSION did not return any value... because there is a notion of scope on the management of the ARG versus the FROM in the Dockerfile. All the ARG declared before the FROM will not be accessible after the instruction during the build.

Here is how I had to write my Dockerfile to handle this case:

ARG PHP_VERSION
FROM php:${PHP_VERSION}-apache

ARG NODEJS_VERSION

RUN curl -sL https://deb.nodesource.com/setup_$NODEJS_VERSION.x | bash - && apt-get install -y nodejs

For more information: https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact



in Docker Posted on Sep 28, 2021