skip to Main Content

I start under docker and I do not understand the difference between volumes and configs.

https://docs.docker.com/compose/compose-file/#volumes-top-level-element

https://docs.docker.com/compose/compose-file/#configs-top-level-element (added in version 3.3 of docker compose)

Should the configs properties be defined if, for example, a configuration file is used on different services ?

In what case does it apply ?

For example to share the Apache Root Document (/usr/local/apache2/htdocs) with volumes :

version: '3.8'

services:

  apache:
    image: httpd:2.4
    restart: always
    ports:
      - 8000:80
    volumes:
      - ./:/usr/local/apache2/htdocs
      - php-socket:/run/php
    depends_on:
      - php-fpm
    networks:
      - code

  php-fpm:
    image: php:7.4-fpm
    restart: always
    ports:
      - 9000:9000
    volumes:
      - ./:/usr/local/apache2/htdocs
      - ./.docker/php-fpm/zz-docker.conf:/usr/local/etc/php-fpm.d/zz-docker.conf
      - php-socket:/run/php
    networks:
      - code

volumes:
  php-socket:

networks:
  code:

What is the difference with the configs properties ? :

version: '3.8'

services:

  apache:
    image: httpd:2.4
    restart: always
    ports:
      - 8000:80
    volumes:
      - php-socket:/run/php
    configs:
      - source: apache-www
        target: /usr/local/apache2/htdocs
    depends_on:
      - php-fpm
    networks:
      - code

  php-fpm:
    image: php:7.4-fpm
    restart: always
    ports:
      - 9000:9000
    volumes:
      - ./.docker/php-fpm/zz-docker.conf:/usr/local/etc/php-fpm.d/zz-docker.conf
      - php-socket:/run/php
    configs:
      - source: apache-www
        target: /usr/local/apache2/htdocs
    networks:
      - code

volumes:
  php-socket:

configs:
  apache-www:
    file: ./

networks:
  code:

The two examples above work, But I don’t understand the difference between volumes and configs ?

Can anyone explain to me ? Thanks !

2

Answers


  1. It is the same thing.

    The config attribute is just syntactic sugar and does the same thing as volumes.

    Login or Signup to reply.
  2. Configs were added for Swarm Mode. They are immutable objects added into swarm, that get pushed to the worker node when needed, and mounted as a file in the container. They solved the issue that the file you wanted to mount may not be on the worker node in the cluster.

    If you aren’t using Swarm Mode, you likely don’t need any of the v3 syntax and can stick to either v2 or compose-spec and mount the file directly as a volume. You’ll find newer version of compose have added compatibility features to treat compose files written for Swarm with comparable features in compose, effectively treating these as volume mounts.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search