Getting Started with Laravel and Docker


Introduction

Docker provides a disposable, virtual development environment via the use of containers, defined in specific build scripts. This allows you to create extremely consistent local hosting environments matched 1:1 with a deployment build process and/or production server and shared among all developers of a project.

Developers using Docker don’t have to install and configure complex databases or web servers. When an app is dockerized, that complexity is pushed into containers that are easily built, shared and run.

Code that ships with Dockerfiles is simpler to work on: Dependencies are pulled as neatly packaged Docker images and anyone with Docker and an editor installed can build and develop the app in minutes.

Key Terms

Container

A container is a runtime instance of a docker image. A Docker container consists of:

  • A Docker image
  • An execution environment
  • A standard set of instructions

The concept is borrowed from Shipping Containers, which define a standard to ship goods globally. Docker defines a standard to ship software.

Image

Docker images are the basis of containers. An Image is an ordered collection of root filesystem changes and the corresponding execution parameters for use within a container runtime. An image typically contains a union of layered filesystems stacked on top of each other. An image does not have state and it never changes.

Installing Docker

Before we continue, you should install Docker on your local machine. Download and install the appropriate Docker Client for your operating system.

Using Docker with Laravel

Once Docker is installed, you’re ready to set up your local file structure, with some configuration to build a local development environment. Don’t worry, a lot of the work is done for you.

You can get a pre-dockerized Laravel install from here: https://github.com/xdega/laravel-with-docker. Or you can simply download and add the following to your existing Laravel project root: https://github.com/xdega/laravel-with-docker/tree/upgrade-only

The above is an actively maintained open-source implementation of Dockerized Laravel. Feel free to submit issues and contribute.

Set your Environment Variables

By default, Laravel uses a .env file to manage your environment settings. This file will not be tracked via Git, as these settings are specific to the deployment environment. Simply copy the following into a .env file in your Laravel project root:

APP_ENV=local
APP_KEY=base64:0/RLFKE+S8qQlhsVab8TVNSKFy4/y+xAtbuIWVwi6IA=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost:8080
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33061
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=guest

This will simply set up your application to link to the local development environment that will be built by Docker.

Build and Run your Docker Containers

To build your local environment, you will simply call the docker-compose script which I have built and set up for general development with Laravel. Run the following command from your Laravel project root:

docker-compose up -d

Pull In your Development Dependencies

By default, Composer and Node dependencies are not tracked via git. To pull in these dependencies, you will want to run the following commands from your project root:

Composer

docker-compose run composer install

Node

docker-compose run app npm install

That’s it!

You should now be able to navigate to localhost:8080 to view the default Laravel welcome view. Welcome to Docker+Laravel!

Additional Commands

Of course, your Laravel workflow will likely utilize other commands such as Artisan. And how do you go about destroying your development envionment?

PHP Artisan

docker-compose run app php artisan

Stop and Destroy your Docker Development Environment

docker-compose down

Note: If you are not running the Docker daemon/client as root/administrator, you will need to run docker commands as sudo

Conclusion

I hope this serves as a helpful guide, to help you get started with a modern container-based development workflow. If you have any issues, questions, or concerns, please do reach out to me. Thank you for reading.