This example comes from the official document. Suppose you have a python application using redis as backend. After writing Dockerfile
, create a docker-compose.yml
file like this:
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
depends_on:
- redis
redis:
image: redis
Then run docker-compose up
will setup the entire application includes: python app and redis.
version: '2'
is the version of the docker-compose file syntaxservices:
is a section that describes the services to runweb:
and redis:
are the names of the services to start, their contents describe how docker should start containers for those servicesdepends_on
implies a dependency of web to redis and therefor docker-compose first starts the redis
container and then the web container. Nevertheless does docker-compose
not wait until the redis
container is ready before starting the web
container. To achieve this you have to use a script that delays the start of the application server or whatever until the redis
container can perform requests.A volumes and networks section can be added as well. Using the volumes section allows for disconnected volume that can live independently of the docker compose services section. The networks section has a similar result.
The redis section of services would have to adjusted like so:
redis:
image: redis
volumes:
- redis-data:/code
networks:
-back-tier
Next, add the following sections to the bottom of the docker compose version 2 file.
volumes:
# Named volume
redis-data:
driver: local
networks:
back-tier:
driver: bridge
redis-data
provides an accessible label from the services section. driver:local
sets the volume to the local file system.
back-tier
sets the networks section label to be accessible in the services section as bridged.