docker-compose.yml → pronounced “yamel”
- yml is similar to JSON but indentations instead of brackets. No commas
- “Extends” keyword
- Enables sharing of common configurations amongst diff files & projects
- Useful if several different files which use common set of configurations
Example of using “extends” in yaml files
Common.yml
version: '2'
services:
dockerapp:
ports:
- "5000:5000"
redis:
image: redis:3.2.0
docker-compose.yml
version: '2'
services:
dockerapp:
extends:
file: common.yml
service: dockerapp //use common.yml dockerapp code
build: . //dockerapp’s Dockerfile in cwd
redis:
extends:
file: common.yml //use common.yml redis code
service: redis
Example docker-compose files
Example 0: github.com/jleetutorial/dockerapp
------------- version: '2'
Services: //either “build” image with Dockerfile or from image
dockerapp: //local-image-name → (local-image-name_repository_name)
build: .
ports:
- "5000:5000" external host(OsX):internal container
volumes:
- ./app:/app //host:container (ie OsX:container)
//no longer need COPY command in Dockerfile
redis:
image: redis:3.2.0 //naming image
Example 1: github.com/DanWahlin/NodeExpressMongoDBDockerApp
version: '2' // ‘2’ supports volumes & networks (v1 does NOT)
services:
Node:
container_name: ex1Name
build:
context: . // “.” - look in curr dir(of .yml) for dockerfile
dockerfile: node.dockerfile
ports:
- "3000:3000" //leading “-” b/c can have multiple
networks:
- node-app-network //leading “-” b/c can have multiple
mongodb:
image: mongo
networks:
- node-app-network //leading “-” b/c can have multiple
networks:
node-app-network:
driver: bridge
Example 2: github.com/DanWahlin/CodeWithDanDockerServices/blob/master/docker-compose.yml
1. Update config values (localhost --> mongo and localhost --> redis) in config.development.json
2. Set APP_ENV environment variable to proper value in Dockerfile-redis (default is "development")
export APP_ENV=development
export DOCKER_ACCT=<yourHubUserName>
3. Run docker-compose build
4. Run docker-compose up
5. Live long and prosper
version: "2"
services:
nginx: //reverse proxy server. Balance loads node1,2 & 3
container_name: nginx // name container. OK: “-” & CAPS. NO: “ “ & /
image: ${DOCKER_ACCT}/nginx
build:
context: .
dockerfile: .docker/nginx.${APP_ENV}.dockerfile
links:
node1:node1 //links: important here since feeds 3 servers
node2:node2
node3:node3
ports:
"80:80"
"443:443"
Env_file:
- ./.docker/env/app.${APP_ENV}.env
networks:
- codewithdan-network
node1:
container_name: node-codewithdan-1
image: ${DOCKER_ACCT}/node-codewithdan
build:
context: .
dockerfile: .docker/node-codewithdan.${APP_ENV}.dockerfile
ports:
- "8080"
volumes:
- .:/var/www/codewithdan
working_dir: /var/www/codewithdan
env_file:
- ./.docker/env/app.${APP_ENV}.env
networks:
- codewithdan-network
node2:
container_name: node-codewithdan-2
image: ${DOCKER_ACCT}/node-codewithdan
build:
context: .
dockerfile: .docker/node-codewithdan.${APP_ENV}.dockerfile
ports:
- "8080"
volumes:
- .:/var/www/codewithdan
working_dir: /var/www/codewithdan
env_file:
- ./.docker/env/app.${APP_ENV}.env
networks:
- codewithdan-network
node3:
container_name: node-codewithdan-3
image: ${DOCKER_ACCT}/node-codewithdan
build:
context: .
dockerfile: .docker/node-codewithdan.${APP_ENV}.dockerfile
ports:
- "8080"
volumes:
- .:/var/www/codewithdan
working_dir: /var/www/codewithdan
env_file:
- ./.docker/env/app.${APP_ENV}.env
networks:
- codewithdan-network
mongo:
container_name: mongo
image: ${DOCKER_ACCT}/mongo
build:
context: .
dockerfile: .docker/mongo.dockerfile
ports:
- "27017:27017"
env_file:
- ./.docker/env/mongo.${APP_ENV}.env
networks:
- codewithdan-network
redis:
container_name: redis
image: ${DOCKER_ACCT}/redis
build:
context: .
dockerfile: .docker/redis.${APP_ENV}.dockerfile
ports:
- "6379"
networks:
- codewithdan-network
networks:
codewithdan-network:
driver: bridge