Menu

Sponsored By: Password Angel - Share passwords, API keys, credentials and more with secure, single-use links.

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

Note: We're mounting your hosts global .gitconfig into the container via -v ${HOME}/.gitconfig:/root/.gitconfig as the Symfony command sets up the git repository and needs to know who you are (i.e. the git configuration values for user.name and user.email).

This will output

* Creating a new Symfony project with Composer
  (running /usr/local/bin/composer create-project symfony/skeleton /app  --no-interaction)

* Setting up the project under Git version control
  (running git init /app)

  (running /usr/local/bin/composer require webapp --no-interaction)

 [OK] Your project is now ready in /app

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

References

Enjoyed this article?

Thank you for reading this article! If you found the information valuable and enjoyed your time here, consider supporting my work by buying me a coffee. Your contribution helps fuel more content like this. Cheers to shared knowledge and caffeine-fueled inspiration!

Buy me a coffee

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

Subscribe to my newsletter...

... and receive the musings of an aspiring #indiehacker directly to your inbox once a month.

These musings will encompass a range of subjects such as Web Development, DevOps, Startups, Bootstrapping, #buildinpublic, SEO, personal opinions, and experiences.

I won't send you spam and you can unsubscribe at any time.