Laradock and Docker Dev Environment for Laravel on Ubuntu 18.04

This blog post is designed to help someone set up a Docker development environment on Ubuntu 18.04 LTS and Laravel 5.7. It is highly likely to work on different versions of Ubuntu, including 16.04 LTS and Debian variants.

Setting up a production environment is out of scope of this post.

Beginning and Required Knowledge

This guide assumes the user has:

  • Working knowledge of general Linux commands, and in particular Ubuntu/Debian.
  • Working knowledge of Laravel 5.7.
  • Working knowledge of Nginx and mySQL/MariaDB.
  • Sufficient knowledge of Docker, including the philosophical concepts of containers and images.

Installing Docker

The below commands will update underlying software on your machine, then install some necessary dependencies:

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update
sudo apt install docker-ce docker-compose git

This should start the docker service, which you should check with the below command. If the response says “Active: active (running)” – possibly in green – then it’s right.

sudo systemctl status docker

Optional: Add the user to the docker group

This part is optional, but highly recommended. Unless you do this step, you’ll have to prefix virtually every docker and docker-compose command with “sudo”.

sudo usermod -aG docker YOURUSERNAME
su - YOURUSERNAME

Download Your Existing Laravel Project

In this step, we’re going to download your Laravel project into a subfolder of ~/git (of course, you can substitute whatever folder you like), then clone the Laradock directory into it, and start the docker machines.

cd ~
mkdir git
cd git
git clone https://gitlab.com/username/project
cd project
sudo chmod -R 777 storage bootstrap/cache
docker run --rm --interactive --tty --volume $PWD:/app --user $(id -u):$(id -g) composer install
cp .env.example .env

Now you need to edit your project’s .env file so that the DB_HOST directive is “mariadb”, rather than 127.0.0.1 or whatever else. You also need to make sure that the DB_PORT, DB_DATABASE, DB_USERNAME and DB_PASSWORD settings in your project’s .env file match up to the settings in laradock’s .env file. If you didn’t change these, this will mean you should enter:

  • DB_CONNECTION=mysql
  • DB_HOST=mariadb
  • DB_PORT=3306
  • DB_DATABASE=default
  • DB_USERNAME=default
  • DB_PASSWORD=secret

Once that’s setup, run the below commands:

cd ~/git/project
git clone https://github.com/Laradock/laradock.git
cd laradock
cp env-example .env
docker-compose up -d nginx mariadb

Now wait about 20 minutes for the docker application to set itself up, then visit http://localhost in the browser. Done!

If you need to use phpmyadmin, you can go to http://localhost:8080, and use a server of “mariadb” and username of “default” and password of “secret”. Of course, if you did edit the laradock .env file, then you need to use the user/pass combination that you entered there.

Run Migration and Other Commands

Depending on the state of your project, you’ll probably get an error saying the application key isn’t set, or databases could not be found. This is OK, because we haven’t done that yet. To get it sorted, we’re going to open up the workspace docker container and run what we need.

cd ~/git/project/laradock
docker-compose exec workspace bash

Now you’ll notice that your shell will seem to change. This is because we’re now typing commands into the docker container rather than into our machine. In particular, you’ll notice that you’re now entering commands as root@12ffb1c89566 (random string of characters) instead of yourusername@yourcomputer.

php artisan key:generate
php artisan migrate --seed

Your project may need more commands to set up – you’ll know what these are, it’s your baby! Enter what you need and you should notice the app now runs on http://localhost perfectly.

Setting Up Multiple Projects

If you have multiple projects, then you’ll need to edit Laradock’s .env file to change:

COMPOSE_PROJECT_NAME=laradock-project
NGINX_HOST_HTTP_PORT=9000
MARIADB_PORT=3307

And then run the docker-compose command normally.

Leave a Reply

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