Docker Compose

Outline

  • Why
  • Implement
    1. run a database
    2. run a web server
    3. serve a Laravel app
    4. connect to database
  • Recap
  • Cheat Sheet

Why Docker Compose

  • Can have different apps require different enviroments on local machine. For example, one app use php5, the other use php7.
  • Portible enviroment setup of yout app
  • Keep local machine clean. For example, install Composer in container instead of local machine

Implement

clone thise repo

git clone https://github.com/n795113/docker-compose-demo.git

Run A Database

Let's run a MySQL database with phpMyAdmin

cd db docker-compose up -d

go to localhost:8888 check if the database is up

Run A Web Server

cd php_app docker-compose up -d

go to localhost:4444 check if the website served

Run a Laravel App

docker exec -ti example-app sh
composer create-project laravel/laravel example-app

# it will panic without this
cp supervisord.conf example-app
cp Envoy.blade.php example-app

exit

Remap Nginx serve path to example-app. Edit docker-compose.yml

# ...
volumes:
    - ./example-app:/var/www
# ...

Recreate the container

docker-compose down docker-compose up -d

go to localhost:4444 check if the Laravel app served

Laravel App Connect To DB

  1. Create a database. (we can use phpMyAdmin or go into container use CLI)

  2. edit .env of the Laravel app

    ​​​​DB_HOST=172.17.0.1
    ​​​​DB_PASSWORD=root
    
  3. go into the container to build tables

    ​​​​docker exec -ti example-app sh
    

    Run this command to check if the app can connect to DB.

    ​​​​php artisan migrate
    

Recap

  • docker volume (mapping local dir to container dir)
  • how to manage containers
  • container interact with container (172.17.0.1)

Command cheat sheet

# There are 2 ways to go into the container # 1. by name # 2. by id # list all container docker ps # go into a container docker exec -ti <container name or id> # remove a container docker rm -f <container name or id> # build and run a docker compose docker-compose up # up in detach mode docker-compose up -d # force rebuild docker-compose up --build # shot down & remove docker compose docker-compose down