Dockerfile does not download the specified image

Docker-Compose is not downloading the specific version of PHP and Nginx that I want. I want the version “php:8.4.5-fpm” and it only downloads the “latest” version. I tried several things, but I can’t get it to download the specific image, it only downloads the “latest” image.

docker-compose

version: "3.9"

services:
  nginx:
    build:
      context: ../nginx  # Caminho para a pasta Nginx (relativo à pasta docker-compose)
    ports:
      - "80:80"
    volumes:
      - ../app:/var/www/html  # Monta a pasta app como /var/www/html (relativo à pasta docker-compose)
    depends_on:
      - php
    networks:
      - laravel-network

  php:
    build:
      context: ../php  # Caminho para a pasta PHP (relativo à pasta docker-compose)
    expose:
      - 9000
    volumes:
      - ../app:/var/www/html  # Monta a pasta app como /var/www/html (relativo à pasta docker-compose)
    depends_on:
      - db
    networks:
      - laravel-network

  db:
    image: mariadb:11.7.2
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel
      MYSQL_PASSWORD: laravel
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - laravel-network

  phpmyadmin:
    image: phpmyadmin:latest
    ports:
      - "8080:80"
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: root
    depends_on:
      - db
    networks:
      - laravel-network

volumes:
  db_data:

networks:
  laravel-network:
    driver: bridge

Dockerfile PHP

FROM bitnami/php-fpm:8.4.6

# Definir diretório de trabalho
WORKDIR /var/www/html

# Instalar dependências do sistema
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    unzip \
    git \
    curl \
    libzip-dev \
    libonig-dev \
    libxml2-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Instalar extensões PHP necessárias para o Laravel
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl soap
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd

# Instalar o Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Criar usuário para a aplicação Laravel
RUN groupadd -g 1000 www && useradd -u 1000 -ms /bin/bash -g www www

# Copiar o código da aplicação
COPY --chown=www:www . /var/www/html

# Alterar usuário
USER www

# Expor a porta 9000 para o PHP-FPM
EXPOSE 9000

CMD ["php-fpm"]

Dockerfile Nginx

FROM nginx:1.27.3

# Copiar a configuração do Nginx

COPY default.conf /etc/nginx/conf.d/default.conf
default.conf

    server {
        listen 80;
        index index.php index.html;
        server_name localhost;
        root /var/www/html/public;
        error_log /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }

        location ~ /\.ht {
            deny all;
        }
    }

A Dockerfile doesn’t do anything, it is just a description of the image you want to build. It is not clear how you build that image, but if you already ran the project once using docker compose up -d, images will not be rebuilt, so if you had the latest images, nothing will change until you add --build like:

docker compose up  -d --build

If it doesn’t help, please, explain how you build the image exactly and how you test the versions.

I want to download the specific image and build. Am I doing something wrong?

You will need to explain better what you are doing, otherwise I can only say what I already wrote in my previous post. Including the question at the end.

If you didn’t udnerstand my previous message, please, share which part was not clear.

in your PHP Dockerfile I see FROM bitnami/php-fpm:8.4.6, is this image contained in docker image ls
? Can you please provide the output of docker image ls

Recent image builders don’t tag the base image layer, so even if the layer is there, it won’t be shown in the output of docker image ls unless buildkit was disabled to use the legacy builder. It doesn’t mean the image layers were not downloaded, they were just not tagged, but I don’t think that is the issue here.

Ok, I think I didn’t really understand the problem. Maybe the questioner can clarify again.

I’m trying to create a stack to study and learn Laravel. I want to download specific images. At this moment, I want to use this image with this exact version, but Docker always pulls the latest version instead.

vini@DESKTOP-K2QIPHU:/mnt/c/Docker-

Localhost/centraldocavaco$ docker image ls
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
centraldocavaco-php   latest    361b56c91a3c   2 minutes ago   799MB
mariadb               11.7.2    9f3d79eba61e   2 months ago    328MB
phpmyadmin            latest    7a9597477072   3 months ago    570MB

