Docker init for Symfony

Hi everyone,
I’m creating a new project in Symfony and trying to create a container for it

symfony new my_project
cd my_project/
symfony server:start

Everything works correctly in the browser

Then I generate the files needed for Docker using
docker init

malgorzata@malgorzata-X556UQK:~/php/my_project$ docker init

Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml
  - README.Docker.md

Let's get started!

? What application platform does your project use? PHP with Apache
? What version of PHP do you want to use? 8.2
? What's the relative directory (with a leading .) for your app? ./src
? What port do you want to use to access your app? 9000

✔ Created → .dockerignore
✔ Created → Dockerfile
✔ Created → compose.yaml
✔ Created → README.Docker.md

→ Your Docker files are ready!
  Review your Docker files and tailor them to your application.
  Consult README.Docker.md for information about using the generated files.

What's next?
  Start your application by running → docker compose up --build
  Your application will be available at http://localhost:9000

I follow the instructions byt I end up with the error

ERROR [server deps 3/3] RUN --mount=type=bind,source=composer.json,target=composer.json     --mount=type=bind,source=composer.lock,target=composer.lock     --mount=type=cache,target=/tmp/cache     composer install --no-dev --no-interaction

1.458 
1.480 Executing script cache:clear [KO]
1.521  [KO]
1.521 Script cache:clear returned with error code 1
1.521 !!  Could not open input file: ./bin/console
1.521 !!  
1.521 Script @auto-scripts was called via post-install-cmd

This is my generated Dockerfile

# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

################################################################################

# Create a stage for installing app dependencies defined in Composer.
FROM composer:lts as deps

WORKDIR /app

# If your composer.json file defines scripts that run during dependency installation and
# reference your application source files, uncomment the line below to copy all the files
# into this layer.
# COPY . .

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a bind mounts to composer.json and composer.lock to avoid having to copy them
# into this layer.
# Leverage a cache mount to /tmp/cache so that subsequent builds don't have to re-download packages.
RUN --mount=type=bind,source=composer.json,target=composer.json \
    --mount=type=bind,source=composer.lock,target=composer.lock \
    --mount=type=cache,target=/tmp/cache \
    composer install --no-dev --no-interaction

################################################################################

# Create a new stage for running the application that contains the minimal
# runtime dependencies for the application. This often uses a different base
# image from the install or build stage where the necessary files are copied
# from the install stage.
#
# The example below uses the PHP Apache image as the foundation for running the app.
# By specifying the "8.2-apache" tag, it will also use whatever happens to be the
# most recent version of that tag when you build your Dockerfile.
# If reproducability is important, consider using a specific digest SHA, like
# php@sha256:99cede493dfd88720b610eb8077c8688d3cca50003d76d1d539b0efc8cca72b4.
FROM php:8.2-apache as final

# Your PHP application may require additional PHP extensions to be installed
# manually. For detailed instructions for installing extensions can be found, see
# https://github.com/docker-library/docs/tree/master/php#how-to-install-more-php-extensions
# The following code blocks provide examples that you can edit and use.
#
# Add core PHP extensions, see
# https://github.com/docker-library/docs/tree/master/php#php-core-extensions
# This example adds the apt packages for the 'gd' extension's dependencies and then
# installs the 'gd' extension. For additional tips on running apt-get, see
# https://docs.docker.com/go/dockerfile-aptget-best-practices/
# RUN apt-get update && apt-get install -y \
#     libfreetype-dev \
#     libjpeg62-turbo-dev \
#     libpng-dev \
# && rm -rf /var/lib/apt/lists/* \
#     && docker-php-ext-configure gd --with-freetype --with-jpeg \
#     && docker-php-ext-install -j$(nproc) gd
#
# Add PECL extensions, see
# https://github.com/docker-library/docs/tree/master/php#pecl-extensions
# This example adds the 'redis' and 'xdebug' extensions.
# RUN pecl install redis-5.3.7 \
#    && pecl install xdebug-3.2.1 \
#    && docker-php-ext-enable redis xdebug

# Use the default production configuration for PHP runtime arguments, see
# https://github.com/docker-library/docs/tree/master/php#configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

# Copy the app dependencies from the previous install stage.
COPY --from=deps app/vendor/ /var/www/html/vendor
# Copy the app files from the app directory.
COPY ./src /var/www/html

