Setting up a project with docker

Requirements

Install docker

Follow the following link for instructions to install docker on Mac

https://docs.docker.com/docker-for-mac/install/

Download and install the symfony binary

Follow the instructions at https://symfony.com/download

Set up the application

Create a symfony application 

Create a sample todo application using symfony new command 

symfony new todo

This will create a minimal web application using symfony.

Change into the project directory

cd todo

You can start the symfony web server using

symfony serve -d 

You can see the application by issuing 

symfony open:local

Now, this application does not have a database configured yet. 

You can see the database configuration in the .env file.

Setting up symfony server with docker database service

Symfony server integrates well with docker

We have to create a database service in docker-compose.yaml. Refer to https://hub.docker.com/_/mysql to create a mysql database service.

The integration documentation for symfony server and docker can be found at https://symfony.com/doc/current/setup/symfony_server.html#docker-integration

After bringing up the database service using docker-compose up -d, Symfony server will now connect to the database service in the docker-compose file.

To check run 

symfony console doctrine:database:create

This will create the default database configured using doctrine configuration.

This will mean though, that you have to use 

symfony console 

And not

bin/console doctrine:database:create 

The above command will work through the conjuration for DATABASE_URL in .env configuration.

To make the app work in both cases, make sure the DATABASE_URL is also configured properly.

This is an easy setup for any local development using symfony.

Another option is to use symfony server as a docker service.

For that, you will have to define an app service , where you put the steps to create the symfony project in the Dockerfile and then give symfony serve  command as the docker-compose service command, without the -d option, so the service will run in the foreground.

To further ease the process, use a Makefile to prepare the project and run the docker-compose commands.

0

(Visited 1 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.