Docker notes #3

Words
200
Reading
1 min
Listen
Play
8y


The original Docker logo


We continue to learn Docker. See the first part, second part on the learning notes.

docker compose

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.

Port mapping


services:
     web:
         image: "emre/my-web-app"
         ports:
              - "80:5000"

Volume mapping

services:
        database:
             image: "mongodb"
             volumes:
                 - /opt/data:/var/lib/mongodb

Stop or Remove containers

  • docker-compose stop to stop containers.

  • docker-compose down to stop everything and remove the containers entirely.

Passing environment files to containers

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.

Placing dependencies on containers

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.

Networking between services

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.

Docker notes #3 | Ecency