# Switch to a non-privileged user (defined in the base image) that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
USER www-data

What should I modify to make it work?

1 Like

To resolve the “Could not open input file: ./bin/console” error, you need to adjust the command structure. Instead of using php bin/console server:run, use php app/console server:run. Aarp Membership This change ensures compatibility with Symfony 2 and resolves the issue.

I’m creating a completely new project in Symfony 7, so it contains a bin folder and a console file.
I set read and write permissions for everyone for the console file (-rwxrwxrwx)

The error appears when I follow the instructions after docker init

What's next?
  Start your application by running → docker compose up --build
  Your application will be available at http://localhost:9000
malgorzata@malgorzata-X556UQK:~/php/my_project$ docker compose up --build
[+] Building 6.1s (15/17)                                                                                                                                                                   docker:desktop-linux
 => [server internal] load build definition from Dockerfile                                                                                                                                                 0.0s
 => => transferring dockerfile: 3.79kB                                                                                                                                                                      0.0s
 => [server] resolve image config for docker-image://docker.io/docker/dockerfile:1                                                                                                                          1.9s
 => [server auth] docker/dockerfile:pull token for registry-1.docker.io                                                                                                                                     0.0s
 => CACHED [server] docker-image://docker.io/docker/dockerfile:1@sha256:a57df69d0ea827fb7266491f2813635de6f17269be881f696fbfdf2d83dda33e                                                                    0.0s
 => [server internal] load metadata for docker.io/library/php:8.2-apache                                                                                                                                    1.1s
 => [server internal] load metadata for docker.io/library/composer:lts                                                                                                                                      2.0s
 => [server auth] library/php:pull token for registry-1.docker.io                                                                                                                                           0.0s
 => [server auth] library/composer:pull token for registry-1.docker.io                                                                                                                                      0.0s
 => [server internal] load .dockerignore                                                                                                                                                                    0.1s
 => => transferring context: 692B                                                                                                                                                                           0.0s
 => [server deps 1/3] FROM docker.io/library/composer:lts@sha256:3bb7af11660f998f448eb0c2752a9175e6e0a5bfc58c1c71de085add3bfb009b                                                                           0.0s
 => [server final 1/4] FROM docker.io/library/php:8.2-apache@sha256:b22afbc5a6d5357b21dfb50a3d3ea8b22e1946b3224bf197d041eec4b13d6766                                                                        0.0s
 => [server internal] load build context                                                                                                                                                                    0.0s
 => => transferring context: 160B                                                                                                                                                                           0.0s
 => CACHED [server deps 2/3] WORKDIR /bin                                                                                                                                                                   0.0s
 => CACHED [server final 2/4] RUN mv "/usr/local/etc/php/php.ini-production" "/usr/local/etc/php/php.ini"                                                                                                   0.0s
 => ERROR [server deps 3/3] RUN --mount=type=bind,source=composer.json,target=composer.json     --mount=type=bind,source=composer.lock,target=composer.lock     --mount=type=cache,target=/tmp/cache     c  1.7s
------                                                                                                                                                                                                           
 > [server deps 3/3] RUN --mount=type=bind,source=composer.json,target=composer.json     --mount=type=bind,source=composer.lock,target=composer.lock     --mount=type=cache,target=/tmp/cache     composer install --no-dev --no-interaction:                                                                                                                                                                                     
