Skip to content

Instantly share code, notes, and snippets.

@pavan538
Last active August 7, 2020 06:48
Show Gist options
  • Save pavan538/b2c6467e91e908a91b4280f076a9313a to your computer and use it in GitHub Desktop.
Save pavan538/b2c6467e91e908a91b4280f076a9313a to your computer and use it in GitHub Desktop.
Docker

sample docker compose

version: '3'
services:
  blob-consumer:
    container_name: article-blob-consumer-container-compose
    image: blob-consumer-image:1.0
    build:
      context: .
      dockerfile: Dockerfile
    env_file:
      - ./deploy/article_blob_consumer_params.env

we can also pass arguments to Dockerfile in two ways using docker-compose file:

build:
  context: .
  args:
    buildno: 1
    password: secret

build:
  context: .
  args:
    - buildno=1
    - password=secret

so in docker file we can use as follows:

ARG buildno
ARG password

RUN echo "Build number: $buildno"
RUN script-requiring-password.sh "$password"
  • You can omit the value when specifying a build argument, in which case its value at build time is the value in the environment where Compose is running.
args:
  - buildno
  - password
  • Note: YAML boolean values (true, false, yes, no, on, off) must be enclosed in quotes, so that the parser interprets them as strings.

we can specify the environment to the container as follows

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:

environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET

we can aslo sepcify the healthcheck as follows

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]
  interval: 1m30s
  timeout: 10s
  retries: 3
  start_period: 40s

we can override the entrypoint of the container build using dockerfile as follows

entrypoint: /code/entrypoint.sh or

entrypoint:
    - php
    - -d
    - zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
    - -d
    - memory_limit=-1
    - vendor/bin/phpunit

we can also configs in docker compose file as follows:

version: "3.3"
services:
  redis:
    image: redis:latest
    deploy:
      replicas: 1
    configs:
      - source: my_config
        target: /redis_config
        uid: '103'
        gid: '103'
        mode: 0440
configs:
  my_config:
    file: ./my_config.txt
  my_other_config:
    external: true

Share Compose configurations between files and projects

  • we can add multiple services in the same configuration file.
web:
  image: example/my_web_app:latest
  links:
    - db
    - cache

db:
  image: postgres:latest

cache:
  image: redis:latest
  • By default, Compose reads two files, a docker-compose.yml and an optional docker-compose.override.yml file. By convention, the docker-compose.yml contains your base configuration. The override file, as its name implies, can contain configuration overrides for existing services or entirely new services.
web:
  build: .
  volumes:
    - '.:/code'
  ports:
    - 8883:80
  environment:
    DEBUG: 'true'

db:
  command: '-d'
  ports:
    - 5432:5432

cache:
  ports:
    - 6379:6379
  • To use multiple override files, or an override file with a different name, you can use the -f option to specify the list of files. Compose merges files in the order they’re specified on the command line. See the docker-compose command reference for more information about using -f. When you run docker-compose up it reads the overrides automatically.
  • To pass multiple files we can use the following command
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

Extending service

  • we can add the new service by extending the service. In common-services.yml
webapp:
  build: .
  ports:
    - "8000:8000"
  volumes:
    - "/data"

In docker-compose.yml

web:
  extends:
    file: common-services.yml
    service: webapp
  environment:
    - DEBUG=1
  cpu_shares: 5

important_web:
  extends: web
  cpu_shares: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment