Home > Back-end >  How do I add a folder with yaml configuration to docker using docker-compose?
How do I add a folder with yaml configuration to docker using docker-compose?

Time:01-12

I have a project structure:

configs
   -config.yaml
server
...
docker-compose.yaml

the docker file is :

version: '3.8'

services:
    volumes:
      - /configs:/configs
    postgres:
        image: postgres:12
        restart: always
        ports:
            - '5432:5432'    
        volumes:
            - ./db_data:/var/lib/postgresql/data
            - ./server/scripts/init.sql:/docker-entrypoint-initdb.d/create_tables.sql
        env_file:
            - local.env    
        healthcheck:
            test: [ "CMD", "pg_isready", "-q", "-d", "devdb", "-U","postgres" ]
            timeout: 45s
            interval: 10s
            retries: 10
    app:
        build: 
          context: ./server/app
          dockerfile: Dockerfile
        env_file:
            - local.env
        environment:
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
            - POSTGRES_DB=devdb

volumes:
  configs:
        

The app uses config.yml and I'm wondering how to add the configs folder to the container? I tried to do this :

volumes:
      - /configs:/configs

but it gives me services.volumes must be a mapping.

How can this be resolved?

CodePudding user response:

You need to put volumes directive inside a service. Probably something like this:

    app:
        build: 
          context: ./server/app
          dockerfile: Dockerfile
        env_file:
            - local.env
        environment:
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
            - POSTGRES_DB=devdb
        volumes:
            - ./configs:/configs

If multiple containers need it you'll have to repeat it in multiple services.

  • Related