1. Declare the variables in .env
PHPFPM_IMAGE=php:7.2-fpm USER_ID=1000
2. Pass the variables to Dockerfile using args in docker-compose.yml
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
args:
PHPFPM_IMAGE: ${PHPFPM_IMAGE}
USER_ID: ${USER_ID}
3. Use the keyword ARG to receive the variables in Dockerfile
# ARG to use WITH "FROM" ARG PHPFPM_IMAGE FROM $PHPFPM_IMAGE # ARG to use UNDER FROM ARG USER_ID # Declare again if you want to the variable UNDER FROM ARG PHPFPM_IMAGE
Just make sure that you put the ARG keyword in the right place. In your Dockerfile, if you specify ARG before the FROM instruction, ARG is not available in the build instructions under FROM. If you need an argument to be available in both places, also specify it under the FROM instruction.
Ref: https://docs.docker.com/compose/compose-file/