Demo - Docker with Symfony, nginx, and MySQL
Welcome to Docker 101.
You may be here because you want to see what Docker is, and how it could work for you.
You may be here because you already know how to use Docker, but want to know how to use it for working with a Symfony project.
I've got you covered.
There's no better way to understand the power of Docker than to see it in action.
I'm going to show you a bunch of ways Docker can be used. They may not be right for you, but by the end of this short course you will know how to customise the build to fit your own needs.
Let's start with something interesting.
# cd some dir git clone https://github.com/codereviewvideos/docker-symfony-mysql-nginx-example.git
This repository consists of:
The most immediately interesting is the
# docker-compose.yml version: '2' services: db: image: mysql:5.7.17 env_file: - ./.env volumes: - "./volumes/mysql/dev:/var/lib/mysql" networks: crv_dev_network: aliases: - mysql nginx: image: codereviewvideos/nginx.dev ports: - "81:80" restart: always volumes: - "./volumes/nginx/logs:/var/log/nginx/" volumes_from: - php networks: crv_dev_network: aliases: - nginx php: image: codereviewvideos/www.dev restart: always env_file: - ./.env volumes: - "./volumes/php/app/cache:/var/www/dev/app/cache/:rw" - "./volumes/php/app/logs:/var/www/dev/app/logs/:rw" - "./volumes/.composer:/var/www/.composer" - "./app:/var/www/dev" networks: crv_dev_network: aliases: - php networks: crv_dev_network: driver: bridge
If you have done reading on the subject, you may be wondering why I'm showing you a version 2 style
docker-compose.yml file, when version 3 style is newer.
Simply: version 2 is easier to get going with. All the examples right now are in version 2, so if you get stuck, it's easier to find a fix.
Version 3 syntax makes some things appear harder, but in actuality are largely defined to be to your benefit. The benefit becomes clearer a little later, and we shall cover this later in this series.
This configuration specifies that we will have a Database server (MySQL), an nginx web server, and a PHP container where all our code and related dependencies actually run from.
As you can see from the configuration, there is a requirement for an
env_file, of which we have one,
.env. We also need to create this file locally to run this container:
# .env SECRET_KEY=somesupersecretkey DB_HOST=mysql DB_PORT=3306 DB_DATABASE=db_dev DB_USER=dbuser DB_PASSWORD=dbpassword MYSQL_ROOT_PASSWORD=root_dbpassword MAILER_HOST=127.0.0.1 MAILER_USER=~ MAILER_PASSWORD=~ MAILER_PORT=465
With a little squinting, you might recognise these options as being very similar to those found in Symfony's
parameters.yml file by default.
This isn't a coincidence, but we will get into exactly how that works in a different video.
These environment variables need passing to Symfony via
Before we get to that we need to cover the
This is almost a fresh install of the latest version of Symfony (3.3.6 at the time of recording).
This is where you will do all your development work. We will learn ways to customise the directory structure, so don't worry if this style is not to your taste.
There are two changes I made to the standard
symfony new outcome:
app_dev.php file I commented out the security section.
This is less worrisome than may initially appear. We will never be able to access
app_dev.php outside our development environment. We will cover why in a future video. For now it is enough to say that we are using one nginx image for development, and another similar but tweaked version for production.
The changes to
parameters.yml.dist are a little more visible:
# /app/app/config/parameters.yml.dist parameters: database_host: '%env(DB_HOST)%' database_port: '%env(DB_PORT)%' database_name: '%env(DB_DATABASE)%' database_user: '%env(DB_USER)%' database_password: '%env(DB_PASSWORD)%' mailer_transport: "smtp" mailer_host: '%env(MAILER_HOST)%' mailer_user: '%env(MAILER_USER)%' mailer_password: '%env(MAILER_PASSWORD)%' mailer_port: '%env(MAILER_PORT)%' mailer_encryption: "ssl" mailer_auth_mode: "login" # A secret key that's used to generate certain security-related tokens secret: '%env(SECRET_KEY)%'
app/app is intended :)
The variables from our
.env file will be passed to the respective containers, which in turn makes the environment variables available as needed. Again, we will cover this further in a separate video.
Post Clone Install Steps
This means we have two tasks to complete after cloning the repo:
cd app cp app/config/parameters.yml.dist app/app/config/parameters.yml composer install --no-scripts
composer install process completes you can spin up the stack by running:
If you don't have
Make installed then the following command can be used instead:
docker-compose down && \ docker-compose build --pull --no-cache && \ docker-compose \ -f docker-compose.yml \ up -d --remove-orphans
As you do not have the docker images downloaded at this point, this part may take a while depending on your internet connection speed.
Your Symfony site should be available on
Now, I would be telling you a fairy tale if I told you it is this easy in reality.
Perhaps you already don't think this is easy :)
This is in actuality, quite a rigid example designed to show you what's possible. You will need to customise this build, and this series is all about gaining the skills necessary to do so.
After you have played around with the site, stopping the running containers is advised:
There's plenty to cover here, and really this is designed to be a taster / preview only.
Hopefully you like what you see so far.