Docker Compose file is invalid, additional properties not allowed - docker

This is my sample docker-compose.yml file.
version: '2'
config-server:
image: ccc/config-server
restart: always
registration-server:
image: ccc/registration-server
restart: always
ports:
- 1111:1111
When I use docker-compose up -d I get an error:
"ERROR: The Compose file './docker-compose.yml' is invalid because:
Additional properties are not allowed ('registration-server', 'config-server' were unexpected)
You might be seeing this error because you're using the wrong Compose file version. Either specify a version of "2" (or "2.0") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/

You are missing a services keyword, your correct .yml is:
version: '2'
services:
config-server:
image: ccc/config-server
restart: always
registration-server:
image: ccc/registration-server
restart: always
ports:
- 1111:1111

This can also happpen if one of the keys is misspelled. In my case memory was spelled incorrectly:
After fixing it:
version: "3.8"
services:
redis:
image: redis:alpine
deploy:
resources:
limits:
cpus: '0.50'
memory: 50M
reservations:
cpus: '0.25'
memory: 20M

In the This can also happen if, this can also happens if global level volumes is indented wrong, like not starting at column 0.

Related

Multiple docker-composes access single volume

I have two separate docker-composes and I want them to have access to the same volume but I'm not sure the best approach for this.
This is a roughly how I am trying to make this work:
version: '3'
services:
container1:
volumes:
- myvolume
volumes:
myvolume:
version: '3'
services:
container2:
volumes:
- myvolume
I have one docker compose which defines ands uses a volume and I want to have the other docker compose to also have access to the volume.
I figured it out:
version: '3'
services:
container1:
volumes:
- internal-volume-name
volumes:
internal-volume-name:
name: external-volume-name
version: '3'
services:
container2:
volumes:
- internal-volume-name
volumes:
internal-volume-name:
external: true
name: external-volume-name
Where the volume is created, it must be given a name (external-volume-name).
When you want to reference it from another docker-compose, you must specify external: true and then specify the same given name (name: external-volume-name)

How to use swap memory in docker-compose

I want to use swap memory in my dockerized application because sometimes the container's memory consumption exceeds the limit and they get crashed.
I am using the below configuration in docker-compose which produces an error
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.app1-cnn.deploy.resources.reservations value Additional properties are not allowed ('memory-swap' was unexpected)
docker-compose.yml
version: "3.3"
services:
app-cnn:
build: ./app
image: "app-cnn"
restart: always
container_name: app-cnn
ports:
- "5000:5000"
deploy:
replicas: 1
resources:
limits:
memory: 3G
reservations:
memory-swap: 6G
refer to https://docs.docker.com/compose/compose-file/#memswap_limit
from my test result by default memswap is double of the memory size. if you need more, use memswap_limit to overwrite it.
have to say the structure is wierd somehow, not sure why it is designed like that. Anyway, below configuration works for me.
...
deploy:
resources:
limits:
memory: 512M
reservations:
memory: 128M
memswap_limit: 6G

How to config volumes in docker-compose.yml?

Is a very simple question I guess, but I could not find an answer.
This is my docker-compose.yml
version: '3'
services:
db:
volumes:
- db:/var/lib/mysql
volumes:
db:
nextcloud:
The question is, I want to specify the value of "db" or "nextcloud" in "volumes", and reference them in "Services".like this
services:
db:
volumes:
- db:/var/lib/mysql
nextcloud:
volumes:
- nextcloud:/var/www/html
volumes:
db: /home/roj/DataDisk/nextcloud-insecure/db
nextcloud: /home/roj/DataDisk/nextcloud-insecure/disk
but I got problemERROR: In file './docker-compose.yml', volume 'db' must be a mapping not a string.
how can i fix it ?
The top-level volumes section is not meant to specify mounts but volume driver configuration (see official documention on that matter). ie. this is incorrect
volumes:
db: /home/roj/DataDisk/nextcloud-insecure/db # incorrect
nextcloud: /home/roj/DataDisk/nextcloud-insecure/disk # incorrect
If you want to mount host directories to you container, you must specify it in the volumes section of your services, eg.
services:
db:
volumes:
- /home/roj/DataDisk/nextcloud-insecure/db:/var/lib/mysql
nextcloud:
volumes:
- /home/roj/DataDisk/nextcloud-insecure/disk:/var/www/html
See official documentation on services volumes for more information on that.
Your syntax in the outer volumes instruction is incorrect.
If you want to mount to a docker-managed volume, do this:
services:
test:
image: alpine
volumes:
- db:/app
volumes:
db:
If you want to mount to a local path, do this (you can replace the dot in .:/app with any other local path, like: /home/you:/server/path):
services:
test:
image: alpine
volumes:
- .:/app
If it starts with a dot or a slash, it will be treated as a path, otherwise, as a docker-managed named volume.
These are the common usage patterns, but you can read more about volumes in compose for some additional information.

