yaml file configuration for multiple services in docker compose - yaml

While I'm configuring my yaml it shows the error below:
version:'3.9'
services:
Web:
image:nginx
database:
image:redis
ERROR: yaml.scanner.ScannerError: mapping values are not allowed here
in ".\docker-compose.yml", line 2, column 9

YAML requires a space after mapping keys:
version: '3.9'
services:
Web:
image: nginx
database:
image: redis
If that space is missing, YAML reads version:'3.9' as single scalar that continues on the next line. On the next line, there is space after the :, but you are now in a multiline scalar, and multiline scalars do not allow implicit mapping keys. This is what the error message is trying to tell you.
You also need to fix the indentation to have a proper docker compose file:
version:'3.9'
services:
Web:
image: nginx
database:
image: redis

Related

Symbols << & * in .gitlab-ci.yml [duplicate]

I recently came across this and was wondering what &django means
version: '2'
services:
django: &django
I can't see anything in the docs related to this.
These are a YAML feature called anchors, and are not particular to Docker Compose. I would suggest you have a look at below URL for more details
https://learnxinyminutes.com/docs/yaml/
Follow the section EXTRA YAML FEATURES
YAML also has a handy feature called 'anchors', which let you easily duplicate
content across your document. Both of these keys will have the same value:
anchored_content: &anchor_name This string will appear as the value of two keys.
other_anchor: *anchor_name
Anchors can be used to duplicate/inherit properties
base: &base
name: Everyone has same name
foo: &foo
<<: *base
age: 10
bar: &bar
<<: *base
age: 20
To complement Tarun's answer, & identifies an anchor and * is an alias referring back to the anchor. It is described as the following in the YAML specification:
In the representation graph, a node may appear in more than one
collection. When serializing such data, the first occurrence of the
node is identified by an anchor. Each subsequent occurrence is
serialized as an alias node which refers back to this anchor.
Sidenote:
For those who want to start using anchors in your docker-compose files, there is more powerful way to make re-usable anchors by using docker-compose YAML extension fields.
version: "3.4"
# x-docker-data is an extension and when docker-compose
# parses the YAML, it will not do anything with it
x-docker-data: &docker-file-info
build:
context: .
dockerfile: Dockerfile
services:
some_service_a:
<<: *docker-file-info
restart: on-failure
ports:
- 8080:9090
some_service_b:
<<: *docker-file-info
restart: on-failure
ports:
- 8080:9595

Project name does not act as prefix while running docker compose up on multiple services

I have set COMPOSE_PROJECT_NAME=xxx in .env file in order to customise project name. But when I use the following:
docker-compose up --abort-on-container-exit --scale influx=0 kafka=0
It throws an error saying -
ERROR: No such service: kafka=0
If I provide only one service then it scales up/down without any error but breaks down when I provide multiple services. Is there a way to fix this?
Here's the structure of docker-compose.yml file -
version: '3'
services:
influx:
image: xxx
volumes:
- xxx
ports:
- xxx
kafka:
image: xxx
volumes:
- xxx
ports:
- xxx
The syntax is docker-compose up [options] continers... and one of the option is --scale and it takes one argument. You can, for exmaple:
docker-compose up --scale influx=1 --scale kafka=2 kafka influx
^^^^^^^^^^^^ - containers, just omit if all
^^^^^^^^ - argument to --scale option
^^^^^^^ - option

How to lint a yaml and ignore multiple "depends_on"?

I wrote a script that creates a local development environment using a docker-compose.yml file.
When running the script, I want to use a yaml linter command to validate that the file is a valid yaml before upping the environment and to do that I'm using the command yamllint.
In this docker-compose.yml file, there is more than one service which "depeneds_on" another service but when I run yamllint, it returns the following error:
47:5 error duplication of key "depends_on" in mapping (key-duplicates)
Which is not a real error, but since the lint is part of the script run then I cannot count on its exit code as it counts this error as an error while in reality, it's not.
An example portion of the docker-compose.yml file:
microservice-one:
image: ms-one:feature-local_development_env
environment:
NODE_ENV: 'development'
NPM_TOKEN: 'SECRET'
ports:
- "3013:3000"
depends_on:
- redis-cluster
microservice-two:
image: ms-two:feature-local_development_env
environment:
NODE_ENV: 'development'
NPM_TOKEN: 'SECRET'
ports:
- "3014:3000"
depends_on:
- redis-cluster
networks:
default:
Is there any other command line yaml linter that you know which will not count more than one "depends_on" as an error?
I found my answer and thought I'll share it with whoever gets here.
So the solution is to override yamllint's default configuration by creating a specific yamllint configuration file.
In my case, the file looks like so:
extends: default
rules:
key-duplicates: disable
Then, I'm running the command like so:
yamllint -d config_file docker-compose.yml
More options can be found in yamllint's official documentation page,
If you need only syntax error and nothing else , below command can be used.
yamllint -d "{rules:{}}"