Dockerfile

FROM serversideup/php:8.4.6-fpm-nginx

WORKDIR /app

# Usar o usuário root para instalar pacotes
USER root

# Instalar dependências necessárias e o Node.js
RUN apt-get update && apt-get install -y curl gnupg unzip zip git && \
    curl -fsSL https://deb.nodesource.com/setup_22.x | bash - && \
    apt-get install -y nodejs && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# Definir o PATH explicitamente (por precaução)
ENV PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"

# Definir o PATH corretamente no Dockerfile
ENV PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin"

# Definir o PATH explicitamente
ENV PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/composer/vendor/bin"

# Retornar ao usuário padrão
USER www-data

docker-compose

services:
  php:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "80:8080"
    environment:
      - SSL_MODE=off
    volumes:
      - ./app:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - laravel-network

  mariadb:
    container_name: mariadb
    image: mariadb:11.7.2
    environment:
      MARIADB_ROOT_PASSWORD: root
      MARIADB_DATABASE: laravel
      MARIADB_USER: laravel
      MARIADB_PASSWORD: laravel
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - laravel-network

  phpmyadmin:
    container_name: phpmyadmin
    image: phpmyadmin:latest
    ports:
      - "8181:80"
    environment:
      PMA_HOST: mariadb
      MARIADB_ROOT_PASSWORD: root
    depends_on:
      - mariadb
    networks:
      - laravel-network

networks:
  laravel-network:
    driver: bridge

volumes:
  db_data:

Your php image is build inside compose, it will have latest tag automatically.

You use image mariadb:11.7.2, so that container has the matching version.

You use image phpmyadmin:latest, so that container also has latest.

What did you expect to be different?

I want to download specific image from php and instead download “latest”
I want to download the specific image from php but it only downloads “latest”

You download a specific image version, but with the Dockerfile you build a new extended image within compose, which will have latest tag.

How do I use this specific image downloaded from the Dockerfile?

serversideup/php:8.4.6-fpm-nginx

For php you create a new image via the build section in compose:

The Dockerfile uses serversideup/php:8.4.6-fpm-nginx as base image, but adds more layers, which results in a new image.

If you just want to use the base image, change compose to:

services:
  php:
    image: serversideup/php:8.4.6-fpm-nginx

I hope @bluepuma77’s explanation already made it clear, but since you have not responded yet, let me explain it from a different angle.

Just because you put a sheet on the bed, it doesn’t mean the bed is missing. It is under the sheet, and the image you define in the Dockerfile is under you changes. When you build your own image, it doesn’t mean you change the original image. You can never change an existing image. You use the chosen image as a base, and add new layers to it as bluepuma77 explained. The base image and your added layers together will be a new image. And how that new image is tagged (what version tag it gets) is up to you.

Docker Compose will automatically name your image and the full image name will include the project name and service name as well with “latest” as version tag. It is irrelevant as the whole image name was autogenerated.

If you want the original version to be part of your new image name, you can set it manually

  php:
    image: projectname-php:8.4.6-fpm-nginx
    build:
      context: .
      dockerfile: Dockerfile

Or read the version tag from a common variable:

.
├── .env
├── compose.yml
└── Dockerfile

.env

PHP_VERSION_TAG=8.3-fpm

compose.yml

services:
  php:
    image: projectname-php:${PHP_VERSION_TAG:?PHP_VERSION_TAG must be set}
    build:
      context: .
      args:
        PHP_VERSION_TAG: $PHP_VERSION_TAG

Dockerfile

ARG PHP_VERSION_TAG

FROM php:${PHP_VERSION_TAG}

RUN echo "HELLO"

If you forget to set the variable, you will get an error message like this:

error while interpolating services.php.image: required variable PHP_VERSION_TAG is missing a value: PHP_VERSION_TAG must be set

Thanks, friend! Solved!!!