If you are here, you want to improve your skill in Docker.
I imagine you are actually developing with a third-party application as WAMP/MAMP.
It was a good setup, 5 years ago...
Now, this is a typical bad practice!
The list is far from short :
- You depend on WAMP installation...
- The SSL support sucks...
- The port handling isn't easy to manage
- You're 99% sure you are not iso with the production
Directly on Linux
The only thing I can disagree with is that your computer doesn't containerize your project.
After several coding years, you have installed tons of extensions/packages. It's hard to determine which extensions your project needs.
I know and I agree, you have to know what each extension does. But are you 100% sure you didn't miss anything?
In my opinion, I prefer not to leave anything to chance.
The better you containerize each project, the easier you create your production environment. It's important to have an iso environment between development and production.
Don't worry, it's not that difficult to create your own environment with Docker.
For me, Docker is a must-have for every developer.
Here, I'll show you how to create a Symfony/MariaDB/Nginx environment with Docker!
Let's start a new project called my
Initiate Docker with Docker Compose
The first thing to do is to install Docker. The installation contains Docker and Compose, the 2 tools that we'll use day!
When the installation is over, we start with the first step create the file
It will contain all the services of our stack!
Check out your Docker version (
docker -v) to validate if your Compose version is correct: https://docs.docker.com/compose/compose-file/
This is how Compose works.
Compose uses the
docker-compose.yml to instantiate containers. As the opposite of Docker (without Compose), you can instantiate linked containers at the same time.
It allows you to create a poll of containers for a unique function, and to delete them at the same time when you'll not need them anymore. We call them
Each service is instantiated from an image from Docker Hub (it centralizes all public images).
Moreover, each service has its own parameters (image, environment, ports, network...) which allow to configure them.
As a database, we'll use the latest images from MariaDB.
Don't forget to change all variables with your own.
And you can also add the service
phpMyAdmin if you want an interface to access your database from your browser:
It's all we need to have a database and a front interface!
The PHP image is a bit more complex than those seen previously.
There is no official image that contains all Symfony packages.
So we have to create our own Docker image with all needed packages (composer included) in the file
Dockerfile-php. It's based on the image
php:7.4-fpm, with additional installations.
2 lines are here :
- build: docker-compose will build our custom image in this container
- volumes: link a volume name to a path inside the image.
The important thing here is that you want to manipulate the exact same files. So you have to create a link between your local application and the Docker PHP.
This link is
volumes. Add the following code at the end of the file.
PHP and Symfony are all set!
Finally, we end by setting Nginx to create access to our Symfony project.
Exactly the same way as the PHP image, we'll create our own Nginx image to include Let's Encrypt support if you need to develop with HTTPS protocol.
And we create a custom Nginx configuration file as the default configuration.
The final docker-compose.yml
Wait, I added some parameters to make it fully customizable. To make easier the database dump, we also create a volume for SQL files.
So copy this one to get the final file.
Start the Docker instance
The next step is to build our containers from the docker-compose.yml
Warning: you need to be on the same path as the file to launch the command!
Also, pay attention to the indentation if you don't want to waste hours for silly mistakes.
I recommend you add the parameter
--no-cache at the end of the command line.
When you build, docker-compose doesn't rebuild modification outside the
docker-compose.yml file. If you want, for example, to change the file
default.conf, it won't rebuild.
Now, you can launch:
docker-compose up -d
Here we are, we have access to our instance NGINX:
http://localhost:8000 and even to our PHPMyAdmin:
You can start and stop your containers directly with the Docker UI installed, or you can use those commands
To delete your containers:
docker-compose down. It won't delete your project files linked to Docker since you created a volume.
Launch command in Docker instance
Last and not the less, the command to access our PHP container bash :
docker exec -it myapp_php bash
It will connect you to the container bash. Now you can launch your Symfony command directly in your Docker.
symfony new myapp
Last thing: copy the entire content
myapp in the parent folder
symfony (to match the Nginx configuration file).
Now you have your environment Docker for Symfony! Let's code :)
Isn't it beautiful?