A Better, Flexible Docker Compose File


We add features to our `docker-compose.yml` file to help our developers.

Let's add a feature for our developers. If we have multiple, some may have ports in use and may not want to bind port 80 and 3306 by default. We can change our docker-compose.yml file to support environment variables for this:

version: '2'
services:
  app:
    build:
      context: ./docker/app
      dockerfile: Dockerfile
    image: shippingdocker.com/app
    volumes:
     - .:/var/www/html
    ports:
     - "${APP_PORT}: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:
     - "${DB_PORT}: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"

Then we can see how to use them:

# Errors, because it needs the env vars set :/
docker-compose ps

# Works! More boilerplate?!
APP_PORT=80 DB_PORT=3306 docker-compose ps

# Let's use port 8888 for our app container:
docker-compose down
APP_PORT=8888 DB_PORT=3306 docker-compose up

Finally, we still have lots of boilerplate! In the next video, we start making our workflow easier.