Docker stack deploy command error running compose yml file docker-compose.yml

[root#d1 docker]# docker stack deploy -c docker-compose.yml getstartedlab
yaml: line 12: did not find expected key
Here is the docker yml file I am using please see the docker-compose.yml file below listed
version: "3"
services:
web:
image: pragneshpanchal/httpdsrv
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition:on-failure
ports:
- "4000:80"
networks:
- webnet
networks:
webnet:
You are missing a space between condition: and on-failure. Please add a space and see if it works.
Did you put your Yaml through a yaml validator?
It's really sensitive to the indentation, so if you put one too many space at some place you can have some strange behavior even with something valid.
I tried to put you file through this validator (http://www.yamllint.com/) and it came false when copy/pasted in it. Try to respect a 2 spaces when working on a child item, like this:
services:
web:
image: pragneshpanchal/httpdsrv
For the moment I can see multiple type of spacing and this will always get you errors. Like in ports and the first network call.
For the second network call, it should be at the same level as services.
And lastly, as stated in Mark answer's there is a missing space in your restart condition.

Docker stack deploy error about top-level object mappings

Following along with the Docker getting started guide, https://docs.docker.com/get-started/part3/#your-first-docker-composeyml-file, I'm running into an issue. I've created the docker-compose.yml file and verified that the contents are correct:
version: "3"
services:
web:
image: joshuabelden/get-started:part2
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
I also verified that I can run my image outside of a swarm. After running the command:
docker stack deploy -c docker-compose.yml getstartedlab
I'm getting the following error:
Top-level object must be a mapping
I can't seem to find any information on the error message.
What I did to solve this is I removed the double quotes and made them single quotes to change
version: "3" -> version: '3'
This removed the error for me, also do this for all double quotes.
You probably didn't save after modifying the docker-compose.yml file. So if you run 'docker compose up' without having saved, you get the error about top-level object mappings.
You have to add the "volumes" where your code should be copied:
version: "3"
services:
web:
image: iconkam/get-started:part2
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
volumes:
- .:/app
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
I am using Symfony 6 on Linux Ubuntu and I had the same type of problem but not with "docker stack".
I had this problem simply when running:
docker-compose up
$ docker-compose up
Top-level object must be a mapping
I searched for a long time to find a solution.
No solution described here worked.
In fact the problem was caused by the presence of a default file in Symfony 6:
docker-compose.override.yml
I commented all the content inside but it was not enough.
Renaming the file made the command "docker-compose up" worked.
Deleting the file is also a solution ;-)
This happens when Docker is running in Kubernetes mode and not swarm.
I fixed it by changing it to Swarm through settings > Kubernetes
This error arises from the formatting of the file. Please try to convert the file encoding to UTF-8 and you will be able to run docker stack deploy command. Double quotes is not an issue here.
In my case, I wrapped all the values in double quotes expect the replica, and it got fixed. Like so:
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: "image details"
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: "50M"
restart_policy:
condition: "on-failure"
ports:
- "4000:80"
networks:
- "webnet"
networks:
webnet:
Sometimes it's just the formatting in the file. I recommend selecting your text in the compose file and seeing if you have a training blank space somewhere.
In my case, I had a space right after the image tag.
Just close and reopen Terminal again - and run command again.
p.s. I'm using Windows + WSL Terminal. Time to time randomly see same error.
Probably you forgot to save the docker compose file.
Another reason this may be happening while you're pulling your hair out:
You have the environment variable COMPOSE_FILE set, which refers to a file which is empty or an invalid yml file.
In my case it was set to docker-compose.combined.yml while running docker compose config > docker-compose.combined.yml, which first created an empty file to write to, and then stated the error Top-level object must be a mapping while trying to read that empty file.
I just had this problem and the reason was that I had an empty docker-compose.override.yml

Resources