This is a workaround process for someone using a docker container. The basic idea will also work for anyone on a unix/bsd os filesystem.
In my case, I have a container that maps the project directory with some existing files mapped to both php and apache containers in a docker-compose.yml
file like this:
php:
build: './php'
restart: always
networks:
- backend
volumes:
- ./:/usr/local/apache2/htdocs
- ./tmp:/usr/local/tmp
This assumes that in the project directory there's a /php directory with a Dockerfile
containing something like this:
FROM php:7.4.33-fpm-alpine3.16
RUN apk update; \
apk upgrade; \
apk add bash; \
apk add shadow;
RUN docker-php-ext-install mysqli opcache pdo pdo_mysql
RUN apk add freetype libpng libjpeg-turbo freetype-dev libpng-dev libjpeg-turbo-dev zlib-dev icu-dev
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-install intl
COPY ./conf/php.ini /usr/local/etc/php/php.ini
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
For my needs I am still using php 7.x, but the idea will work with the latest PHP. The important thing to note is that the PHP container has composer added from the official composer container.
Once the containers are started, I will use docker-compose ps
to get the php container name, which will be something like project-php-1
in most cases.
Then use docker to exec into the container with docker exec -it project-php-1 /bin/bash
This should drop you into the container, with your project directory mapped to /usr/local/apache2/htdocs
cd /usr/local/apache2/htdocs
cd ..
Now run the composer create command exactly as you would if you were in the local directory where you would otherwise have composer create a new project, and name it whatever you would have normally. I'll just use the same project/folder name because there's no conflict in the container.
For example this would init the skeleton needed for a symphony 5.x project:
composer create-project symfony/skeleton:"^5.4" myproject
No you'll have a directory in the container named "myproject"
cd into that and use the cp command to recursively copy all contents to the mapped /usr/local/apache2/htdocs directory (which will be your actual existing project directory).
cd myproject
cp -R * ../htdocs
You will now have all the files needed in your project to continue working.
I also have a .gitignore already copied in my project directory, so that I ignore directories I don't want in git.
It looks something like this:
vendor
tmp
var
.idea
.buildpath
.project
.env
apache-local.conf
At this point, you now have all the files and directories you need to continue with development. You can delete the directory composer made in the container as you no longer need it.
This same idea could of course be used to create the directory and copy the files and directories made by composer to an existing project, just so long as you are aware of any potential naming conflicts, and the possibility you might overwrite any existing files or directories.