We continue to learn Docker. See the first part, second part on the learning notes.
A tool for running multi-container application. An application may need postgresql, redis, or any other 3rd party servers.
Building all of them inside a single container doesn't make any sense in practical.
Example: Running two containers; first one is the web app, second one is the mongodb instance. (filename convention: docker-compose.yml.
services:
web:
image: "emrebeyler/my-web-app"
database:
image: "mongodb"
use docker compose up to run the containers.
services:
web:
image: "emre/my-web-app"
ports:
- "80:5000"
services:
database:
image: "mongodb"
volumes:
- /opt/data:/var/lib/mongodb
docker-compose stop to stop containers.
docker-compose down to stop everything and remove the containers entirely.
services:
database:
image: "mongodb"
environment:
- MYSQL_ROOT_PASSWORD=password
It's also possible to set a environment file:
web:
env_file:
- web-variables.env
It's a good practice to share the configuration information via environment variables.
It's not a good idea to fire up the web app before the database.
version: '3'
services:
web:
depends_on:
- db
db:
image: postgres
docker compose will start the postgres first, and web later since it. It follows the dependency order.
By default, every service can reach each other by their image names. No need to know which IP they operate on.
version: '3'
services:
web:
depends_on:
- db
db:
image: postgres
web service can reach the postgres server by using postgres://db:5432 as a connection string.