As a PHP Developer I’ve always had instances of webserver, php & database server running as first level services on my Development machines. I can claim that configuring LAMP (recently LEMP) stack was always one of the first things I’d do when I get a new machine.

I’ve been using Docker for running other services such as Redis, Mongo, Elasticsearch, Neo4J and … but today I am going to completely configure my development environment on top of containers on my new Mac.

Thanks to LaraDock this process should be much easier and smooth. For those of you who don’t know about LaraDock, it’s a set of containers with configurations templates that can come handy to any PHP developers.

Although the project is targeting Laravel developers, but any PHP developer can benefit from these tools.

Why the change?

Running a traditional LAMP/LEMP stack setup has no issues, but occasionally there are some glitches time to time that I’m hoping to avoid by fully going Dockerized.

  • I realised sometimes OS updates (Specially Major Updates) can mess up with services installed on the machine. This is simply inevitable and has happened to me multiple times so far. So this way I only need to make sure my Docker service is running.
  • I realised that I hesitate more on updating my services fearing that they may break. I believe going with Docker should make the process of upgrading those services easier.
  • Things sometimes break! I remember when homebrew simply changed where formulas for PHP were, they added it to the core formulas and I had a hard time updating from PHP 7.0 to 7.2.

I’ll however install php itself on the machine for times that I want to run php scripts within the console or work with the PHP Built-in Webserver.

LaraDock comes with many containers, but I am more interested in a LAMP stack of containers, so we will be starting nginx, mariadb, php-fpm and make sure they will be always up by modifying the restart-policy variable.

Installation of LaraDock

for our purpose we will follow the installation guide provided by LaraDock for multiple project setup.

Firstly clone LaraDock on the main directory which will host all the projects.

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

so basically our directory structure should look like:

.
├── laradock
├── project1
└── project2

now copy the file env-example and save it as .env in laradock folder. the values in this file will be used within the docker-compose.yml file.

cp env-example .env

you can look up the default passwords for different services and change them if you wish to within this file.

autostarting services (optional)

The docker-compose.yml file shipped in LaraDock doesn’t come with any restart policy for any of the services. This means if any of the services crash or you restart/shutdown your machine they won’t be running again until you start them again.

I want mariadb, nginx and (php-fpm needed by nginx) to run all the time, unless I stopped them, so we need to modify the docker-compose.yml and add the following to each of these services:

if you do not want the containers to run all the time simply skip this step.

restart: unless-stopped

For example the nginx section of the file should look like:

### NGINX Server #########################################
    nginx:
      build:
        context: ./nginx
        args:
          - PHP_UPSTREAM_CONTAINER=${NGINX_PHP_UPSTREAM_CONTAINER}
          - PHP_UPSTREAM_PORT=${NGINX_PHP_UPSTREAM_PORT}
          - CHANGE_SOURCE=${CHANGE_SOURCE}
          - http_proxy
          - https_proxy
          - no_proxy
      volumes:
        - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
        - ${NGINX_HOST_LOG_PATH}:/var/log/nginx
        - ${NGINX_SITES_PATH}:/etc/nginx/sites-available
        - ${NGINX_SSL_PATH}:/etc/nginx/ssl
      ports:
        - "${NGINX_HOST_HTTP_PORT}:80"
        - "${NGINX_HOST_HTTPS_PORT}:443"
      depends_on:
        - php-fpm
      networks:
        - frontend
        - backend
      restart: unless-stopped

next will be configuring the nginx virtual hosts for each project. nginx container reads these from nginx/sites. there are few samples provided in this directory. create your own configuration file and modify the following:

    server_name project1.local;
    root /var/www/project1/public;

I am using the domain .local in the server_name variable. so for project1.local to work I will need to either set proper setting in my /etc/hosts file or install a DNS proxy. (for Windows you can use Acrylic & Dnsmasq for Mac)

Now we can start the services by:

docker-compose up -d nginx mariadb

The command above will start the nginx, mariadb & php-fpm in background. and if we made the changes for restart-policy in the docker-compose.yml they will be restarted in case of crash.

so right now the port 80, 443 are bound to the nginx container, and port 3306 will be bound to mariadb container. This means I can use sequel pro or any other sql client to connect to my mariadb instance using localhost or 127.0.0.1 just as I would if I install MariaDB manually.

Remember when you add a new virtualhost file to the nginx/sites folder you will need to restart the nginx container. you can do this with:

docker-compose restart nginx

So basically thats it! I would say it takes even less effort than setting up LAMP Stack. There are tens of other containers shipped with LaraDock project and you can simply make them available to your project by starting the container.

Incoming search terms:

  • laradock
  • laradock restart

Leave a comment

Leave a Reply