Search code examples
phpdockerdockerfilefpm

correct syntax for docker file alpine with --platform and php fpm-alpine


What is the correct syntax to add a PHP version in docker file?

i have this syntax:

FROM --platform=linux/x86-64 alpine:3.11 php:7.4-fpm-alpine

WORKDIR /app

ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8

RUN apk --no-cache add tzdata && \
    cp /usr/share/zoneinfo/Etc/UTC /etc/localtime && \
    echo "UTC" | tee /etc/timezone && \
    apk del tzdata

RUN apk --update add wget \
    curl \
    git \
    php7 \
    php7-opcache \
    php7-ctype \
    php7-xml \
    php7-xmlreader \
    php7-xmlwriter \
    php7-tokenizer \
    php7-pcntl \
    php7-json \
    php7-dom \
    php7-zip \
    php7-gd \
    php7-curl \
    php7-mbstring \
    php7-redis \
    php7-posix \
    php7-mcrypt \
    php7-iconv \
    php7-pdo_mysql \
    php7-phar \
    php7-simplexml \
    php7-openssl \
    php7-sockets \
    php7-fileinfo && rm /var/cache/apk/*

The problem with this syntax is I am getting an error on this line: FROM --platform=linux/x86-64 alpine:3.11 php:7.4-fpm-alpine

What is the correct syntax to add a PHP version in my docker file?

Thanks!


Solution

  • FROM only accepts one or three arguments and --platform=linux/x86_64 is a modifier not an argument so split it onto two lines.

    FROM --platform=linux/x86_64 alpine:3.11
    FROM php:7.4-fpm-alpine
    

    Although, according to the Official Docs, you probably only want FROM php:7.4-fpm-alpine unless you have another reason for wanting things that are available on the alpine:3.11 image that are not on the php:7.4-fpm-alpine image.

    Adding my changes to the your Dockerfile results in the below. I have tested this and docker build ./ works with this file in an empty directory.

     FROM --platform=linux/x86-64 alpine:3.11 
     FROM php:7.4-fpm-alpine
     
     WORKDIR /app
     
     ENV LC_ALL en_US.UTF-8
     ENV LANG en_US.UTF-8
     
     RUN apk --no-cache add tzdata && \
         cp /usr/share/zoneinfo/Etc/UTC /etc/localtime && \
         echo "UTC" | tee /etc/timezone && \
         apk del tzdata
     
     RUN apk --update add wget \
         curl \
         git \
         php7 \
         php7-opcache \
         php7-ctype \
         php7-xml \
         php7-xmlreader \
         php7-xmlwriter \
         php7-tokenizer \
         php7-pcntl \
         php7-json \
         php7-dom \
         php7-zip \
         php7-gd \
         php7-curl \
         php7-mbstring \
         php7-redis \
         php7-posix \
         php7-mcrypt \
         php7-iconv \
         php7-pdo_mysql \
         php7-phar \
         php7-simplexml \
         php7-openssl \
         php7-sockets \
         php7-fileinfo && rm /var/cache/apk/*
    

    As an additional recommendation, and this is more a "consistency" thing than a "your build will break soon" because PHP 7.4 is in LTS and there will be no PHP 7.5. You may want to change FROM php:7.4-fpm-alpine to FROM php:7-fpm-alpine as you're intstalling php7-* packages which will technically take the latest compatible version 7 package but your locking your php-fpm version to the latest 7.4 version.