Developing A Symfony Project With Docker on Mac Os

Süleyman Aydoslu
suleyman-aydoslu
Published in
5 min readMay 31, 2020

Docker is a tool that created to make it much more easier to create, deploy, and run applications by using containers that you created by your own desires. It can be really useful for your team’s development environment. Every team member doesn’t have to setup lots of stuff to make development. When you create a docker setup for your project, your team only will install docker and after build containers, it’s all be fine for magic.

In this article we will create a docker setup for a Symfony 5 project with some beautiful docker images. These images are PHP, Mysql and Nginx.

In the first place we will create a symfony skeleton project.

composer create-project symfony/website-skeleton dockersymfony5

After you run this command you will have Symfony 5 project easily. I will only create a default landing page for now.

Then if you don’t have docker in your system you need to go https://docs.docker.com/get-docker/ address and download docker for your operating system.

After you install docker, you need to create a config file to create your docker container. We need to create docker-compose.yml file for this:

cd dockersymfony5
touch docker-compose.yml

In this config file we firstly specify which docker version will be used. You can find a detailed list at https://docs.docker.com/compose/compose-file/compose-versioning/ here. We will use version 3 for this example.

After specify our version we will make configs for our PHP image.

In this config we say, in order to build our PHP image use Dockerfile, and in our project’s src folder links to var/www/app. In ports breakdown we say 9003 port in our local machine links to 9000 port in docker container. To build a bridge our computer and docker container ports are really important.

Now we will create our Dockerfile in docker/php folder.

cd dockersymfony5
mkdir docker/php
touch Dockerfile

In this file we say go and pull php:7.2-fpm image from docker hub, install composer and create a working directory as var/www/app.

We need to reach our project that works in our container from a browser. So we have to configure our Nginx or Apache configuration. In this article i will use Nginx like:

Allright, now we say go and pull nginx:stable image from docker hub and 8088 port in my computer reach 80 port in container and this is image will be reach my src folder. But i need to create an nginx configuration and this should be reachable by container. In docker/nginx/default.conf file i will write down this and it will be read by container.

This is a real simple nginx configuration for a Symfony project. After all this i’m ready to compose my container. In the first place we will use these commands:

docker-compose build && docker-compose up -d

After run this command your containers will be created.

In my computer because of i’ve already downloaded php and nginx images, docker didn’t download it again but in the first place in it will be downloaded in your computer. After these, we will go to http://localhost:8088/index and it works!

Mostly we use Mysql as our database and now we’ll pull mysql image like:

With environment breakdown we specify our credentials and via 4306 port we can reach mysql like:

In our Symfony env file we can reach mysql like:

DATABASE_URL=mysql://root:secret@127.0.0.1:4306/symfonytest

So we created a base development environment in our project and now we will reach our containers and make some workout.

HOW TO LIST CONTAINERS?

We can list our containers and get some data about them by this command:

docker ps

after this command you see:

HOW TO GET INTO A CONTAINER?

ContainerId is so important for us because we can reach php container via this id. In order to reach PHP container you only need to write down:

docker exec -it 40d653361309 /bin/bash

So after run the command you’re in container and if you want you can clear cache, composer update etc.

HOW TO LIST ALL IMAGES YOU’VE DOWNLOADED BEFORE?

You can list all the images by this command:

docker images

It will show you all images detailed like:

HOW TO REMOVE A IMAGE BY ID?

After you pull lots of images, these files keep too much space on your drive. You can clean up sometimes these images by:

docker rmi 6dffd0f61b50

This “6dffd0f61b50" id is IMAGE ID when you list your images.

HOW TO REMOVE ALL IMAGES?

docker rmi $(docker images -q)

HOW TO APPLY CHANGES AFTER UPDATE MY DOCKER COMPOSE FILE?

If you update some config you don’t have to build containers again, you can only run docker-compose build command to apply changes.

HOW TO STOP ALL CONTAINERS?

docker stop $(docker ps -a -q)

HOW TO REMOVE ALL CONTAINERS?

docker rm $(docker ps -a -q)

HOW TO START AND RESTART A CONTAINER?

docker start 40d653361309
docker restart 40d653361309

HOW TO INSTALL AND ENABLE A PHP EXTENSION ?

You can docker-php-ext-install library but you need to write down this in your Dockerfile like:

RUN pecl install mongodbRUN docker-php-ext-enable mongodb

Don’t forget to run docker-compose build after this update.

Thanks for all for reading this article. In the next article we’ll practice about how can we increase our docker container’s performance.

--

--