Boot Symfony From Scratch With a Single Command
Tags: symfony, php, docker
Published: 2020-05-11 13:54:31

As promised:

```bash
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`](https://docs.docker.com/engine/reference/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`](https://docs.docker.com/engine/reference/run/#foreground) options provide us a better bash experience with an interactive shell.

The [`docker run --rm`](https://docs.docker.com/engine/reference/run/#clean-up---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`](https://docs.docker.com/engine/reference/run/) and [`bash -c` option](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Invoking-Bash). 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](https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup#_your_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].

[Docker]: https://www.docker.com/
[container]: https://www.docker.com/resources/what-container
[image]: https://docs.docker.com/get-started/overview/#images
[Symfony]: https://symfony.com/
[Symfony CLI]: https://symfony.com/download
[solune/symfony]: https://hub.docker.com/r/solune/symfony
[Composer]: https://getcomposer.org/
[Git]: https://git-scm.com/
[docker port mapping]: https://docs.docker.com/engine/reference/run/#expose-incoming-ports