Versions of docker-compose file

Versions of docker-compose file

·

2 min read

There are 3 different versions of the docker-compose file and in this article we will look at what those different versions offer and what are their features.

version-1

Note that Version 1 is supported by Compose up to 1.6.x*. It will be deprecated in a future Compose release.*

Version 1 has certain disadvantages such as :

  • Version 1 files cannot declare named volumes.

  • It cannot declare networks.

  • it also cannot declare named build arguments.

  • when you use version 1: every container is placed on the default bridgenetwork and is reachable from every other container at its IP address.

  • You need to use links to enable discovery between containers.

    Example YAML file:

      web:
        build: .
        ports:
         - "8000:5000"
        volumes:
         - .:/code
        links:
         - redis
      redis:
        image: redis
    

version-2

version 2 overcame a lot of disadvantages that were posed by version 1.

Version 2 files are supported by Compose 1.6.0+ and require a Docker Engine of version 1.10.0+.

Advantages of using version 2:

  • Named volumes can be declared under the volumes key

  • Networks can be declared under the networks key.

  • By default, every container joins an application-wide default network and is discoverable at a hostname that’s the same as the service name. This means links are largely unnecessary.

Example YAML file:

version: "2"
services:
  web:
    build: .
    ports:
     - "8000:5000"
    volumes:
     - .:/code
    networks:
      - front-tier
      - back-tier
  redis:
    image: redis
    volumes:
      - redis-data:/var/lib/redis
    networks:
      - back-tier
volumes:
  redis-data:
    driver: local
networks:
  front-tier:
    driver: bridge
  back-tier:
    driver: bridge

Version-3:

version-3 is designed to be cross-compatible between Compose and the Docker Engine’s swarm mode, version 3 removes several options and adds several more.

Removed: volume_driver, volumes_from, cpu_shares, cpu_quota, cpuset, mem_limit, memswap_limit, extends, group_add.

Between versions 2 and 3, several options have been removed, one of those removed options is volume-drivers, the change is explained below ->

volume_driver: Instead of setting the volume driver on the service, define a volume using the top-level volumesoption and specify the driver there.

version: "3.9"
services:
  db:
    image: postgres
    volumes:
      - data:/var/lib/postgresql/data
volumes:
  data:
    driver: mydriver

Thanks for reading my blog.