DEV Community

Renan
Renan

Posted on

Boot Symfony From Scratch With a Single Command

As promised:

docker run -it --rm \
    -p 8000:8000 \
    solune/symfony:7.4-cli \
    bash -c "yes n | symfony new --no-git . && yes n | symfony serve"

Application will be available at http://127.0.0.1:8000

You're welcome.

If you want to understand the command above, continue reading.

Behind The Scenes

As you may have noticed, the command is actually a few of well orchestred embedded commands.

The base is docker run. It means we are going to run Symfony inside an isolated container. That's why we don't need enything else as requirement.

The docker run -it options provide us a better bash experience with an interactive shell.

The docker run --rm option automatically removes the container when it exits.

The image selected is solune/symfony, one of my favorites. This image contains almost everything we need to work with Symfony: some PHP extensions, Composer, Git, and Symfony CLI.

Once the container is running, we can install and serve an application. We run actually a script inside the container, thanks to the pair docker run and bash -c option. The script we run inside the container is:

yes n | symfony new --no-git .
yes n | symfony serve

The commands contain yes no. This trick prevents us to be prompted for updating Symfony CLI.

The symfony new command carries yet the --not-git option. This command performs a couple of tasks, which includes the project git set up and an initial commit. It's great, but it doesn't work unless we set up our git identity. Remember we're inside a container. Adding this option we skip these actions.

The symfony serve starts the web server at localhost:8000 that serves our Symfony application. Remember, we are inside the container.

Lastly, the docker run -p 8000:8000 option. This makes the application available for us. This is mandatory because the container's ports aren't reachable from outside unless we ask for it. Note the symfony serve command serves the application under the port 8000, but inside the container. Thanks to the docker port mapping, we can link a port into the container for us. In this case we use the same port number.

Conclusion

It's all about saving time. It's not about a smart shortcut. It's neither about a long script that performs boring tasks.

By simply using Docker we can dive immediately into a more pleasant context. The trick is to know and pick the best-fit start point. In this case I've choose solune/symfony.

Top comments (1)

Collapse
 
ericovasconcelos profile image
ericovasconcelos

Thank you. 😉