Skip to content

docker-compose file improvement #355

@sandstromviktor

Description

@sandstromviktor

Hello Team Whale.
Hope everyone is doing good :) I was doing some docker compose stuff and realized that there some cool yaml things that can simplify your docker-compose.yaml file (and of course other yaml files, like the charts etc).

When you define the studio service here

  studio:
    user: "${UID}:${GID}"
    container_name: studio
    ... etc etc...

you can define a yaml anchor studio: &studio
and then merge the config on other services using the yaml alias <<: *studio

Example

  studio: &studio # Define the anchor here
    user: "${UID}:${GID}"
    container_name: studio
    build:
      context: .
      target: runtime
    image: stackn:develop
    env_file:
      - .env
    command: ["scripts/wait-for-it.sh", "db:5432", "--", sh, scripts/run_web.sh]
    ports:
      - "8080:8080"
    volumes:
      - .:/app:cached
     # - ${PWD}/cluster.conf:/app/cluster.conf

# Now in the celery worker

  celery-worker:
    <<: *studio # Merge configs from studio using the alias
    # Note that i have removed a bunch of duplicated fields here
    container_name: celery-worker
    command: sh ./scripts/run_worker.sh
    volumes:
      - ${PWD}/cluster.conf:/app/cluster.conf
    links:
      - db
      - studio
      - rabbit
      - redis
    depends_on:
      - db
      - studio
      - rabbit
      - redis
    networks:
      internal_network:
        aliases:
          - celery.127.0.0.1.nip.io

What happens?

The alias merges all the config from studio and then you override specific fields afterwards. This makes it easier to define the user, image, env_file etc.. once in the file, instead of doing it multiple times for the different services.

All the best
/Viktor

PS. Serve looks awesome now:)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions