Laravel and Docker setup

I’m creating an exciting new app which cannot be mentioned right now. Unlike most other client projects, I have a blank slate for tech. These became my main considerations:

  • I need to spin up something ASAP
  • I’ve used CodeIgniter and Cake before and I’m more familiar with PHP than Ruby or python at this point
  • Laracasts are hecking great

And Laravel is where I landed.

So far I’ve spent a bunch of time configuring things and noodling around with Docker and Composer, which are both wholly new to me. I ran through Laravel’s installation tutorial and quickly discovered that I have to disable MySQL and Apache for Laravel’s sail up to work properly:

$ sudo service mysqld stop && sudo service apache2 stop

(Which I’ll have to do every time I restart unless I go and fix that permanently, but I don’t want to do that right this moment)

I also got tired of typing ./vendor/bin/sail so I made a bash alias:

alias sail='./vendor/bin/sail'

Since Docker is entirely self-contained, when the container sails up its services are not accessible outside of Docker unless ports get forwarded. I got tripped up here a bit and couldn’t get TablePlus to work (it looked so easy in the Laracast, but alas), so I went with trusty ol’ phpMyAdmin does. The main things to pay attention to are under MySQL, you need to forward the ports properly:

mysql:
  ports:
    - '3356:3306'

And then for PMA, you need to make sure it’s on the correct network (as well as forwarding the ports properly):

phpmyadmin:
  ports:
    - "8080:80"
  networks:
    - sail

Here’s the relevant section in my docker-compose.yml:

mysql:
        image: 'mysql:8.0'
        ports:
            - '3356:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_TCP_PORT: '${DB_PORT}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
          retries: 3
          timeout: 5s
    phpmyadmin:
        image: 'phpmyadmin/phpmyadmin:latest'
        restart: always
        environment:
            PMA_HOST: mysql
            PMA_USER: '${DB_USERNAME}'
            PMA_PASSWORD: '${DB_PASSWORD}'
        ports:
            - "8080:80"
        networks:
            - sail

The next time I sailed up, the Docker image for PMA was automatically downloaded and installed. :thumbsup:

Next challenge was running migrate in the Docker because I don’t want to manually do this — Laravel’s Eloquent is just so, well, eloquent. Turns out, of course, you can execute things from within running containers, with docker exec or docker-compose exec, by giving it the name of the container:

$ docker-compose exec test-app php artisan migrate

And of course, a new bash alias to go with that:

alias de='docker-compose exec'

That way, I can easily run de test-app php artisan tinker to get to the tinker shell interface.

That’s all for now! Now I’m ready to noodle some more with actual Laravel stuff.