Hidden reusable blocks in YAML

I am trying to define a reusable block in a docker-compose.yml file in a way that the reusable block definition itself is NOT included in the final (evaluated) YAML.
I know how to define a reusable block with this syntax:
services:
default: &default
image: some/image
dashboard:
<<: *default
command: run dashboard
ports: ["3000:3000"]
But, the above also creates an entry named default under services, which I would like to avoid. In other words, I need the final YAML result to only include dashboard under the services property.
Is this possible with YAML? I was unable to find any reference that discusses this structure clearly enough.
Intuitively, I have tried some variations of the below, but it also did not work.
services:
&default:
image: some/image
dashboard:
<<: *default
command: run dashboard
ports: ["3000:3000"]
This is not possible in YAML 1.2 (or any former version). The reasoning behind this is that YAML has been designed to be a serialization language, not a configuration language.
The Anchor/Alias construct is nice for serializing cyclic data structures. It was never intended to be used for declaring variables that will be used in multiple places. So currently, the only way to create a reusable structure which can be used in multiple places it to define the structure at the first place where it is used. For example:
services:
dashboard:
<<: &default
image: some/image
command: run dashboard
ports: ["3000:3000"]
some_other_service:
<<: *default
other_props: ...
Also, be aware that the merge key << is not part of the YAML spec and only defined as additional feature for YAML 1.1. It is not defined for YAML 1.2 and will be explicitly deprecated for upcoming YAML 1.3.
We (as in: the people currently working on YAML 1.3) are aware of this missing feature and plan to provide a better solution with YAML 1.3.
Docker Compose file format 3.4 adds support for extension fields: top-level keys starting with x- that are ignored by Docker Compose and the Docker engine.
For example:
version: '3.4'
x-default: &default
image: some/image
services:
dashboard:
<<: *default
command: run dashboard
ports: ["3000:3000"]
Source: “Don’t Repeat Yourself with Anchors, Aliases and Extensions in Docker Compose Files” by King Chung Huang https://link.medium.com/N5DFdiC3F0

docker-compose.yml file errors on running docker-compose up

Here is my docker-compose.yml file:
version:'2':
services:
redis:
image: redis
environment:
- HOST='localhost'
- PORT=6379
ports:
-"0.0.0.0:${PORT}:6379"
I get this error on running docker-compose up:
ERROR: The Compose file './docker-compose.yml' is invalid because:
Invalid service name 'services' - only [a-zA-Z0-9\._\-] characters are allowed
Unsupported config option for services: 'redis'
There are multiple problems with your file. The one causing the syntax error is that you have an extra colon on the first line:
version:'2':
that way you define a scalar string key version:'2' with value of null. Since you are therefore not defining the version of the docker compose file, the rest of the file (which is version 2 oriented) fails. This is best resolved by adding a space after version:
In addition your ports definition is incorrect, the value for that should be a sequence/list, and you again specify a scalar string -"0.0.0.0:${PORT}:6379" because there is no space after the initial dash.
Change your docker_compose.yaml file to:
version: '2'
# ^ no colon here
# ^ space here
services:
redis:
image: redis
environment:
- HOST='localhost'
- PORT=6379
ports:
- "0.0.0.0:${PORT}:6379"
# ^ extra space here
just remove last character ":" into string version:'2':
after it docker-compose.yml must be like
version:'2'
services:
redis:
image: redis
environment:
- HOST='localhost'
- PORT=6379
ports:
-"0.0.0.0:${PORT}:6379"

Resources