Our Basic Docker Compose File


We create a basic `docker-compose.yml` file that uses our built images.

So we have images for our application! Let's next create a docker-compose.yml file to spin up these containers, and any others we may need, to run our application and use commands such as composer install to get our PHP dependencies.

Docker Compose File

Create file docker-compose.yml in our project directory.

We cover the details of this in the video.

version: '2'
services:
  app:
    build:
      context: ./docker/app
      dockerfile: Dockerfile
    image: shippingdocker.com/app
    volumes:
     - .:/var/www/html
    ports:
     - "80:80"
    networks:
     - sdnet
  node:
    build:
      context: ./docker/node
      dockerfile: Dockerfile
    image: shippingdocker.com/node
    volumes:
     - .:/var/www/html
    networks:
     - sdnet
  mysql:
    image: mysql:5.7
    ports:
     - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "secret"
      MYSQL_DATABASE: "homestead"
      MYSQL_USER: "homestead"
      MYSQL_PASSWORD: "secret"
    volumes:
     - mysqldata:/var/lib/mysql
    networks:
     - sdnet
  redis:
    image: redis:alpine
    volumes:
     - redisdata:/data
    networks:
     - sdnet
networks:
  sdnet:
    driver: "bridge"
volumes:
  mysqldata:
    driver: "local"
  redisdata:
    driver: "local"

Run the Composer Command

Now we can use this setup to run some commands against our application.

# Don't need to set networks, volume mounts or 
# other config set for the `app` service in
# the docker-compose.yml file
docker-compose run --rm \
    -w /var/www/html \
    app \
    composer install

Up and Running

Then we create a new .env file. Copy the original .env.example and change the following in .env:

DB_HOST=mysql

CACHE_DRIVER=redis
SESSION_DRIVER=redis

REDIS_HOST=redis

We'll need an APP_KEY as well for the .env file:

docker-compose run --rm \
    -w /var/www/html \
    app \
    php artisan key:generate

Redis Library

Note that I already have the predis/predis PHP library, but if you're starting from a Laravel application that does NOT already have this library, you can include it here as well:

docker-compose run --rm \
    -w /var/www/html \
    app \
    composer require predis/predis

Then we can spin up our containers to run this in development:

docker-compose up -d

This will run our app, mysql and redis containers defined. The Node container exists, we can use it, but it doesn't actively run (it's there just for one-off npm or gulp commands).

Check out our containers:

docker-compose ps