2

Is there some globally admitted coding conventions for docker-compose files? Below is a sample docker-compose.yml from a small project we have here:

version: 3
networks:
  net:
    driver: 'bridge'

services:
  app:
    build: .
    networks:
    - net
    ports:
    - 3000:3000
  redis:
    image: redis:alpine
    networks:
    - net
  client:
    image: "app/test_clients"
    deploy:
      replicas: '3'

The quoting is especially inconsistent. Sometimes quotes are used around text and numerical literals. Sometimes they aren't. Sometimes single quotes are used, sometimes double quotes are used instead. The various example in the official docker-compose documentation seems relatively inconsistent in that area too.

This is valid YAML. And YAML is very permissive about quoting and, as far as I know, quotes could simply be omitted in most of the cases. I know this is probably a matter of internal policy but I wonder if there are some commonly accepted "best practices" in that matter.

  • 2
    This answer seems well considered stackoverflow.com/a/22235064/329496 – simbo1905 Feb 5 '19 at 22:36
  • @simbo1905 Could you post an answer? – 030 Dec 24 '19 at 17:19
  • According to this Q&A one could also use docker-compose config. I tried it and it returned ERROR: Version in "./docker-compose.yml" is invalid - it should be a string. and WARNING: Some services (client) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use docker stack deploy` to deploy to a swarm.`. – 030 Dec 24 '19 at 17:29
0

One could use Composerize to create a consistent docker-compose.yml.

There seems to be two options. Options one is to enter some docker run command, e.g. docker run app/test_clients on their website, which will return:

version: '3.3'
services:
    test_clients:
        image: app/test_clients

Or use the command line: composerize docker run app/test_clients.

| improve this answer | |
0

You could also introduce a YAML linter to your CI to ensure that a consistent docker-compose.yml format will be enforced, like yaml-lint. According the docs it should be possible to define a certain format. If the pipeline fails, then the developer has to apply changes in order to fix the build.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.