DEV Community

Cover image for Laradock - ready to use PHP docker environment
Damian Brdej
Damian Brdej

Posted on • Updated on

Laradock - ready to use PHP docker environment

Laradock is a full PHP development environment for Docker. It contains a variety of popular services such as PostgreSQL, MySQL, Redis, MongoDB, PHPmyAdmin, and so on.

Let's setup empty Symfony project with a PostgreSQL database to see how it works:

First clone project from GitHub repository:

git clone https://github.com/Laradock/laradock.git

Next enter the laradock directory and rename .env.example to .env:

cp .env.example .env

Let's look at the basic settings in the .env file

APP_CODE_PATH_HOST - here you can specify the location of the directory with your project

PHP_VERSION - with this setting you can set up a version of PHP, unfortunately at the time of writing this article the newest available version in laradock is 8.0

I set up the location on my project to ../app and PHP version to 8.0.

Now we can start containers

To do so type: docker-compose up -d nginx postgres - this command will create Nginx and Postgres container. First start-up may take a while but next ones should be quick.


If you get an error that the address is already in use just change the port in the blocking service. For example to change the port of Nginx change value of NGINX_HOST_HTTP_PORT variable in the .env file.


Nginx and PostgreSQL are two of many services available in laradock. Full list of services you can check on the project page


Set up our Symfony project

We need to get into Docker container by typing:
docker exec -it laradock_workspace_1 sh
When we are in a container we can use composer to install Symfony:
composer create-project symfony/skeleton .

After this, we can visit localhost in our browser to see the standard Symfony 404 page.

Symfony 404 page

Setting up PostgreSQL:

In workspace container execute another command:

composer require symfony/orm-pack

and then configure a database connection in Symfony .env, all needed configuration data we can find in laradock .env. The default ones look like this:

POSTGRES_VERSION=alpine
POSTGRES_DB=default
POSTGRES_USER=default
POSTGRES_PASSWORD=secret
POSTGRES_PORT=5432
POSTGRES_ENTRYPOINT_INITDB=./postgres/docker-entrypoint-initdb.d
Enter fullscreen mode Exit fullscreen mode

But what's the host?

Host is simply a service name, so in our case it is postgres

Whole connection configuration variable in symfony .env looks like this:

DATABASE_URL="postgresql://default:secret@postgres:5432/default?serverVersion=13&charset=utf8"

And that's it, we've set up docker environment with nginx and postgreSQL. Now go and write some code!!!

Top comments (1)

Collapse
 
kellywhite profile image
Kelly White

Thanks for sharing :)