Was this helpful? Support me via buymeacoffee.com and help me create lots more great content!

Creating New Symfony Applications with Docker and the Symfony CLI

If you're like me and using docker to manage your development environment then using something like the Symfony CLI to create a new Symfony project won't work using the standard instructions because PHP, composer or some other dependency is not installed or up-to-date on your host machine.

To work around this for a recent project, I created a Symfony CLI Docker Image, built from the PHP image and installs composer & the symfony cli. You can get this image by running

docker pull chrisshennan/symfony-cli

Using this docker image you can follow the installation instructions and replace symfony with docker run chrisshennan/symfony (for the most part - see "Create your new Symfony application" for full instructions) and you will be able to set up a new application.

If you would prefer, you can build the image yourself using the instructions below.

Create the Dockerfile

FROM php:latest

RUN apt-get update

# Install composer (see https://getcomposer.org/download/)
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN mv composer.phar /usr/local/bin/composer

# Install symfony CLI (https://symfony.com/download)
RUN curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.deb.sh' | bash
RUN apt install symfony-cli -y

# Clear apt cache to reduce image size
RUN rm -rf /var/cache/apt/archives /var/lib/apt/lists

ENTRYPOINT ["/usr/bin/symfony"]

Build the docker image

docker build -t chrisshennan/symfony-cli .

Create your new Symfony application

# Create the project directory
mkdir supercoolwidgets.xyz

# Create the symfony application inside the project directory
docker run --rm \
    -v ./supercoolwidgets.xyz:/app \
    -v ${HOME}/.gitconfig:/root/.gitconfig \
    -w /app \
    chrisshennan/symfony-cli new  --webapp /app

If you are using your SSH key to sign your commits then you will also need to mount that into the container as well. You can achieve this by mounting the .ssh folder like

docker run --rm \
    -v ./supercoolwidgets.xyz:/app \
    -v ${HOME}/.gitconfig:/root/.gitconfig \
    -v ${HOME}/.ssh:${HOME}/.ssh \
    -w /app \
    chrisshennan/symfony-cli new --webapp /app

Or if you would rather mount the individual key rather than the whole .ssh directory, you can do

docker run --rm \
    -v ./supercoolwidgets.xyz:/app \
    -v ${HOME}/.gitconfig:/root/.gitconfig \
    -v ${HOME}/.ssh/id_rsa:${HOME}/.ssh/id_rsa \
    -w /app \
    chrisshennan/symfony-cli new --webapp /app

Note: The signingkey defined in .gitconfig is an absolute path so we need to mount it to the key to the same location inside the container, hence the -v ${HOME}/.ssh:${HOME}/.ssh \

Start the PHP Development Web Server

To test the Symfony application has been created and is ready for development, you can run the following command to start the PHP built-in webserver

docker run --rm \
    -v supercoolwidgets.xyz:/app \
    -w /app \
    -p 8888:80 \
    php:latest \
    bash -c "php -S 0.0.0.0:80 -t public/"

and then open a browser and go to http://localhost:8888/ (Note: it's port 8888 and not 80). This should bring up the Symfony default webpage like the one shown below.

Symfony 7 Default Page

Additional Reading

Originally published at https://chrisshennan.com/blog/creating-symfony-applications-with-symfony-cli-and-docker