0.829 Installing dependencies from lock file                                                                                                                                                                     
0.832 Verifying lock file contents can be installed on current platform.                                                                                                                                         
0.856 Package operations: 32 installs, 0 updates, 0 removals                                                                                                                                                     
0.898     0 [>---------------------------]    0 [>---------------------------]
0.899   - Installing symfony/flex (v2.4.5): Extracting archive
0.940   - Installing symfony/runtime (v7.0.7): Extracting archive
0.952   - Installing psr/cache (3.0.0): Extracting archive
0.952   - Installing symfony/cache-contracts (v3.5.0): Extracting archive
0.955   - Installing symfony/polyfill-mbstring (v1.29.0): Extracting archive
0.955   - Installing symfony/polyfill-intl-normalizer (v1.29.0): Extracting archive
0.957   - Installing symfony/polyfill-intl-grapheme (v1.29.0): Extracting archive
0.961   - Installing symfony/string (v7.0.7): Extracting archive
0.965   - Installing symfony/deprecation-contracts (v3.5.0): Extracting archive
0.970   - Installing psr/container (2.0.2): Extracting archive
0.981   - Installing symfony/service-contracts (v3.5.0): Extracting archive
0.985   - Installing symfony/console (v7.0.7): Extracting archive
0.991   - Installing symfony/dotenv (v7.0.7): Extracting archive
1.001   - Installing psr/event-dispatcher (1.0.0): Extracting archive
1.008   - Installing symfony/event-dispatcher-contracts (v3.5.0): Extracting archive
1.008   - Installing symfony/routing (v7.0.7): Extracting archive
1.008   - Installing symfony/polyfill-php83 (v1.29.0): Extracting archive
1.009   - Installing symfony/http-foundation (v7.0.7): Extracting archive
1.017   - Installing symfony/event-dispatcher (v7.0.7): Extracting archive
1.022   - Installing symfony/var-dumper (v7.0.7): Extracting archive
1.023   - Installing psr/log (3.0.0): Extracting archive
1.024   - Installing symfony/error-handler (v7.0.7): Extracting archive
1.024   - Installing symfony/http-kernel (v7.0.7): Extracting archive
1.024   - Installing symfony/finder (v7.0.7): Extracting archive
1.024   - Installing symfony/process (v7.0.7): Extracting archive
1.025   - Installing symfony/filesystem (v7.0.7): Extracting archive
1.025   - Installing symfony/var-exporter (v7.0.7): Extracting archive
1.026   - Installing symfony/dependency-injection (v7.0.7): Extracting archive
1.027   - Installing symfony/config (v7.0.7): Extracting archive
1.028   - Installing symfony/cache (v7.0.7): Extracting archive
1.029   - Installing symfony/framework-bundle (v7.0.7): Extracting archive
1.029   - Installing symfony/yaml (v7.0.7): Extracting archive
1.051   0/30 [>---------------------------]   0%
1.082  10/30 [=========>------------------]  33%
1.200  24/30 [======================>-----]  80%
1.233  30/30 [============================] 100%
1.454 Generating autoload files
1.474 28 packages you are using are looking for funding.
1.474 Use the `composer fund` command to find out more!
1.481 
1.481 Run composer recipes at any time to see the status of your Symfony recipes.
1.481 
1.496 Executing script cache:clear [KO]
1.539  [KO]
1.539 Script cache:clear returned with error code 1
1.539 !!  Could not open input file: ./bin/console
1.539 !!  
1.540 Script @auto-scripts was called via post-install-cmd
------
failed to solve: process "/bin/sh -c composer install --no-dev --no-interaction" did not complete successfully: exit code: 1

I don’t use any of these commands.
I added the full message from:
docker compose up --build

i have same issue after rebuild
with command docker compose up --build

.485 Executing script cache:clear [KO]
3.495 [KO]
3.495 Script cache:clear returned with error code 1
3.495 !! Could not open input file: ./bin/console
3.495 !!
3.495 Script @auto-scripts was called via post-install-cmd



    "require": {
        "php": ">=8.2",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "api-platform/core": "^3.3",
        "doctrine/dbal": "^3",
        "doctrine/doctrine-bundle": "^2.12",
        "doctrine/doctrine-migrations-bundle": "^3.3",
        "doctrine/orm": "^3.1",
        "nelmio/cors-bundle": "^2.4",
        "phpdocumentor/reflection-docblock": "^5.4",
        "phpstan/phpdoc-parser": "^1.29",
        "symfony/asset": "7.0.*",
        "symfony/console": "7.0.*",
        "symfony/dotenv": "7.0.*",
        "symfony/expression-language": "7.0.*",
        "symfony/flex": "^2",
        "symfony/framework-bundle": "7.0.*",
        "symfony/property-access": "7.0.*",
        "symfony/property-info": "7.0.*",
        "symfony/runtime": "^7.0",
        "symfony/security-bundle": "7.0.*",
        "symfony/serializer": "7.0.*",
        "symfony/twig-bundle": "7.0.*",
        "symfony/validator": "7.0.*",
        "symfony/yaml": "7.0.*"
    },
    "config": {
        "allow-plugins": {
            "php-http/discovery": true,
            "symfony/flex": true,
            "symfony/runtime": true
        },
        "sort-packages": true
    },

i resolved my problem.
i was copping ready project’s foldres to docker, but accedentaly replace my copy . . directive with
COPY composer.* .